Ansible |如果目录为空,则创建符号链接
我有一本这样的剧本:Ansible |如果目录为空,则创建符号链接,ansible,Ansible,我有一本这样的剧本: --- - 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
---
- 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
提取输出,但未能成功提取其中没有文件的项(目录)。找到了解决方案
---
- 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
...