一 ,handlers 和 notify 结合触发条件
handlers(触发器): 定义一些 task 列表, 与之前剧本中 task 没有关系, 只有资源发送变化才会采取一定的操作
notify:notify 中调用 handler 中定义的操作
二, 修改配置文件, 重启服务
1,yum 安装 httpd
- [[email protected] ~]# tree ansible/
- ansible/
├── files
│ └── httpd.conf
├── file.YAML
└── httpd.YAML
- [[email protected] ansible]# cat httpd.YAML
- ---
- - hosts: date
- remote_user: root
- tasks:
- - name: install httpd package
- yum: name=httpd
- - name: copy conf file
- copy: src=files/httpd.conf dest=/etc/httpd/conf backup=yes
- - name: start service
- service: name=httpd state=started enabled=yes
2, 修改配置文件端口 8081, 在次执行, httpd 服务没有重启
- [[email protected] ansible]# cat httpd.YAML
- ---
- - hosts: date
- remote_user: root
- tasks:
- - name: install httpd package
- yum: name=httpd
- - name: copy conf file
- copy: src=files/httpd.conf dest=/etc/httpd/conf backup=yes
- notify: restart service
- - name: start service
- service: name=httpd state=started enabled=yes
- handlers:
- - name: restart service
- service: name=httpd state=restarted
三, 定义多个 notify
来源: http://www.bubuko.com/infodetail-3356436.html