handlers特性:
1.剧本提供的handlers特性,是专门的用于实践,任务触发模块机制2.有 任务1 发生变化后,必然会出发 任务2(比如,更改了nginx配置文件,必然要重启)
- name: handlers and notify
hosts: xp
remote_user: root
tasks:
- name: copy rsyncd.conf
copy:
src=/script/rsyncd.conf ##这里的文件一旦发生改变,copy模块就会返回changed状态,
dest=/etc/
notify: ## 返回状态之后就会在这里出发 notify属性
- restart rsyncd.service ##触发notify属性,就会执行这里的列表事件,去找handles对应的列表
handlers:
- name: restart rsyncd.service
systemd:
name: rsyncd
state: restarted
解释
1.handlers关键字必须在剧本结尾
2.handlers是定义事件列表,可以定义多个要执行的事件,给每一个事件起一个名字
如何调用handlers :
通过notify关键字,notify关键字是写在tasks列表里的,当某一个任务的确发生了changed更改状态,就会触发notify的执行,执行notify指定的名称 handlers事件