Error handling ANSIBLOCKS:在“rescue”块中重新引发错误
我是Ansible的新手,所以我可能没有正确地处理这个问题,但我正试图找到一种方法,在错误被Error handling ANSIBLOCKS:在“rescue”块中重新引发错误,error-handling,ansible,Error Handling,Ansible,我是Ansible的新手,所以我可能没有正确地处理这个问题,但我正试图找到一种方法,在错误被rescued阻塞后重新引发错误 这样做的目的是能够在终止playbook之前将失败的任务记录到API中。我会使用始终部分,但是没有填充ansible\u failed\u任务和ansible\u failed\u结果变量 我相当确信我的方法是错误的,那么一个有经验的Ansible开发人员如何处理这个问题呢?谢谢 如果我正确理解了您的意图,这是一个基本的(非功能性的)说明,说明了我将如何管理它。关键是在按
rescue
d阻塞后重新引发错误
这样做的目的是能够在终止playbook之前将失败的任务记录到API中。我会使用始终
部分,但是没有填充ansible\u failed\u任务
和ansible\u failed\u结果
变量
我相当确信我的方法是错误的,那么一个有经验的Ansible开发人员如何处理这个问题呢?谢谢 如果我正确理解了您的意图,这是一个基本的(非功能性的)说明,说明了我将如何管理它。关键是在按照您希望的方式处理错误后,在救援阶段使用结束剧本
- name: handle error nicely in my block
block:
- name: This is my task that can fail
debug:
msg: "I'm a a task that can fail"
register: some_var
rescue:
- name: Do whatever you need to log the failure
debug:
msg: "I'm a log task playing around with some_var: {{ some_var }}"
- name: fail the playbook as the task was not successful
fail:
msg: "The task was not successful. Aborting"
此场景是否符合您的期望?来自ansible(见最后一节)
版本2.1中的新功能
Ansible为区块救援部分的任务提供了两个变量:
- ansible\u任务失败 返回“失败”并触发救援的任务。例如,要获取名称>请使用ansible_failed_task.name
- ansible\u失败\u结果 触发救援的失败任务的捕获返回结果。这相当于在register关键字中使用了这个var
- name: expample
hosts: localhost
become: no
gather_facts: no
tasks:
- block:
- debug:
msg: some random task
- name: a failing task
fail:
msg: "i failed "
rescue:
- debug:
msg: the failed task in the block is {{ ansible_failed_task }}. the result of the task is {{ansible_failed_result}}
谢谢@Zeitounator!是的,这是有道理的。但是,是否有可能因原始错误而失败?我想我是在以编程的方式考虑这个问题——如果我想在Python中这样做,我会捕获异常,然后在完成日志记录后再次引发它。我想我只是缺少Ansible习语的知识?只要探索一下
一些包含你需要的信息并写出你喜欢的东西就行了。请注意,原始故障将始终出现在输出中。Ansible在rescue块中提供以下变量以帮助完成此操作:Ansible_failed_task(要获取名称,请使用Ansible_failed_task.name)Ansible_failed_result