任務(wù)失敗控制
Ansible 通常默認(rèn)會(huì)確保檢測(cè)模塊和命令的返回碼并且會(huì)快速失敗 – 專注于一個(gè)錯(cuò)誤除非你另作打算.
有時(shí)一條命令會(huì)返回 0 但那不是報(bào)錯(cuò).有時(shí)命令不會(huì)總是報(bào)告它‘改變’ 了遠(yuǎn)程系統(tǒng).本章節(jié)描述了 如何將 Ansible 處理輸出結(jié)果和錯(cuò)誤處理的默認(rèn)行為改變成你想要的.
忽略錯(cuò)誤的命令
通常情況下, 當(dāng)出現(xiàn)失敗時(shí) Ansible 會(huì)停止在宿主機(jī)上執(zhí)行.有時(shí)候,你會(huì)想要繼續(xù)執(zhí)行下去.為此你需要像這樣編寫任務(wù): - - name: cat no exist file
- command: cat /root/noexist
- ignore_errors: yes
復(fù)制代碼
控制對(duì)失敗的定義
假設(shè)一條命令的錯(cuò)誤碼毫無意義只有它的輸出結(jié)果能告訴你什么出了問題,比如說字符串“FAILED”出 現(xiàn)在輸出結(jié)果中.
在 Ansible 1.4及之后的版本中提供了如下的方式來指定這樣的特殊行為: - - name: this command prints FAILED when it fails
- command: /usr/bin/ls -x -y -z
- register: command_result
- failed_when: "'FAILED' in command_result.stderr"
復(fù)制代碼在 Ansible 1.4 之前的版本能通過如下方式完成: - - name: this command prints FAILED when it fails
- command: /usr/bin/ls -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"
復(fù)制代碼
覆寫更改結(jié)果 當(dāng)一個(gè) shell或命令或其他模塊運(yùn)行時(shí),它們往往都會(huì)在它們認(rèn)為其影響機(jī)器狀態(tài)時(shí)報(bào)告“changed”狀態(tài)
有時(shí)你可以通過返回碼或是輸出結(jié)果來知道它們其實(shí)并沒有做出任何更改.你希望覆寫結(jié)果的 “changed”狀態(tài)使它不會(huì)出現(xiàn)在輸出的報(bào)告或不會(huì)觸發(fā)其他處理程序:
- tasks:
- - shell: /usr/bin/ps -ef | grep zabbix
- register: zabbix_result
- changed_when: "zabbix_result.rc != 2"
復(fù)制代碼
文獻(xiàn)參考鏈接:
|