談談 ansible handlers

百度了一下handlers force_handlers的用法,幾乎不少文章都是這樣描述的: shell

Handlers只有在其所在的任務被執行時,纔會被運行;若是一個任務中定義了notify調用Handlers,可是因爲條件判斷等緣由,該任務未被執行,那麼Handlers一樣不會被執行。學習

Handlers只會在每個play的末尾運行一次;若是想在一個playbook中間運行Handlers,則須要使用meta模塊來實現。例如: -meta: flush_handlers.spa

若是一個play在運行到調用Handlers的語句以前失敗了,那麼這個Handlers將不會被執行。咱們可使用meta模塊的--force-handlers選項來強制執行Handlers,即便Handlers所在的play中途運行失敗也能執行。code

 

因而乎就實際操做使用了一下handlers,作出以下總結:blog

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 執行結果cmd

# 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 , it

2.  ansible使用flush_handlers 在中間執行

注意要點: 1. 在使用flush_handlers以後,ansible會就此從後往上執行flush 以前的 handlersclass

例如以下playbook應該執行輸出順序爲: test5 ——> test1 ——> handlers ——> handlers2 ——> test2 ——> test3 ——> handler3test

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,就暫時學習到這兒,路漫漫其修遠兮,吾將上下而求索

相關文章
相關標籤/搜索