일종의 함수
task 수행 결과가 changed인 경우 미리 만들어놓은 handler를 실행한다.
Ex) 웹 서버가 설치되면(changed) httpd service 시작(handler 시작)
→ 미리 만들어놔야 됨
[root@vagrant test]# vagrant ssh ansible-server
[vagrant@ansible-server ~]$ vi han.yml
- name: play handler
hosts: web1
become: yes
gather_facts: yes
tasks:
- name: Install httpd
yum:
name: httpd
state: present
notify: # handler 호출하는 module
- start_handler
# 위 조건이 충족되면, 즉 task 수행결과가 changed면
# handler가 실행되고,
# handler는 name으로 특정된다.
handlers: # 미리 만들어놔야 실행된다.
- name: start_handler
service:
name: httpd
state: started
enabled: yes
[vagrant@ansible-server ~]$ anp han.yml -k
이러고 또 하면 이미 httpd는 생성되어 있으므로 task의 결과가 changed가 아니라 ok 또는 failed다.
이 경우 handler가 실행되지 않는다.
ansible-server의 index.html을 node02로 복사하는 playbook을 작성하되 index.html에 대해 변경사항이 발생한 경우 httpd를 서비스를 재시작하도록 하시오.
- name: play handler
hosts: 172.16.1.12
become: yes
gather_facts: yes
tasks:
- name: Install httpd
yum:
name: httpd
state: present
- name: start httpd
service:
name: httpd
state: started
enabled: yes
- copy:
src: httpd-index.html
dest: /var/www/html/index.html
notify:
- restart_handler
handlers:
- name: restart_handler
service:
name: httpd
state: restarted