区分Ansible中的Ansible错误和任务错误?
Distinguish between Ansible error and task error in Ansible?
我正在尝试 运行 ansible 剧本中的命令任务,如果失败,我想 运行 救援部分。如果命令失败(退出状态!= 0)并且 stderr 输出不包含某个字符串,我希望失败状态是这样的
- block:
- name: some command
command: "command {{ my_var }}"
register: command_status
failed_when: command_status.rc != 0 and "Some text" not in command_status.stderr
changed_when: false
rescue:
- name: error occurred. Rolling back
command: rescue-command
上面的问题是,如果上面没有定义变量my_var
,命令会失败(出现类似the field 'args' has an invalid value, which appears to include a variable that is undefined
的消息,救援部分会运行,这是不需要。是否有任何方法可以避免 Ansible 考虑 Ansible 特定错误与来自游戏中实际命令的错误?
我有点希望 rc
return 值仅特定于命令,但事实并非如此。
我是运行ning Ansible 2.3
不直接区分错误来源,但更多的是针对您的用例进行破解:
您可以在救援部分添加对 command_status.cmd
的检查。如果模块失败,变量command_status.cmd
不会被定义,所以救援任务不会运行:
- block:
- name: some command
command: "command {{ my_var }}"
register: command_status
failed_when: command_status.rc != 0 and "Some text" not in command_status.stderr
changed_when: false
rescue:
- name: error occurred. Rolling back
command: rescue-command
when: command_status.cmd is not defined
如果出现意外错误,我会用显式 fail
扩展 @techraf 的答案,否则 Ansible 将继续执行任务,就好像没有发生任何不好的事情一样。
- block:
- name: some command
command: "command {{ my_var }}"
register: command_status
failed_when: command_status.rc != 0 and "Some text" not in command_status.stderr
changed_when: false
rescue:
- fail:
msg: Unexpected error occurred "{{ command_status.msg }}"
when: command_status.failed_when_result is undefined
- name: Expected error occurred. Rolling back
command: rescue-command
如果出现意外错误,主机将失败(因为 failed_when_result
已定义,如果 failed_when
的任务已完成;如果错误是预期的(命令因 failed_when
语句而失败,将应用救援命令。
我正在尝试 运行 ansible 剧本中的命令任务,如果失败,我想 运行 救援部分。如果命令失败(退出状态!= 0)并且 stderr 输出不包含某个字符串,我希望失败状态是这样的
- block:
- name: some command
command: "command {{ my_var }}"
register: command_status
failed_when: command_status.rc != 0 and "Some text" not in command_status.stderr
changed_when: false
rescue:
- name: error occurred. Rolling back
command: rescue-command
上面的问题是,如果上面没有定义变量my_var
,命令会失败(出现类似the field 'args' has an invalid value, which appears to include a variable that is undefined
的消息,救援部分会运行,这是不需要。是否有任何方法可以避免 Ansible 考虑 Ansible 特定错误与来自游戏中实际命令的错误?
我有点希望 rc
return 值仅特定于命令,但事实并非如此。
我是运行ning Ansible 2.3
不直接区分错误来源,但更多的是针对您的用例进行破解:
您可以在救援部分添加对 command_status.cmd
的检查。如果模块失败,变量command_status.cmd
不会被定义,所以救援任务不会运行:
- block:
- name: some command
command: "command {{ my_var }}"
register: command_status
failed_when: command_status.rc != 0 and "Some text" not in command_status.stderr
changed_when: false
rescue:
- name: error occurred. Rolling back
command: rescue-command
when: command_status.cmd is not defined
如果出现意外错误,我会用显式 fail
扩展 @techraf 的答案,否则 Ansible 将继续执行任务,就好像没有发生任何不好的事情一样。
- block:
- name: some command
command: "command {{ my_var }}"
register: command_status
failed_when: command_status.rc != 0 and "Some text" not in command_status.stderr
changed_when: false
rescue:
- fail:
msg: Unexpected error occurred "{{ command_status.msg }}"
when: command_status.failed_when_result is undefined
- name: Expected error occurred. Rolling back
command: rescue-command
如果出现意外错误,主机将失败(因为 failed_when_result
已定义,如果 failed_when
的任务已完成;如果错误是预期的(命令因 failed_when
语句而失败,将应用救援命令。