Fork me on GitHub

handlers01.yaml

---
- hosts: ubuntu
  handlers:
  - name: restart apache
    service: name=apache2 state=restarted
  tasks:
    - name: 确保APT包列表是最新的
      apt: update_cache=yes
    - name: 部署apache2
      apt: name=apache2 state=present
    - name: 设置开机自启
      service: name=apache2 enabled=yes
    - name: 开启Apache rewrite模块
      apache2_module: name=rewrite state=present
      notify: restart apache
root@test:~/handlers# ansible-playbook handlers01.yaml 

PLAY [ubuntu] ******************************************************************************************************************************

TASK [Gathering Facts] *********************************************************************************************************************
ok: [192.168.255.110]

TASK [确保APT包列表是最新的] ************************************************************************************************************************
changed: [192.168.255.110]

TASK [部署apache2] ***************************************************************************************************************************
ok: [192.168.255.110]

TASK [设置开机自启] ******************************************************************************************************************************
ok: [192.168.255.110]

TASK [开启Apache rewrite模块] ******************************************************************************************************************
ok: [192.168.255.110]

PLAY RECAP *********************************************************************************************************************************
192.168.255.110            : ok=5    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 

handlers02.yaml

---
- hosts: ubuntu
  handlers:
  - name: restart apache
    service: name=apache2 state=restarted
    notify: restart memcached
  - name: restart memcached
    service: name=memcached state=restarted
  tasks:
    - name: 确保APT包列表是最新的
      apt: update_cache=yes
    - name: 部署apache2
      apt: name=apache2 state=present
    - name: 设置开机自启
      service: name=apache2 enabled=yes
    - name: 开启Apache rewrite模块
      apache2_module: name=rewrite state=present
      notify: restart apache

    - name: 部署memcached
      apt: name=memcached state=present
    - name: 设置开机自启
      service: name=memcached enabled=yes state=started
    - name: 创建目录
      file:
        path: /etc/memcached/
        state: directory
        mode: 0755
    - name: 指定配置文件
      template:
        src: templates/memcached.conf.j2
        dest: /etc/memcached/memcached.conf
        mode: 0755
      #这里通过changed_when设置为修改了配置文件,从而触发重启apache,同时重启memcached
      changed_when: true
      notify:
        - restart apache

    - name: 部署vim
      apt: name=vim state=present

root@test:~/handlers# ansible-playbook handlers02.yaml 

PLAY [ubuntu] ******************************************************************************************************************************

TASK [Gathering Facts] *********************************************************************************************************************
ok: [192.168.255.110]

TASK [确保APT包列表是最新的] ************************************************************************************************************************
changed: [192.168.255.110]

TASK [部署apache2] ***************************************************************************************************************************
ok: [192.168.255.110]

TASK [设置开机自启] ******************************************************************************************************************************
ok: [192.168.255.110]

TASK [开启Apache rewrite模块] ******************************************************************************************************************
ok: [192.168.255.110]

TASK [部署memcached] *************************************************************************************************************************
ok: [192.168.255.110]

TASK [设置开机自启] ******************************************************************************************************************************
ok: [192.168.255.110]

TASK [创建目录] ********************************************************************************************************************************
ok: [192.168.255.110]

TASK [指定配置文件] ******************************************************************************************************************************
changed: [192.168.255.110]

TASK [部署vim] *******************************************************************************************************************************
ok: [192.168.255.110]

RUNNING HANDLER [restart apache] ***********************************************************************************************************
changed: [192.168.255.110]

RUNNING HANDLER [restart memcached] ********************************************************************************************************
changed: [192.168.255.110]

PLAY RECAP *********************************************************************************************************************************
192.168.255.110            : ok=12   changed=4    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
---
- hosts: ubuntu
  handlers:
  - name: restart apache
    service: name=apache2 state=restarted
    notify: restart memcached
  - name: restart memcached
    service: name=memcached state=restarted
  tasks:
    - name: 确保APT包列表是最新的
      apt: update_cache=yes
    - name: 部署apache2
      apt: name=apache2 state=present
    - name: 设置开机自启
      service: name=apache2 enabled=yes
    - name: 开启Apache rewrite模块
      apache2_module: name=rewrite state=present
      notify: restart apache

    - name: 部署memcached
      apt: name=memcached state=present
    - name: 设置开机自启
      service: name=memcached enabled=yes state=started
    - name: 创建目录
      file:
        path: /etc/memcached/
        state: directory
        mode: 0755
    - name: 指定配置文件
      template:
        src: templates/memcached.conf.j2
        dest: /etc/memcached/memcached.conf
        mode: 0755
      #这里通过changed_when设置为修改了配置文件,从而触发重启apache,同时重启memcached
      changed_when: true
      notify:
        - restart apache

    - name: 立即执行handlers
      meta: flush_handlers

    - name: 部署vim
      apt: name=vim state=present
root@test:~/handlers# ansible-playbook handlers03.yaml 

PLAY [ubuntu] ******************************************************************************************************************************

TASK [Gathering Facts] *********************************************************************************************************************
ok: [192.168.255.110]

TASK [确保APT包列表是最新的] ************************************************************************************************************************
changed: [192.168.255.110]

TASK [部署apache2] ***************************************************************************************************************************
ok: [192.168.255.110]

TASK [设置开机自启] ******************************************************************************************************************************
ok: [192.168.255.110]

TASK [开启Apache rewrite模块] ******************************************************************************************************************
ok: [192.168.255.110]

TASK [部署memcached] *************************************************************************************************************************
ok: [192.168.255.110]

TASK [设置开机自启] ******************************************************************************************************************************
ok: [192.168.255.110]

TASK [创建目录] ********************************************************************************************************************************
ok: [192.168.255.110]

TASK [指定配置文件] ******************************************************************************************************************************
changed: [192.168.255.110]

RUNNING HANDLER [restart apache] ***********************************************************************************************************
changed: [192.168.255.110]

RUNNING HANDLER [restart memcached] ********************************************************************************************************
changed: [192.168.255.110]

TASK [部署vim] *******************************************************************************************************************************
ok: [192.168.255.110]

PLAY RECAP *********************************************************************************************************************************
192.168.255.110            : ok=12   changed=4    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  

Handlers只有在其所在的任务被执行时,才会被运行

如果一个任务中定义了notify调用Handlers,但是由于条件判断等原因,该任务未被执行,那么Handlers同样不会被执行

Handlers只会在Play的末尾运行一次;

如果想在一个Playbook的中间运行Handlers,则需要使用meta模块来实现,

meta: flush_handlers

---
- hosts: ubuntu
  handlers:
  - name: restart apache
    service: name=apache2 state=restarted
  tasks:
    - block:
        - name: Task that might fail
          command: /bin/false
          notify: restart apache #如果条件为错误,不能触发handlers
      rescue:
        - name: Handle the failure
          command: /bin/true      #保证触发handlers
          notify: restart apache
      always:
        - meta: flush_handlers  # 确保 handlers 被执行

    - name: 部署vim
      apt: name=vim state=present
root@test:~/handlers# ansible-playbook handlers04.yaml  

PLAY [ubuntu] ******************************************************************************************************************************

TASK [Gathering Facts] *********************************************************************************************************************
ok: [192.168.255.110]

TASK [Task that might fail] ****************************************************************************************************************
fatal: [192.168.255.110]: FAILED! => {"changed": true, "cmd": ["/bin/false"], "delta": "0:00:00.004002", "end": "2024-08-13 07:01:49.166651", "msg": "non-zero return code", "rc": 1, "start": "2024-08-13 07:01:49.162649", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}

TASK [Handle the failure] ******************************************************************************************************************
changed: [192.168.255.110]

RUNNING HANDLER [restart apache] ***********************************************************************************************************
changed: [192.168.255.110]

TASK [部署vim] *******************************************************************************************************************************
ok: [192.168.255.110]

PLAY RECAP *********************************************************************************************************************************
192.168.255.110            : ok=4    changed=2    unreachable=0    failed=0    skipped=0    rescued=1    ignored=0   
posted on 2024-08-13 11:34  anyux  阅读(11)  评论(0)    收藏  举报