ansible stat然后复制with_items

use*_*834 1 ansible

我有这种重复的模式::

- 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
...
Run Code Online (Sandbox Code Playgroud)

如何使用with_items获取大清单?::

 with_items:
    - .vimrc
    - .bashrc
    - .profile
    - .gitconfig
Run Code Online (Sandbox Code Playgroud)

Arb*_*zar 12

有时您甚至不想复制文件,如果目标计算机上存在该文件,即使内容不同.那么你可以像这样使用(没有在你的场景中测试它,但我认为它会工作)

- 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
Run Code Online (Sandbox Code Playgroud)

希望对你有所帮助