Ansible任务失败,但继续运行
我现在正在为Ansible写一大堆支票 它包括联系远程机器并执行检查 根据这次检查的结果,我决定它是否失败 这是最重要的一点:任务本身永远不会失败。它只返回一条消息。我注册结果,然后分析它。基于这一点,我决定这是否是一次失败 问题是,我想添加一个标志,允许测试继续运行而不是失败 代码如下所示:Ansible任务失败,但继续运行,ansible,Ansible,我现在正在为Ansible写一大堆支票 它包括联系远程机器并执行检查 根据这次检查的结果,我决定它是否失败 这是最重要的一点:任务本身永远不会失败。它只返回一条消息。我注册结果,然后分析它。基于这一点,我决定这是否是一次失败 问题是,我想添加一个标志,允许测试继续运行而不是失败 代码如下所示: - name: Check fail. fail: msg: "Test failed" when: "failfast_flag and <the actual check>
- name: Check fail.
fail:
msg: "Test failed"
when: "failfast_flag and <the actual check>"
-名称:检查失败。
失败:
消息:“测试失败”
当:“failfast_标志和”
问题是,如果我将failfast_标志设置为false,它将不再输出红色
在这种情况下,我希望它继续进行下一个测试,但我也希望它显示为红色,表示它是错误/失败
我如何做到这一点
编辑:谢谢你的建议,我稍后会尝试一下。我不确定我是否完全理解你的问题,但你可以使用
-block
和rescue
结构来处理故障,并在故障场景中继续剧本:
例如:
- block:
- name: Check fail.
fail:
msg: "Test failed"
when: "failfast_flag and <the actual check>"
rescue:
- name: do somthing when the task above fails
command: <do somthing>
-块:
-名称:检查失败。
失败:
消息:“测试失败”
当:“failfast_标志和”
救援:
-名称:当上述任务失败时,执行某些操作
命令:
我不确定我是否完全理解了您的问题,但您可以使用-block
和rescue
结构来处理故障,并在故障场景中继续剧本:
例如:
- block:
- name: Check fail.
fail:
msg: "Test failed"
when: "failfast_flag and <the actual check>"
rescue:
- name: do somthing when the task above fails
command: <do somthing>
-块:
-名称:检查失败。
失败:
消息:“测试失败”
当:“failfast_标志和”
救援:
-名称:当上述任务失败时,执行某些操作
命令:
这是@edrupado的回答和@Jack的评论的结合
我们的想法是将错误移到任务中,在任务中注册值,并使用一个rescue块以信息性消息失败,使用标志跳过失败
我不知道你收集数据的实际任务是什么。我使用了一个虚拟示例来检查dir/文件是否存在。你应该能够适应你的实际情况
---
- name: Dummy POC just to test feasability
hosts: localhost
gather_facts: false
tasks:
- block:
- name: Make sure /whatever dir exists
stat:
path: /whatever
register: whatever
failed_when: not whatever.stat.exists | bool
- debug:
msg: "/whatever exists: Good !"
rescue:
- fail:
msg: /whatever dir must exist.
ignore_errors: "{{ ignore_flag | default(false) | bool }}"
- block:
- name: Make sure /tmp dir exists
stat:
path: /tmp
register: tmpdir
failed_when: not tmpdir.stat.exists | bool
- debug:
msg: "/tmp exists: Good !"
rescue:
- fail:
msg: /tmp dir must exist.
ignore_errors: "{{ ignore_flag | default(false) | bool }}"
其中:
$ ansible-playbook /tmp/test.yml
PLAY [Dummy POC just to test feasability] *************************************************************************************************************************************************************************
TASK [Make sure /whatever dir exists] *****************************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"changed": false, "failed_when_result": true, "stat": {"exists": false}}
TASK [fail] *******************************************************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"changed": false, "msg": "/whatever dir must exist."}
PLAY RECAP ********************************************************************************************************************************************************************************************************
localhost : ok=0 changed=0 unreachable=0 failed=1 skipped=0 rescued=1 ignored=0
$ ansible-playbook /tmp/test.yml -e ignore_flag=true
PLAY [Dummy POC just to test feasability] *************************************************************************************************************************************************************************
TASK [Make sure /whatever dir exists] *****************************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"changed": false, "failed_when_result": true, "stat": {"exists": false}}
TASK [fail] *******************************************************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"changed": false, "msg": "/whatever dir must exist."}
...ignoring
TASK [Make sure /tmp dir exists] **********************************************************************************************************************************************************************************
ok: [localhost]
TASK [debug] ******************************************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": "/tmp exists: Good !"
}
PLAY RECAP ********************************************************************************************************************************************************************************************************
localhost : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=1 ignored=1
这是@edrupado的回答和@Jack的评论的结合 我们的想法是将错误移到任务中,在任务中注册值,并使用一个rescue块以信息性消息失败,使用标志跳过失败 我不知道你收集数据的实际任务是什么。我使用了一个虚拟示例来检查dir/文件是否存在。你应该能够适应你的实际情况
---
- name: Dummy POC just to test feasability
hosts: localhost
gather_facts: false
tasks:
- block:
- name: Make sure /whatever dir exists
stat:
path: /whatever
register: whatever
failed_when: not whatever.stat.exists | bool
- debug:
msg: "/whatever exists: Good !"
rescue:
- fail:
msg: /whatever dir must exist.
ignore_errors: "{{ ignore_flag | default(false) | bool }}"
- block:
- name: Make sure /tmp dir exists
stat:
path: /tmp
register: tmpdir
failed_when: not tmpdir.stat.exists | bool
- debug:
msg: "/tmp exists: Good !"
rescue:
- fail:
msg: /tmp dir must exist.
ignore_errors: "{{ ignore_flag | default(false) | bool }}"
其中:
$ ansible-playbook /tmp/test.yml
PLAY [Dummy POC just to test feasability] *************************************************************************************************************************************************************************
TASK [Make sure /whatever dir exists] *****************************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"changed": false, "failed_when_result": true, "stat": {"exists": false}}
TASK [fail] *******************************************************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"changed": false, "msg": "/whatever dir must exist."}
PLAY RECAP ********************************************************************************************************************************************************************************************************
localhost : ok=0 changed=0 unreachable=0 failed=1 skipped=0 rescued=1 ignored=0
$ ansible-playbook /tmp/test.yml -e ignore_flag=true
PLAY [Dummy POC just to test feasability] *************************************************************************************************************************************************************************
TASK [Make sure /whatever dir exists] *****************************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"changed": false, "failed_when_result": true, "stat": {"exists": false}}
TASK [fail] *******************************************************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"changed": false, "msg": "/whatever dir must exist."}
...ignoring
TASK [Make sure /tmp dir exists] **********************************************************************************************************************************************************************************
ok: [localhost]
TASK [debug] ******************************************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": "/tmp exists: Good !"
}
PLAY RECAP ********************************************************************************************************************************************************************************************************
localhost : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=1 ignored=1
您是否尝试将
ingore\u erros:yes
添加到任务中?是否尝试将ingore\u erros:yes
添加到任务中?