我有这本Ansible-playbook:
---
- hosts: 127.0.0.1
tasks:
- name: test nested loops
shell: echo "a={{ item.0 }} b={{ item.1 }}"
with_together:
- [ 'vol1', 'vol2', 'vol3', 'vol4', 'vol5']
- [ 'cmp01', 'cmp02', 'cmp03']
目前我得到了这个结果:
changed: [127.0.0.1] => (item=['vol1', 'cmp01'])
changed: [127.0.0.1] => (item=['vol2', 'cmp02'])
changed: [127.0.0.1] => (item=['vol3', 'cmp03'])
changed: [127.0.0.1] => (item=['vol4', None])
changed: [127.0.0.1] => (item=['vol5', None])
而不是这个结果,我希望最后一个列表重复。
changed: [127.0.0.1] => (item=['vol1', 'cmp01'])
changed: [127.0.0.1] => (item=['vol2', 'cmp02'])
changed: [127.0.0.1] => (item=['vol3', 'cmp03'])
changed: [127.0.0.1] => (item=['vol4', 'cmp01'])
changed: [127.0.0.1] => (item=['vol5', 'cmp02'])
这是一个简短的示例剧本,但它有很多可能性。这是因为我想使用此功能将VOL1
委托给CMP01
等等。
备注:我不能使用嵌套循环,因为我得到了这个:
changed: [127.0.0.1] => (item=['vol1', 'cmp01'])
changed: [127.0.0.1] => (item=['vol1', 'cmp02'])
changed: [127.0.0.1] => (item=['vol1', 'cmp03'])
changed: [127.0.0.1] => (item=['vol2', 'cmp01'])
changed: [127.0.0.1] => (item=['vol2', 'cmp02'])
changed: [127.0.0.1] => (item=['vol2', 'cmp03'])
changed: [127.0.0.1] => (item=['vol3', 'cmp01'])
changed: [127.0.0.1] => (item=['vol3', 'cmp02'])
changed: [127.0.0.1] => (item=['vol3', 'cmp03'])
changed: [127.0.0.1] => (item=['vol4', 'cmp01'])
changed: [127.0.0.1] => (item=['vol4', 'cmp02'])
changed: [127.0.0.1] => (item=['vol4', 'cmp03'])
changed: [127.0.0.1] => (item=['vol5', 'cmp01'])
changed: [127.0.0.1] => (item=['vol5', 'cmp02'])
changed: [127.0.0.1] => (item=['vol5', 'cmp03'])
即使我包含run_once: true
选项,它也不起作用,这是非常合乎逻辑的。
Ansible-documentation: http://docs.ansible.com/ansible/playbooks_loops.html