搜索
您的当前位置:首页正文

【Ansible】ansible 任务失败控制

来源:二三娱乐

 任务失败控制

Ansible 通常默认会确保检测模块和命令的返回码并且会快速失败 – 专注于一个错误除非你另作打算.

有时一条命令会返回 0 但那不是报错.有时命令不会总是报告它 ‘改变’ 了远程系统.本章节描述了 如何将 Ansible 处理输出结果和错误处理的默认行为改变成你想要的.

忽略错误的命令

通常情况下, 当出现失败时 Ansible 会停止在宿主机上执行.有时候,你会想要继续执行下去.为此 你需要像这样编写任务:

- name: cat no exist file

command: cat /root/noexist

ignore_errors: yes

- name: cat file info

shell: cat /root/fileinfo

控制对失败的定义

假设一条命令的错误码毫无意义只有它的输出结果能告诉你什么出了问题,比如说字符串 “FAILED” 出 现在输出结果中.

在 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"

覆写更改结果

当一个 shell或命令或其他模块运行时,它们往往都会在它们认为其影响机器状态时报告 “changed” 状态

有时你可以通过返回码或是输出结果来知道它们其实并没有做出任何更改.你希望覆写结果的

“changed” 状态使它不会出现在输出的报告或不会触发其他处理程序:

tasks:

- shell: /usr/bin/ps -ef | grep zabbix

register: zabbix_result

changed_when: "zabbix_result.rc != 2"

文献参考链接:


原文地址

Top