谈谈 ansible handlers

百度了一下handlers force_handlers的用法,几乎很多文章都是这样描述的: 

Handlers只有在其所在的任务被执行时,才会被运行;如果一个任务中定义了notify调用Handlers,但是由于条件判断等原因,该任务未被执行,那么Handlers同样不会被执行。

Handlers只会在每一个play的末尾运行一次;如果想在一个playbook中间运行Handlers,则需要使用meta模块来实现。例如: -meta: flush_handlers.

如果一个play在运行到调用Handlers的语句之前失败了,那么这个Handlers将不会被执行。我们可以使用meta模块的--force-handlers选项来强制执行Handlers,即使Handlers所在的play中途运行失败也能执行。

于是乎就实际操作使用了一下handlers,做出如下总结:

1. 最简单的ansible handlers

1.1 playbook

# cat handlers_sample.yaml 
---
- hosts: 127.0.0.1
  handlers:
    - name: echo handlers
      shell: echo "handlers"

  tasks:
    - name: 输出 test1
      shell: echo "test1"

      notify:
        - echo handlers

    - name: 输出test2
      shell: echo "test2"

    - name: 输出test3
      shell: echo "test3"
# 

1.2 执行结果

# ansible-playbook -v handlers_sample.yaml 
Using /etc/ansible/ansible.cfg as config file

PLAY [127.0.0.1] ***************************************************************************************************************************************************************************

TASK [Gathering Facts] *********************************************************************************************************************************************************************
ok: [127.0.0.1]

TASK [输出 test1] ****************************************************************************************************************************************************************************
changed: [127.0.0.1] => {"changed": true, "cmd": "echo "test1"", "delta": "0:00:00.002695", "end": "2019-05-25 02:22:36.625148", "rc": 0, "start": "2019-05-25 02:22:36.622453", "stderr": "", "stderr_lines": [], "stdout": "test1", "stdout_lines": ["test1"]}

TASK [输出test2] *****************************************************************************************************************************************************************************
changed: [127.0.0.1] => {"changed": true, "cmd": "echo "test2"", "delta": "0:00:00.002935", "end": "2019-05-25 02:22:36.898646", "rc": 0, "start": "2019-05-25 02:22:36.895711", "stderr": "", "stderr_lines": [], "stdout": "test2", "stdout_lines": ["test2"]}

TASK [输出test3] *****************************************************************************************************************************************************************************
changed: [127.0.0.1] => {"changed": true, "cmd": "echo "test3"", "delta": "0:00:00.002666", "end": "2019-05-25 02:22:37.141926", "rc": 0, "start": "2019-05-25 02:22:37.139260", "stderr": "", "stderr_lines": [], "stdout": "test3", "stdout_lines": ["test3"]}

RUNNING HANDLER [echo handlers] ************************************************************************************************************************************************************
changed: [127.0.0.1] => {"changed": true, "cmd": "echo "handlers"", "delta": "0:00:00.002762", "end": "2019-05-25 02:22:37.401675", "rc": 0, "start": "2019-05-25 02:22:37.398913", "stderr": "", "stderr_lines": [], "stdout": "handlers", "stdout_lines": ["handlers"]}

PLAY RECAP *********************************************************************************************************************************************************************************
127.0.0.1                  : ok=5    changed=4    unreachable=0    failed=0   

# 

可以发现,输出的结果为: test1 test2 test3 handlers , 

2. ansible使用flush_handlers 在中间执行

注意要点: 1. 在使用flush_handlers之后,ansible会就此从后往上执行flush 之前的 handlers

例如如下playbook应该执行输出顺序为: test5 ——> test1 ——> handlers ——> handlers2 ——> test2 ——> test3 ——> handler3

2.1 playbook

# cat handlers_flush_handlers_sample.yaml 
---
- hosts: 127.0.0.1
  handlers:
    - name: echo handlers
      shell: echo "handlers"
   
    - name: echo handlers2
      shell: echo "handlers2"

    - name: echo handlers3
      shell: echo "handlers3"

  tasks:
    - name: 输出 test1
      shell: echo "test5"

      notify:
        - echo handlers2

    - name: 输出 test1
      shell: echo "test1"

      notify:
        - echo handlers

    - name: 调用meta模块的flush_handlers
      meta: flush_handlers

    - name: 输出test2
      shell: echo "test2"

    - name: 输出test3
      shell: echo "test3"

      notify:
        - echo handlers3

# 

2.2 执行结果

# ansible-playbook  handlers_flush_handlers_sample.yaml -v
Using /etc/ansible/ansible.cfg as config file

PLAY [127.0.0.1] ***************************************************************************************************************************************************************************

TASK [Gathering Facts] *********************************************************************************************************************************************************************
ok: [127.0.0.1]

TASK [输出 test1] ****************************************************************************************************************************************************************************
changed: [127.0.0.1] => {"changed": true, "cmd": "echo "test5"", "delta": "0:00:00.003382", "end": "2019-05-25 02:43:14.970005", "rc": 0, "start": "2019-05-25 02:43:14.966623", "stderr": "", "stderr_lines": [], "stdout": "test5", "stdout_lines": ["test5"]}

TASK [输出 test1] ****************************************************************************************************************************************************************************
changed: [127.0.0.1] => {"changed": true, "cmd": "echo "test1"", "delta": "0:00:00.002981", "end": "2019-05-25 02:43:15.211020", "rc": 0, "start": "2019-05-25 02:43:15.208039", "stderr": "", "stderr_lines": [], "stdout": "test1", "stdout_lines": ["test1"]}

RUNNING HANDLER [echo handlers] ************************************************************************************************************************************************************
changed: [127.0.0.1] => {"changed": true, "cmd": "echo "handlers"", "delta": "0:00:00.003637", "end": "2019-05-25 02:43:15.451007", "rc": 0, "start": "2019-05-25 02:43:15.447370", "stderr": "", "stderr_lines": [], "stdout": "handlers", "stdout_lines": ["handlers"]}

RUNNING HANDLER [echo handlers2] ***********************************************************************************************************************************************************
changed: [127.0.0.1] => {"changed": true, "cmd": "echo "handlers2"", "delta": "0:00:00.003179", "end": "2019-05-25 02:43:15.687635", "rc": 0, "start": "2019-05-25 02:43:15.684456", "stderr": "", "stderr_lines": [], "stdout": "handlers2", "stdout_lines": ["handlers2"]}

TASK [输出test2] *****************************************************************************************************************************************************************************
changed: [127.0.0.1] => {"changed": true, "cmd": "echo "test2"", "delta": "0:00:00.003304", "end": "2019-05-25 02:43:15.923582", "rc": 0, "start": "2019-05-25 02:43:15.920278", "stderr": "", "stderr_lines": [], "stdout": "test2", "stdout_lines": ["test2"]}

TASK [输出test3] *****************************************************************************************************************************************************************************
changed: [127.0.0.1] => {"changed": true, "cmd": "echo "test3"", "delta": "0:00:00.002604", "end": "2019-05-25 02:43:16.160683", "rc": 0, "start": "2019-05-25 02:43:16.158079", "stderr": "", "stderr_lines": [], "stdout": "test3", "stdout_lines": ["test3"]}

RUNNING HANDLER [echo handlers3] ***********************************************************************************************************************************************************
changed: [127.0.0.1] => {"changed": true, "cmd": "echo "handlers3"", "delta": "0:00:00.004195", "end": "2019-05-25 02:43:16.405100", "rc": 0, "start": "2019-05-25 02:43:16.400905", "stderr": "", "stderr_lines": [], "stdout": "handlers3", "stdout_lines": ["handlers3"]}

PLAY RECAP *********************************************************************************************************************************************************************************
127.0.0.1                  : ok=8    changed=7    unreachable=0    failed=0   

# 

 

3. ansible force-handlers

注意要点:  force-handlers主要针对即使playbook执行失败,也要执行代码块成功了的handlers,如果代码块本身执行失败,那么它所对应的handlers应当不会被执行

例如如下playbook应当执行的顺序为: test5 ——> test1(报错)——>handler2

3.1 playbook

# cat handlers_force.yaml 
---
- hosts: 127.0.0.1
  handlers:
    - name: echo handlers
      shell: echo "handlers"
   
    - name: echo handlers2
      shell: echo "handlers2"

    - name: echo handlers3
      shell: echo "handlers3"

  tasks:
    - name: 输出 test1
      shell: echo "test5"

      notify:
        - echo handlers2

    - name: 输出 test1
      shell: echo1 "test1"

      notify:
        - echo handlers

    - name: 输出test2
      shell: echo "test2"

    - name: 输出test3
      shell: echo "test3"

      notify:
        - echo handlers3

# 

3.2 执行结果

# ansible-playbook -v handlers_force.yaml --force-handlers
Using /etc/ansible/ansible.cfg as config file

PLAY [127.0.0.1] ***************************************************************************************************************************************************************************

TASK [Gathering Facts] *********************************************************************************************************************************************************************
ok: [127.0.0.1]

TASK [输出 test1] ****************************************************************************************************************************************************************************
changed: [127.0.0.1] => {"changed": true, "cmd": "echo "test5"", "delta": "0:00:00.002600", "end": "2019-05-25 02:59:14.348115", "rc": 0, "start": "2019-05-25 02:59:14.345515", "stderr": "", "stderr_lines": [], "stdout": "test5", "stdout_lines": ["test5"]}

TASK [输出 test1] ****************************************************************************************************************************************************************************
fatal: [127.0.0.1]: FAILED! => {"changed": true, "cmd": "echo1 "test1"", "delta": "0:00:00.002676", "end": "2019-05-25 02:59:14.584181", "msg": "non-zero return code", "rc": 127, "start": "2019-05-25 02:59:14.581505", "stderr": "/bin/sh: echo1: command not found", "stderr_lines": ["/bin/sh: echo1: command not found"], "stdout": "", "stdout_lines": []}

RUNNING HANDLER [echo handlers2] ***********************************************************************************************************************************************************
changed: [127.0.0.1] => {"changed": true, "cmd": "echo "handlers2"", "delta": "0:00:00.002615", "end": "2019-05-25 02:59:14.821083", "rc": 0, "start": "2019-05-25 02:59:14.818468", "stderr": "", "stderr_lines": [], "stdout": "handlers2", "stdout_lines": ["handlers2"]}
    to retry, use: --limit @/root/ansible/playbook/handlers/handlers_force.retry

PLAY RECAP *********************************************************************************************************************************************************************************
127.0.0.1                  : ok=3    changed=2    unreachable=0    failed=1   

# 

关于ansible handlers,就暂时学习到这儿,路漫漫其修远兮,吾将上下而求索

欢迎转发! 请保留源地址: https://www.cnblogs.com/NoneID
原文地址:https://www.cnblogs.com/NoneID/p/10922476.html