Ansible——處理任務失敗


忽略任務失敗

Ansible 默認會檢查命令和模塊的返回狀態,並進行相應的錯誤處理,默認是遇到錯誤就中斷 playbook 的執行,這些默認行為都是可以改變的,可以通過 ignore_errors 忽略返回狀態碼

[root@localhost project]# !vim
vim ceshi1.yml 
---
- hosts: 192.168.190.134
  tasks:
    - command: ls /sdadadsdad            這個文件是不存在的,如果正常執行playbook會出錯並不會執行下面的task
      ignore_errors: yes                 忽略錯誤
    - command: echo 'hello'               繼續執行此task

TASK [command] *****************************************************************************************
fatal: [192.168.190.134]: FAILED! => {"changed": true, "cmd": ["ls", "/sdadadsdad"], "delta": "0:00:00.003481", "end": "2020-09-08 09:23:22.735623", "msg": "non-zero return code", "rc": 2, "start": "2020-09-08 09:23:22.732142", "stderr": "ls: cannot access '/sdadadsdad': No such file or directory", "stderr_lines": ["ls: cannot access '/sdadadsdad': No such file or directory"], "stdout": "", "stdout_lines": []}
...ignoring

TASK [command] *****************************************************************************************
changed: [192.168.190.134]

強制執行任務

通常任務失敗,playbook 會終止,那么收到 play 中之前任務通知的處理程序將不會運行,如果要運行,需要使用關鍵字:force_handlers:yes

[root@localhost project]# !vim
vim ceshi1.yml 
---
- hosts: 192.168.190.134
  force_handlers: yes            強制執行handlers
  tasks:
    - name: show the date
      template:
        src: template/date.j2
        dest: /tmp
      notify:
          restart apache
    - name: error task
      command: ls /sdsdsds
  handlers:
    - name: restart apache
      service:
        name: httpd
        state: restarted


TASK [error task] **************************************************************************************
fatal: [192.168.190.134]: FAILED! => {"changed": true, "cmd": ["ls", "/sdsdsds"], "delta": "0:00:00.003698", "end": "2020-09-08 09:36:44.778096", "msg": "non-zero return code", "rc": 2, "start": "2020-09-08 09:36:44.774398", "stderr": "ls: cannot access '/sdsdsds': No such file or directory", "stderr_lines": ["ls: cannot access '/sdsdsds': No such file or directory"], "stdout": "", "stdout_lines": []}

RUNNING HANDLER [restart apache] ***********************************************************************  handlers成功執行。
changed: [192.168.190.134]

PLAY RECAP *********************************************************************************************
192.168.190.134            : ok=3    changed=2    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   

指定任務失敗條件

方法一: 使用failed_when關鍵字

vim ceshi1.yml 
---
- hosts: 192.168.190.134
  tasks:
    - name: test
      script: files/test.sh            腳本在執行時,就算腳本里有錯誤,最后也是執行ok或者changed
      register: result
      failed_when: "'No such file or directory' in result.stdout"  加上報錯的條件。


成功報錯。
TASK [test] ********************************************************************************************  
fatal: [192.168.190.134]: FAILED! => {"changed": true, "failed_when_result": true, "rc": 0, "stderr": "Shared connection to 192.168.190.134 closed.\r\n", "stderr_lines": ["Shared connection to 192.168.190.134 closed."], "stdout": "ls: cannot access 'sdsdsdsdsdsds': No such file or directory\r\nanaconda-ks.cfg\r\n", "stdout_lines": ["ls: cannot access 'sdsdsdsdsdsds': No such file or directory", "anaconda-ks.cfg"]}

方法二:使用fail模塊進行報錯條件

[root@localhost project]# !vim
vim ceshi1.yml 
---
- hosts: 192.168.190.134
  tasks:
    - name: test
      script: files/test.sh
      register: result
      
    - name: fail control
      fail:
        msg: "you are a fool"       明確錯誤提示。
      when:
        "'No such file or directory' in result.stdout"      


TASK [fail control] ************************************************************************************
fatal: [192.168.190.134]: FAILED! => {"changed": false, "msg": "you are a fool"}

指定任務何時報告"Changed"結果

利用changed_when關鍵字來確定是否報告changed結果

[root@localhost project]# vim ceshi1.yml 
---
- hosts: 192.168.190.134
  tasks:
    - name: useradd
      user:
        name: test1
        state: present
      changed_when: True       無論是否成功執行。報告返回結果都為changed.如果為False,在task沒有錯誤的情況下,始終返回ok。

ansible 塊和錯誤處理

三種關鍵字:

  • block:定義要運行的主要任務
  • rescue:定義要在 block 子句中定義的任務失敗時運行的任務
  • always:定義始終獨立運行的任務
---
- hosts: 192.168.190.134
  vars:
    file_name: test1
  tasks:
    - name: test block
      block:
        - name: block1
          command: ls /tmp
        - name: block2
          command: ls /tmp/{{ file_name }}               若文件不存在,執行rescue
      rescue:
        - name: touch the {{ file_name }}                 創建此文件
          file:
            name: "{{ file_name }}"
            state: touch
      always:                                            無論block成功與否,都執行always
        - name: print
          command: echo "\(@^0^@)/"
          register: result
        - debug:
            msg: " {{ result.stdout }} "                              


免責聲明!

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



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