handlers 用法如下,表示当 tasks 执行成功之后再执行 handlers,相当于 shell 中的 && 用法,如果 tasks 执行失败是不会执行 handlers 语句的
[root@localhost ~]$ cat handlers.yml
---
- name: handlers test
hosts: 192.168.119.134
user: root
tasks:
- name: copy file
copy: src=/etc/passwd dest=/tmp/aaa.txt
notify: test handlers # 这里要指定执行哪个handlers,对应下面的name,我们可以定义多个handlers,通过 handlers name 来识别不同的handlers
handlers: # 表示如果拷贝文件成功,就使用 shell 模块执行 echo "abc" >> /tmp/aaa.txt 命令
- name: test handlers
shell: echo "abc" >> /tmp/aaa.txt
来源:https://www.cnblogs.com/pzk7788/p/10216666.html