我有一个像这样的重复模式::
- name: =code_01= clone repository
git: repo=gitolite@git.site.net:/code_01
dest=/tmp/code_01
update=yes
force=yes
accept_hostkey=yes
version=master
sudo: true
sudo_user: "{{ user }}"
- name: =code_01= egg build
shell: . {{ home }}/{{ venv_name }}/bin/activate && make egg
args:
chdir: "/tmp/code_01"
sudo_user: "{{ user }}"
sudo: true
- name: =code_01= egg get
command: find /tmp/code_01/dist -type f -iname '*.egg'
register: code_01eggs
- name: =code_01= egg install in {{ venv_name }} venv
shell: . {{ home }}/{{ venv_name }}/bin/activate && easy_install {{ item }}
args:
chdir: "{{ home }}"
with_items: "{{ code_01eggs.stdout_lines }}"
sudo_user: "{{ user }}"
sudo: true
- name: =code_01= cleanup
file: path=/tmp/code_01 state=absent
sudo: true
我与此有关: code_02 , code_03 , code_04 ,..., code_0n
我怎样才能“分解”这个?
答案 0 :(得分:11)
您可以将逻辑移至第二个yml文件,并将include
与with_items
一起使用:
- include: factored.yml
with_items:
- code_01
- code_02
- code_03
在第二个文件中,将code_01
替换为{{ item }}
。
有用的链接:Ansible docs: Loops and include
注意:如果您有嵌套循环,则可以在Ansible 2.0之后使用set_fact
设置outter循环项。有关更多信息,请参阅上述文档。