2014-08-31 64 views
1

我想做這樣的事情。在目錄跳過的情況下,否則會被創建。Ansible判斷文件目錄

例如:

#!/bin/bash 

for $i in 'a.test.com a.test.com c.test.com' 
do 
    if [ ! -e $i ]: 
    then 
     mkdir $i 
    fi 
done 

如何使用ansible-劇本實現上述代碼。

感謝

回答

1

loop使用file module貌似簡單的目錄創建。

- name: make sure subdomain directories exist 
    file: path=/opt/{{item}} state=directory recursive=yes 
    with_items: 
    - a.test.com 
    - b.test.com 
    - c.test.com 
+0

感謝您的回答。但是,我想使用文件模塊。文件模塊始終運行。是否存在 – song 2014-09-02 12:04:42

+0

@ user1487402你的意思是「我不想使用文件模塊」?這個評論或問題沒有任何意義。 – tedder42 2014-09-02 16:23:16

0

如果該目錄已創建或跳過。

- name: test log dir 
    shell: "test -e /data/{{item[0]}}/{{item[1]}} -o -h /data/{{item[0]}}/{{item[1]}}" 
    ignore_errors: True 
    with_nested: 
    - ['logs', 'html'] 
    - ['a.test.com', 'b.test.com'] 
    register: dir_stats 
    tags: check_log 

- name: create log dir 
    file: path=/data/{{item.item[0]}}/{{item.item[1]}} state=directory owner=apache group=apache recursive=yes 
    with_items: dir_stats.results 
    when: item.rc != 0 
    tags: check_log 
相關問題