Ansible-调用rescue后跳过块中的其他任务
我有意在块中的第一个任务(Ansible-调用rescue后跳过块中的其他任务,ansible,Ansible,我有意在块中的第一个任务(create file-1)中创建一个错误来处理错误。rescue块触发正常并执行任务,但是块中的第二个任务(创建文件-2)根本没有执行。为什么?如何确保执行块中的所有任务 我的剧本。请注意注释行 您的剧本运行正常。rescue块是异常处理的一种形式。它允许您捕获并响应一个错误,否则会导致您的playbook退出,但块中的所有其他任务都将被跳过。Playbook将在当前块之后继续执行 如果要确保执行第二个任务,可以将它们放在两个单独的块中: - block: -
create file-1
)中创建一个错误来处理错误。rescue块触发正常并执行任务,但是块中的第二个任务(创建文件-2
)根本没有执行。为什么?如何确保执行块中的所有任务 我的剧本。请注意注释行
您的剧本运行正常。
rescue
块是异常处理的一种形式。它允许您捕获并响应一个错误,否则会导致您的playbook退出,但块中的所有其他任务都将被跳过。Playbook将在当前块之后继续执行
如果要确保执行第二个任务,可以将它们放在两个单独的块中:
- block:
- name: create file-1
command: touch /path/to/file-1.txt
rescue:
- name: handle error
debug:
msg: do some error handling here
- block:
- name: create file-2
command: touch /path/to/file-2.txt
register: file1
rescue:
- name: handle error
debug:
msg: do some error handling here
或者在create file-
块上设置ignore\u errors:true
,并在执行这两个块后显式检查错误:
- name: create file-1
command: touch /path/to/file-1.txt
register: file1
ignore_errors: true
- name: handle errors creating file-1
debug:
msg: do something here
when: file1 is failed
- name: create file-2
command: touch /path/to/file-2.txt
register: file2
ignore_errors: true
- name: handle errors creating file-1
debug:
msg: do something here
when: file2 is failed
请不要张贴文字图片。张贴文本本身,格式正确。这使您的问题更容易被发现(例如通过搜索引擎),并使使用屏幕阅读器或其他辅助设备的人可以访问您的问题。