简化ansible文件检查?

Chr*_*ach 3 ansible ansible-playbook

在ansible中,我做了很多这样的事情:

- name: Check if [someFile] exists on host
  stat: path=[someFile]
  register: someFile

- fail: msg="[someFile] not found"
  when: someFile.stat.exists == False
Run Code Online (Sandbox Code Playgroud)

我希望能够更简洁地表达它。像这样的东西:

- fail_on_missing_file: 
  path: [someFile]
Run Code Online (Sandbox Code Playgroud)

实现这一目标的最佳方法是什么?

jar*_*arv 5

你可以failed_when用来做你想做的事:

- stat: path=/path/to/some/file
  register: someFile
  failed_when: not someFile.stat.exists
Run Code Online (Sandbox Code Playgroud)