我有这样的剧本:
---
- hosts: all
tasks:
- name: Create 404 link
file:
dest: /var/www/html/{{ item }}/images/index.html
src: /var/www/html/404.html
state: link
with_items:
- a
- b
- c
- d
- e
# when: ???
...
现在只有/var/www/html/{{ item }}/images
目录为空时才能运行任务。如何完成此操作?我已尝试使用find
模块和register
输出,但未能成功提取其中没有文件的项目(目录)。
答案 0 :(得分:0)
找到了解决方案。
---
- hosts: all
tasks:
- name: check contents of dir
find:
paths: "/var/www/html/{{ item }}/images/"
patterns: "[A-Za-z0-9_-]+"
use_regex: True
file_type: any
recurse: yes
with_items:
- a
- b
- c
- d
- e
register: find_output
- name: Create 404 link
file:
dest: /var/www/html/{{ item.item }}/images/index.html
src: /var/www/html/404.html
state: link
with_items: "{{ find_output.results }}"
when: item.matched == 0
...