2017-05-31 39 views
0

我有一个这样的剧本: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 
     - d 
     - e 
     # when: ??? 
... 

现在的任务应该只运行如果/var/www/html/{{ item }}/images目录是空的。我如何完成这项工作?我试过使用find模块和register输出,但没有成功提取其中没有文件的项目(目录)。

回答

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 
...