[Ansible] 04. When, Block, Hendler
카테고리: ANSIBLE
태그: ansible
[Ansible] 04. When, Block, Hendler
🔔 자세한 모듈 내용은 아래 문서를 참고
모듈 내용 : https://docs.ansible.com/ansible/latest/collections/ansible/builtin/index.html#modules
예제 Github : https://github.com/revenge1005/Ansible_study
🔔 When, Block, Hendler
| 구분 | 설명 |
|---|---|
| When | 조건문, 조건이 참(True)일 경우 Task 실행, 거짓(False)일 경우 Task 미실행 |
| Block | Task 그룹 |
| Handler | 조건부 형식 중에 하나 (조건부 형식이란, if 앞의 테스크가 동작하거나 또는 조건에 맞는다면 handler를 실행한다) Task에서 notify를 이용하여 handler의 name을 매개변수로 전달하여 알린다. 모든 Task가 모두 실행된 후에 Handler가 실행된다. 여러 차레 알람을 받아도 핸들러는 한 번만 실행된다. 플레이에 여러 handler를 포함하면, 해당 핸들러는 알림 순서가 아니라, handler 섹션에 정의된 순서대로 항상 실행된다. |
🔔 Managed 노드의 OS 정보 확인
$ ansible all -m gather_facts --limit node01 | grep ansible_distribution
"ansible_distribution": "Rocky",
"ansible_distribution_file_parsed": true,
"ansible_distribution_file_path": "/etc/redhat-release",
"ansible_distribution_file_variety": "RedHat",
"ansible_distribution_major_version": "8",
"ansible_distribution_release": "Green Obsidian",
"ansible_distribution_version": "8.5",
📜 예제1. 다른 OS 별로 Apache 설치 (Wen 사용)
(1) Ubuntu 노드 추가
$ cat <<EOF >> /etc/hosts
192.168.219.203 node03 node03.test.com
EOF
ssh-copy-id -i ~/.ssh/id_ed25519.pub node03
cat <<EOF >> inventory
node03
EOF
$ ansible -m ping all
(2) Playbook 작성
$ cat <<EOF > install_apache2.yml
---
- hosts: all
become: true
tasks:
- name: update repository index (Ubuntu)
apt:
update_cache: yes
when: ansible_distribution == "Ubuntu" and ansible_distribution_version == "20.04"
- name: install apache2 package (Ubuntu)
apt:
name: apache2
state: latest
when: ansible_distribution in ["Ubuntu", "Debiain"]
- name: apache2 Service Start & enable (Ubuntu)
service:
name: apache2
state: started
enabled: yes
when: ansible_distribution in ["Ubuntu", "Debiain"]
- name: update repository index (Rocky)
dnf:
update_cache: yes
when: ansible_distribution == "Rocky"
- name: install httpd packge (Rocky)
dnf:
name: httpd
state: latest
when: ansible_distribution == "Rocky"
- name: httpd Service Start & enable (Rocky)
service:
name: httpd
state: started
enabled: yes
when: ansible_distribution == "Rocky"
EOF
$ ansible-playbook install_apache2.yml

📜 예제2. Handler 예시
(1) 구성
$ # tree .
.
├── ansible.cfg
├── files
│ └── index.html
├── install_apache3.yml
└── inventory
# cat files/index.html
<h1>Hello World!!!</h1>
(2) Playbook 작성
$ cat <<EOF > install_apache3.yml
---
- name: Install Apache and Upload my Home Page
hosts: all
become: yes
vars:
source_file: ./files/index.html
destin_file: /var/www/html/
tasks:
- name: Install Apache WEB Server
dnf:
name: httpd
state: latest
when: ansible_distribution == "Rocky"
- name: Copy HomePage file to WEB Server
copy:
src: "{{ source_file }}"
dest: "{{ destin_file }}"
mode: 0555
when: ansible_distribution == "Rocky"
notify: Restart Apache
- name: Start WEB Server service
service:
name: httpd
state: started
enabled: yes
when: ansible_distribution == "Rocky"
handlers:
- name: Restart Apache
service:
name: httpd
state: restarted
EOF
$ ansible-playbook install_apache3.yml

📜 예제3. block, When 예제
(1) 구성
$ tree .
.
├── ansible.cfg
├── files
│ └── index.html
├── install_apache4.yml
└── inventory
$ cat files/index.html
<h1>Hello World!!!</h1>
(2) Playbook 작성
$ cat <<EOF > install_apache4.yml
---
- name: Install Apache and Upload my Home Page
hosts: all
become: yes
vars:
source_file: ./files/index.html
destin_file: /var/www/html/
tasks:
- name: Check and Print Linux Version
debug:
var: ansible_os_family
- block: # ======Block for RedHat======
- name: Install Apache WEB Server for RedHat
yum:
name: httpd
state: latest
- name: Copy HomePage file to WEB Server
copy:
src: "{{ source_file }}"
dest: "{{ destin_file }}"
mode: 0555
notify: Restart Apache RedHat
- name: Start WEB Server service for RedHat
service:
name: httpd
state: started
enabled: yes
when: ansible_os_family == "RedHat"
- block: # ======Block for Debian======
- name: Install Apache WEB Server for Debian
apt:
name: apache2
state: latest
- name: Copy HomePage file to WEB Server
copy:
src: "{{ source_file }}"
dest: "{{ destin_file }}"
mode: 0555
notify: Restart Apache Debian
- name: Start WEB Server service for Debian
service:
name: apache2
state: started
enabled: yes
when: ansible_os_family == "Debian"
handlers:
- name: Restart Apache RedHat
service:
name: httpd
state: restarted
- name: Restart Apache Debian
service:
name: apache2
state: restarted
EOF
$ ansible-playbook install_apache4.yml

댓글 남기기