ansible playbook對錯誤的處理


 

Topics

  • Playbooks 中的錯誤處理
    • 忽略錯誤的命令
    • 控制對失敗的定義
    • 覆寫更改結果

Ansible 通常默認會確保檢測模塊和命令的返回碼並且會快速失敗 – 專注於一個錯誤除非你另作打算.

有時一條命令會返回 0 但那不是報錯.有時命令不會總是報告它 ‘改變’ 了遠程系統.本章節描述了 如何將 Ansible 處理輸出結果和錯誤處理的默認行為改變成你想要的.

忽略錯誤的命令

New in version 0.6.

通常情況下, 當出現失敗時 Ansible 會停止在宿主機上執行.有時候,你會想要繼續執行下去.為此 你需要像這樣編寫任務:

- name: this will not be counted as a failure
  command: /bin/false
  ignore_errors: yes

注意上面的系統僅僅處理那個特定的任務,所以當你在使用一個未定義的變量時, Ansible 仍然會報 錯,需要使用者進行處理.

控制對失敗的定義

New in version 1.4.

假設一條命令的錯誤碼毫無意義只有它的輸出結果能告訴你什么出了問題,比如說字符串 “FAILED” 出 現在輸出結果中.

在 Ansible 1.4及之后的版本中提供了如下的方式來指定這樣的特殊行為:

- name: this command prints FAILED when it fails
  command: /usr/bin/example-command -x -y -z
  register: command_result
  failed_when: "'FAILED' in command_result.stderr"

在 Ansible 1.4 之前的版本能通過如下方式完成:

- name: this command prints FAILED when it fails
  command: /usr/bin/example-command -x -y -z
  register: command_result
  ignore_errors: True

- name: fail the play if the previous command did not succeed
  fail: msg="the command failed"
  when: "'FAILED' in command_result.stderr"

覆寫更改結果

New in version 1.3.

When a shell/command or other module runs it will typically report “changed” status based on whether it thinks it affected machine state. 當一個 shell或命令或其他模塊運行時,它們往往都會在它們認為其影響機器狀態時報告 “changed” 狀態

有時你可以通過返回碼或是輸出結果來知道它們其實並沒有做出任何更改.你希望覆寫結果的

“changed” 狀態使它不會出現在輸出的報告或不會觸發其他處理程序:

tasks:

  - shell: /usr/bin/billybass --mode="take me to the river"
    register: bass_result
    changed_when: "bass_result.rc != 2"

  # this will never report 'changed' status
  - shell: wall 'beep'
    changed_when: False


參考鏈接: http://www.ansible.com.cn/docs/playbooks_error_handling.html
 
            


免責聲明!

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



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