我已经搜索了如何在不失败的情况下停止执行Ansible任务的示例.
一个简单的例子:
- name: check file
stat: path={{some_path}}
register: check_file_result
- name: if file exists, stop
**fail**: msg="file exists, stopping"
when: check_file_result.stat.exists
Run Code Online (Sandbox Code Playgroud)
这有效,因为它停止执行,但失败了大量的红色文本可能会阻止整个playbook运行进一步的任务.有没有办法停止执行,好像所有的都以"OK"结束?
注意:解决方法是简单地添加"when:check_file_result.stat.exists == false",但这种情况非常糟糕.
jab*_*lab 18
如果有多个任务你不想执行,check_file_result.stat.exists
那么我可能会将所有这些任务封装在一个单独的文件中,然后有条件地封装include
该文件,例如
- name: check file
stat: path={{ some_path }}
register: check_file_result
- include: file_ops.yml
when: check_file_result.stat.exists
Run Code Online (Sandbox Code Playgroud)
示例目录结构:
|- roles/
|- foo
|- tasks
|- main.yml
|- file_ops.yml
|- vars
|- main.yml
Run Code Online (Sandbox Code Playgroud)
使用这种方法是DRY :-)
归档时间: |
|
查看次数: |
6136 次 |
最近记录: |