Introduction handlers

Introduction mentioned task processor is playbook when handlers, and the task previously used to define the same tasks are used, except that handlers will trigger the need to meet certain conditions task operation, it is necessary to notify a task's corresponding notification handler after the task will be executed. No matter how many informants notify the handler, we will need to wait until after the playbook for all task execution is completed, and finally execute the corresponding handlers, and will only be executed once.

Description begins with understanding it may be more around, this section will come to understand through case handlers concrete action.

1 Review Questions

On a "  PlayBook basis" [ 2.1.5 Case 4 : modify the configuration file], we reserved a problem, and now look at the time defined PlayBook :

sandboxMP) [root@sandboxmp ~]$ cat tasks/modify_nginx.yml 
---
- hosts: server
  remote_user: root
  tasks: 
   - name: Modify the default port
     lineinfile:        
    path: /etc/nginx/nginx.conf  
     regexp: "(.*)listen(.*) 80 (.*)"     
     line: '\1listen\2 8080 \3'      
     backrefs: yes
     state: present
   - name: restart nginx      
     service: name=nginx state=restarted 

  

Our original intention after modifying the configuration file, restart the corresponding service configuration to allow the changes to take effect. In the test execution time indeed achieve this functionality.

Then again perform this playbook to see results:

(sandboxMP) [root@sandboxmp ~]$ ansible-playbook tasks/modify_nginx.yml

 

PLAY [server] ***************************************************************************************************************************************************************************************

 

TASK [Gathering Facts] ******************************************************************************************************************************************************************************

ok: [172.16.3.101]

ok: [172.16.3.102]

 

TASK [Modify the default port] **********************************************************************************************************************************************************************

ok: [172.16.3.101]

ok: [172.16.3.102]

 

TASK [restart nginx] ********************************************************************************************************************************************************************************

changed: [172.16.3.101]

changed: [172.16.3.102]

 

PLAY RECAP ******************************************************************************************************************************************************************************************

172.16.3.101               : ok=3    changed=1    unreachable=0    failed=0   

172.16.3.102               : ok=3    changed=1    unreachable=0    failed=0   

 Look at the execution record:

  • [The Modify default at The Port] : because in the first run, has completed modifying the configuration file content, so the state is running again ok . This task is not made any changes on a remote server;
  • [restart nginx] : Although this did not modify the configuration file contents, but still perform the restart nginx service tasks, status is changed .

2 Use Handler

2.1 handler to resolve the remaining issues

Use Handler can be very good to avoid the above problems, because Handler will trigger operation tasks need to meet certain conditions. handler is also very simple requiring only task in notify the corresponding handler can. Modify /tmp/modify_nginx.yml , using handlers :

 

sandboxMP) [root@sandboxmp ~]$ vim tasks/modify_nginx.yml
---
- hosts: server
  remote_user: root
  tasks: 
    - name: Modify the default port
      lineinfile:  
        path: /etc/nginx/nginx.conf     
        regexp: "(.*)listen(.*) 80 (.*)"  
        line: '\1listen\2 8080 \3'        
        backrefs: yes        
        state: present      
      notify: handler of the restart nginx service # need to perform name  
  handlers:  
    - name: restart nginx service # in a task defined handler
      service: name=nginx state=restarted

We can see the handler definition and the task definition is the same as defined handler names, modules, and module parameters to pass; the task requires only motify want to run handlser name. Note indent ( introducing YAML syntax, description has been unavailable Table , or have a friend with Table to indent, causing the error .)

Run PlayBook :

(

sandboxMP) [root@sandboxmp ~]# ansible-playbook tasks/modify_nginx.yml   

 

PLAY [server] ***************************************************************************************************************************************************************************************

 

TASK [Gathering Facts] ******************************************************************************************************************************************************************************

ok: [172.16.3.102]

ok: [172.16.3.101]

 

TASK [Modify the default port] **********************************************************************************************************************************************************************

ok: [172.16.3.101]

ok: [172.16.3.102]

 

PLAY RECAP ******************************************************************************************************************************************************************************************

172.16.3.101               : ok=2    changed=0    unreachable=0    failed=0   

172.16.3.102               : ok=2    changed=0    unreachable=0    failed=0   

  

See [Modify the default port] execution result is the OK , there is no change in operation occurs, the handler is not performed (results not handler execution information)

Log server01 (172.16.3.101) , modify /etc/nginx/nginx.conf configuration, the monitor port to 80 :

[root@server01 ~]$ vim /etc/nginx/nginx.conf

# Find the server tab, listen to 80 ports, save and exit '' 'in front of the contents of omission' ''

 server {

        listen       80 default_server;

        listen       [::]:80 default_server;

        server_name  _;

        root         /usr/share/nginx/html;

  

 

[root @ server01 ~] $ systemctl nginx restart # restart nginx service

Back sandboxMP (172.16.3.100) rerun PlayBook :

(sandboxMP) [root@sandboxmp ~]$ ansible-playbook tasks/modify_nginx.yml

 

PLAY [server] ***************************************************************************************************************************************************************************************

 

TASK [Gathering Facts] ******************************************************************************************************************************************************************************

ok: [172.16.3.102]

ok: [172.16.3.101]

 

TASK [Modify the default port] **********************************************************************************************************************************************************************

changed: [172.16.3.101]

ok: [172.16.3.102]

 

RUNNING HANDLER [restart nginx service] *************************************************************************************************************************************************************

changed: [172.16.3.101]

 

PLAY RECAP ******************************************************************************************************************************************************************************************

172.16.3.101               : ok=3    changed=2    unreachable=0    failed=0   

172.16.3.102               : ok=2    changed=0    unreachable=0    failed=0

  

Run results show that:

  • [The Modify Port The default] : Server01 configuration task contents is modified ( 80 becomes 8080 ), the state is changed ; and servier02 is 8080 , the task has not been modified, the status is OK ;
  • [restart nginx Service] : because server01 (172.16.3.101) file has changed, so by notify the successful implementation of the Handler , restart nginx services; servier02 (172.16.3.102) is not executed Handler .

So when the need to batch modify the configuration file, need to restart the service, rational use of handlers , can effectively avoid unnecessary reboot.

2.1 Create multiple handlers

And tasks as can also create multiple handlers , a task can notify multiple handlers , multiple task can notify the same Handler , for example:

# The following examples are for illustration only, need not be tested on the system ( none of these systems on the environment )

---

- hosts: server
remote_user: root tasks:
- name: upload project files copy: src: /root/project dest: /tmp/ notify: - restart nginx service
- restart mysql service
handlers: - name: restart nginx service
service: name=nginx state=restarted - name: restart mysql service
service: name=mysql state=restarted

  

In the above presentation playbook assume upload a project file, then restart nginx and mysql service.

 

Guess you like

Origin www.cnblogs.com/jameslove/p/10927145.html