2015-08-17 33 views
1

尋求創建一個目錄列表來驗證存在,然後檢查它們是否正確定義,如果不更新它們。Ansible - 多個循環 - 目錄驗證

如何創建多個一起工作的循環?

# Determine if a path exists and is a directory. 
    - name: check directory existance and characteristics 
    stat: path=/path1 
    register: p1 
# both that p.stat.isdir actually exists, and also that it's set to true. 
    - debug: msg="Path exists" 
    when: p1.stat.isdir is defined 
    - debug: msg="This is a directory" 
    when: p1.stat.isdir 
    - file: path=/path1 owner='user1' group='group1' mode=0755 state=directory 
    when: p1.stat.pw_name != 'user1' or p1.stat.gr_name != 'group1' or p1.stat.mode != '0755' 

最好要檢查所有目錄是否存在和更新他們那裏再失敗,那些不這樣做的名單。 最終需要像目錄和所有權設置文件來驗證。

回答

0

你可以用下面的檢查定義一個task.yml,然後在一個劇本中執行任務和你需要運行這個任務的路徑數組。

----Task.yml 
# Determine if a path exists and is a directory. 
- name: check directory existance and characteristics 
    stat: path=/path1 
    register: p1 
# both that p.stat.isdir actually exists, and also that it's set to true. 
- debug: msg="Path exists" 
    when: p1.stat.isdir is defined 
- debug: msg="This is a directory" 
    when: p1.stat.isdir 
- file: path=/path1 owner='user1' group='group1' mode=0755 state=directory 
    when: p1.stat.pw_name != 'user1' or p1.stat.gr_name != 'group1' or p1.stat.mode != '0755' 

劇本運行任務

--- Playbook 
- name: Running Task 
    host: local 
    var: 
    - paths: ["path1" , "path2", "path3"] 
    tasks: 
    - include: task.yml path={{item}} 
     with_items: paths 
+0

這將在Ansible 2.0重新工作。目前'include'不能和'with_items'一起使用。 – udondan

+0

是的,這是真的。 –

+0

好吧,with_items和include目前是不可能的!?是否有可能通過github中的模塊更新。如果是的話哪些模塊需要更新? (增加包模塊到庫裏我知道是Ansible 2.0,雖然沒有驗證工作正常) –

1

最好要檢查所有目錄是否存在和更新他們那裏再失敗,那些不這樣做的名單。

如果是這樣的話,那麼你就是在過度設計事物。您只需調用file任務,併爲每個目錄使用適當的參數。由於Ansible是idempotent它會檢查你的參數,只改變那些需要改變的參數。所以,這樣的事情應該是你所需要的:

- name: directories 
    file: path={{ item.p }} 
     state=directory 
     owner={{ item.o }} 
     group={{ item.g }} 
     mode=0755 
    with_items: 
    - { p: '/deploy', o: 'deploy_user', g: 'deploy_group' } 
    - { p: '/deploy/scripts', o: 'deploy_user', g: 'deploy_group' } 
    - { p: '/deploy/lib', o: 'deploy_user', g: 'deploy_group' }  

第一次此任務運行時也將創建目錄/deploy/deploy/scripts,並且/deploy/lib與指定的所有權&組。第二次運行此任務時,它不應該執行任何操作,因爲這些路徑已經存在,且指定的所有權組爲&。 Ansible會很好地格式化輸出,特別是如果它在啓用了顏色的shell中運行,那麼只需讀取這一個任務的輸出即可輕鬆確定哪些內容已更改,哪些內容未更改。

編輯:如果你想,如果不存在,那麼目錄測試&顯示錯誤,一個簡單的兩步法也應努力:

vars: 
    my_dirs: 
    - { p: '/deploy', o: 'deploy_user', g: 'deploy_group' } 
    - { p: '/deploy/scripts', o: 'deploy_user', g: 'deploy_group' } 
    - { p: '/deploy/lib', o: 'deploy_user', g: 'deploy_group' } 

tasks: 
    - name: Check directories 
    stat: path={{ item.p }} 
    register: st 
    with_items: my_dirs 

    - name: Complain 
    debug: "Path {{ item.p }} does not exist or isn't set properly" 
    when: p1.stat.isdir is not defined or not p1.stat.isdir or p1.stat.pw_name != item.o or ... 
    with_items: my_dirs 

    - name: create directories 
    file: path={{ item.p }} 
      state=directory 
      owner={{ item.o }} 
      group={{ item.g }} 
      mode=0755 
    with_items: my_dirs 
+0

謝謝我會檢查這兩個出來,它可能會是2步的方法更喜歡。 –