ansible☞playbook-handle

阅读量: zyh 2020-04-28 18:40:44
Categories: > Tags:

功能

用一个短路判断来说,就是两者是串联关系,handlers 用来处理任务后续

tasks && handlers

tasks && handlers - listen (handlers 组)

handlers 的 playbook 样本

---
- hosts: localhost
  become: true
  remote_user: root
  tasks:
  - name: check nginx
    shell: /usr/sbin/nginx -t
    notify:
      log
      
  - meta: flush_handlers 
  
  handlers:
  - name: log
    shell: echo "nginx check success" > ~/playbook.log

多个tasks的时候,tasks后面的 meta: flush_handlers 可以让tasks执行完,立马执行关联的handlers。否则handlers会在所有tasks执行完后,才开始执行

handlers-listen 的 playbook 样本

---
- hosts: localhost
  become: true
  remote_user: root
  tasks:
  - name: check nginx
    shell: /usr/sbin/nginx -t
    notify:
      log group
  
  handlers:
  - name: log1
    listen: log group
    shell: echo "nginx check success 1" > ~/playbook.log
  - name: log2
    listen: log group
    shell: echo "nginx check success 2" >> ~/playbook.log

handlers 通过 listen 绑定在一起, tasks 关联 liasten 绑定 handlers 组,最终 playbook.log 将会写入两行信息