2016-04-26 40 views
1

我有這樣的重複模式::ansible STAT然後複製with_items

- stat: path={{ home }}/.vimrc 
    register: st 
- copy: src=.vimrc dest={{ home }}/.vimrc 
    when: not st.stat.exists 

- stat: path={{ home }}/.gitconfig 
    register: st 
- copy: src=.vimrc dest={{ home }}/.gitconfig 
    when: not st.stat.exists 
... 

我如何使用with_items的大名單做呢?::

with_items: 
    - .vimrc 
    - .bashrc 
    - .profile 
    - .gitconfig 

回答

5

有時甚至不想複製文件,如果目標計算機上存在文件,即使內容不同。使用時,我使用的,因爲CP limitaion的Arbab納扎爾解決方案:然後你就可以使用這樣(沒有測試它在您的情況,但我認爲它會工作)

- stat: path="{{ home }}/{{ item }}" 
    with_items: 
    - .vimrc 
    - .bashrc 
    - .profile 
    - .gitconfig 
    register: st 


- copy: src="{{ item.item }}" dest="{{ home }}/{{ item.item }}" 
    with_items: "{{ st.results }}" 
    when: not item.stat.exists 

希望能夠幫助您

3

可以使用武力參數:

- copy: src={{ item }} dest={{ home }}/{{ item }} force=no 
    with_items: 
    - .vimrc 
    - .bashrc 
    - .profile 
    - .gitconfig 

Force = no只有在文件還不存在的情況下才寫入文件。 我認爲這正是你想要的。

+0

旁註選項:directory_mode和remote_src。請參閱文檔[Ansible copy options](http://docs.ansible.com/ansible/latest/copy_module.html#options) – Martin