2017-02-12 85 views
5

目標:Ansible - 創建多個文件夾,如果不存在

  • 創建多個目錄,如果它們不存在。
  • 不要改變現有的文件夾

目前劇本的權限:

- name: stat directories if they exist 
    stat: 
    path: "{{ item }}" 
    with_items: 
    - /data/directory 
    - /data/another 
    register: myvar 

- debug: var=myvar.results 

- name: create directory if they don't exist 
    file: 
    path: "{{ item.invocation.module_args.path }}" 
    state: directory 
    owner: root 
    group: root 
    mode: 0775 
    with_items: "{{ stat.results }}" 
    # when: myvar.results.stat.exists == false 

when說法是錯誤的。

我看了提供的例子; http://docs.ansible.com/ansible/stat_module.html。但是這隻適用於單個文件夾。

回答

6

Ansible - 創建多個文件夾不改變以前存在的權限。

適合我。希望這對你有用,並試試。

--- 
- name: "Creating multiple by checking folders" 
    hosts: your_host_name 
    tasks: 
    - block: 
    - name: "Checking folders" 
     stat: 
     path: "{{item}}" 
     register: folder_stats 
     with_items: 
     - ["/var/www/f1","/var/www/f2","/var/www/f3","/var/www/f4"] 
    - name: "Creating multiple folders without disturbing previous permissions" 
     file: 
     path: "{{item.item}}" 
     state: directory 
     mode: 0755 
     group: root 
     owner: root 
     when: item.stat.exists == false 
     with_items: 
     - "{{folder_stats.results}}" 
... 
+1

這將有望爲所有人工作 –

3

使用Ansible模塊,你不需要或者檢查是否存在的東西沒有,你剛纔描述的理想狀態,所以:

- name: create directory if they don't exist 
    file: 
    path: "{{ item }}" 
    state: directory 
    owner: root 
    group: root 
    mode: 0775 
    with_items: 
    - /data/directory 
    - /data/another 
+0

這將文件夾的權限更改爲root:根。如果文件夾已經存在,我不想更改用戶/組。 – Kevin

+0

然後爲每個文件夾指定所需的權限。 – techraf

+0

嗯......所以我想要的是Ansible中不可能的? – Kevin

相關問題