?任務失敗控制
Ansible?通常默認會確保檢測模塊和命令的返回碼并且會快速失敗?–?專注于一個錯誤除非你另作打算.
有時一條命令會返回?0?但那不是報錯.有時命令不會總是報告它?‘改變’?了遠程系統(tǒng).本章節(jié)描述了?如何將?Ansible?處理輸出結(jié)果和錯誤處理的默認行為改變成你想要的.
忽略錯誤的命令
通常情況下,?當出現(xiàn)失敗時?Ansible?會停止在宿主機上執(zhí)行.有時候,你會想要繼續(xù)執(zhí)行下去.為此?你需要像這樣編寫任務:
- name: cat no exist file
command: cat /root/noexist
ignore_errors: yes
- name: cat file info
shell: cat /root/fileinfo
控制對失敗的定義
假設一條命令的錯誤碼毫無意義只有它的輸出結(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"
在?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"
覆寫更改結(jié)果
當一個?shell或命令或其他模塊運行時,它們往往都會在它們認為其影響機器狀態(tài)時報告?“changed”?狀態(tài)
有時你可以通過返回碼或是輸出結(jié)果來知道它們其實并沒有做出任何更改.你希望覆寫結(jié)果的
“changed”?狀態(tài)使它不會出現(xiàn)在輸出的報告或不會觸發(fā)其他處理程序:
tasks:
- shell: /usr/bin/ps -ef | grep zabbix
register: zabbix_result
changed_when: "zabbix_result.rc != 2"
文獻參考鏈接:
? ?參考鏈接: http://www.ansible.com.cn/docs/playbooks_error_handling.html
原文地址
(出處: 樂維_一站式運維監(jiān)控管理平臺)