0부터 시작하는 ANSIBLE 공부 - Include_tasks & If 실습

Jaehong Lee·2022년 10월 13일
post-thumbnail

1. ip_forward 설정 & vagrant 재시작할 경우

vagrant 재시작시 문제 해결

  • 일시정지 후 재실행 -> 네트워크 연결 문제
  • 가상머신이 실행되지 않는다면 -> virsh start project이름 node이름
  • 네트워크 문제인 경우 -> virsh net-list --all 을 하고, 네트워크를 virsh net-autostart network 이름

이래도 안되면, Host Pc 를 재부팅하자


ip_forward

echo "1" > /proc/sys/net/ipv4/ip_forward
  • ip forward 는 forwarding 에 대한 설정이다. 즉, ip_forward 를 1 로 설정하면, Host 에서 forwarding 을 허용하여 Host 위의 Guest 들이 Host 를 거쳐 외부로 나갈 수 있게하는 것이다

2. include_tasks & if - install

install_nginx.yaml

---
- name: install nginx
  hosts: all
  become: yes
  vars:
    dist: "{{ 'centos' if ansible_distribution == 'CentOS'
               else 'ubuntu' if ansible_distribution == 'Ubuntu'
               else 'linux' }}"
  tasks:
    - name: install nginx per distribution
      include_tasks: "install_{{dist}}.yaml"
  • 메인 설치 파일을 정의한다. 여기서 Node 의 facts 에서 OS 를 확인하여 조건문을 통해 OS 에 맞는 설치 파일을 실행한다

install_centos.yaml

- name: install epel-release
  action: "{{ ansible_pkg_mgr }} name=epel-release state=latest"

- name: install nginx
  action: "{{ ansible_pkg_mgr }} name=nginx state=present"

- name: create index.html
  get_url: url=https://www.nginx.com dest=/usr/share/nginx/html

- name: start nginx
  service: name=nginx state=started
  • CentOS 에서 nginx 를 설치하고, 실행하기 위한 yaml 파일 작성

install_ubuntu.yaml

- name: install nginx
  action: "{{ ansible_pkg_mgr }} name=nginx state=present"

- name: create index.html
  get_url: url=https://www.nginx.com dest=/var/www/html
  • Ubuntu 에서 nginx 를 설치하는 yaml 파일 작성

install 실행

ansible-playbook install_nginx.yaml

PLAY RECAP ********************************************************************************************************************************
centos1                    : ok=6    changed=4    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
centos2                    : ok=6    changed=4    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
centos3                    : ok=6    changed=4    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
ubuntu1                    : ok=4    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
ubuntu2                    : ok=4    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
  • 설치시 skip 이 발생하지 않는다

3. include_tasks & if - remove

remove_nginx.yaml

---
- name: remove nginx
  hosts: all
  become: yes
  vars:
    dist: "{{ 'centos' if ansible_distribution == 'CentOS'
               else 'ubuntu' if ansible_distribution == 'Ubuntu'
               else 'linux' }}"
  tasks:
    - name: remove nginx per distribution
      include_tasks: "remove_{{dist}}.yaml"
  • 위와 같이 작성해주자

remove_centos.yaml

- name: remove epel-release
  action: "{{ ansible_pkg_mgr }} name=epel-release state=absent"

- name: remove nginx
  action: "{{ ansible_pkg_mgr }} name=nginx state=absent"
  • CentOS 에서 nginx 삭제하기 위한 yaml 파일

remove_ubuntu.yaml

- name: remove nginx & etc...
  action: "{{ ansible_pkg_mgr }} name=nginx state=absent autoremove=yes"
  
- name: stop nginx
  service: name=nginx state=stopped
  • Ubuntu 에서 nginx 와 관련된 것들을 삭제하기 위한 yaml 파일

remove 실행

ansible-playbook remove_nginx.yaml

PLAY RECAP ********************************************************************************************************************************
centos1                    : ok=4    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
centos2                    : ok=4    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
centos3                    : ok=4    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
ubuntu1                    : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
ubuntu2                    : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
  • 마찬가지로 skip 이 발생하지 않는다
profile
멋진 엔지니어가 될 때까지

0개의 댓글