2017-05-31 86 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 
...