当在ansible中找不到文件时,有没有方法跳过任务/播放



下面我发布了一个我目前拥有的示例,但它并没有解决问题。

ignore_errors仍然输出播放中的错误,但不会阻止任务完成。有没有办法跳过这场戏,然后继续下一场?

- name: replace static with delta file
copy:
src: "/home/docs/delta.{{ inventory_hostname }}"
dest: "/usr/share/static"
backup: yes
ignore_errors: yes

如果源文件不存在,可以使用fileglob来阻止任务运行:

- name: replace static with delta file
copy:
src: "{{ item }}"
dest: "/usr/share/static"
backup: yes
loop: "{{ query('fileglob', '/home/docs/delta.%s' % inventory_hostname) }}"

此fileglob将返回0或1结果,因此任务将如果没有匹配的文件,则跳过。

所以我想到的第一件事是创建一个任务,该任务将检查目录是否存在:

- name: Playbook name
hosts: all
tasks:
- name: Task name
stat:
path: [path to the file or directory you want to check]
register: register_name

如果目录存在,则第二项任务是:

- name: Task name
debug:
msg: "The file or directory exists"
when: register_name.stat.exists

最新更新