短名单不会在ansible-task中一起循环重复

时间:2015-12-16 17:14:30

标签: loops ansible nested-loops ansible-playbook

我有这本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

1 个答案:

答案 0 :(得分:1)

我不认为Ansible提供的with循环可以完全满足您的需求。您最好的选择可能是编写自己的custom loop iterator,其行为完全符合您的需要。