playbook核心元素之 --> handlers 介绍


Handlers:由特定条件触发的Tasks;


[root@ansible ~]# vim test2.yaml
- hosts: centos7
  remote_user: root
  tasks:
   - name: install httpd
     yum: name=httpd state=latest
   - name: copy config file
     copy: src=/root/httpd.conf dest=/etc/httpd/conf/httpd.conf
     notify: restart httpd service   --> 和notify配合使用即可
   - name: start httpd service
     service: name=httpd state=started
  handlers:
   - name: restart httpd service
     service: name=httpd state=restarted


wKioL1g-QjiiG2gBAAAxpv3CuB0666.png-wh_50



[root@ansible ~]# ansible-playbook test2.yaml


wKioL1g-QlqhrB1_AABtk3sQPkU982.png-wh_50


[root@ansible ~]# vim httpd.conf
Listen 8080  --> 修改httpd的配置文件,将监听的端口修改为8080


[root@ansible ~]# ansible-playbook test2.yaml


wKiom1g-QoSDQ3kgAAB8Dm3zRFc119.png-wh_50


[root@node7-1 ~]# netstat -tlnp | grep "httpd"
tcp6     0    0 :::8080      :::*      LISTEN     5908/httpd
//对应节点上的httpd已经成功监听在8080端口了