ansible-playbook 之handlers和notify(三)


一 、handlers和notify結合觸發條件

handlers(觸發器): 定義一些task列表,與之前劇本中task沒有關系,只有資源發送變化才會采取一定的操作

notify:notify中調用handler中定義的操作

二、修改配置文件,重啟服務

1、yum安裝httpd

[root@linux-node1 ~]# tree ansible/
ansible/
├── files
│   └── httpd.conf
├── file.yaml
└── httpd.yaml
[root@linux-node1 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服務沒有重啟

[root@linux-node1 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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM