1. [root@vagrant test]# vagrant ssh ansible-server

  2. [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
    
  3. [vagrant@ansible-server ~]$ anp han.yml -k

    이러고 또 하면 이미 httpd는 생성되어 있으므로 task의 결과가 changed가 아니라 ok 또는 failed다.

    이 경우 handler가 실행되지 않는다.

practice

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