Ansible Loop 在同一个循环中使用 with_items
Ansible Loop within the same loop using with_items
寻求帮助尝试遍历多个池,而某些池可能有超过 1 个主机。
我尝试过 with_nested
和 with_subelements
,但两者似乎都没有达到我的要求。
- name: Pool members
bigip_pool_member:
state: present
pool: "{{ item.pool }}"
partition: Common
host: "{{ item.host }}"
port: "{{ item.port }}"
provider:
server: bigip.domain.com
user: admin
password: admin
validate_certs: no
delegate_to: localhost
with_items:
- { pool: pool, host: [10.10.10.10, 10.10.10.11], port: 80 }
- { pool: pool2, host: 10.10.10.10, port: 80 }
我相信主机字段一次只能取一个值,所以我需要循环遍历一个池的主机。输出将类似于
bigip_pool_member:
state: present
pool: pool
partition: Common
host: 10.10.10.10
port: 80
bigip_pool_member:
state: present
pool: pool
partition: Common
host: 10.10.10.11
port: 80
bigip_pool_member:
state: present
pool: pool2
partition: Common
host: 10.10.10.10
port: 80
循环 subelements 就是您要找的。例如,请参阅下面的剧本如何引用您需要的属性
- hosts: localhost
vars:
my_pools:
- {pool: pool, host: [10.10.10.10, 10.10.10.11], port: 80}
- {pool: pool2, host: [10.10.10.10], port: 80}
tasks:
- debug:
msg: "pool:{{ item.0.pool }} port:{{ item.0.port }} host:{{ item.1 }}"
with_subelements:
- "{{ my_pools }}"
- host
给予
"msg": "pool:pool port:80 host:10.10.10.10"
"msg": "pool:pool port:80 host:10.10.10.11"
"msg": "pool:pool2 port:80 host:10.10.10.10"
备注
- 只需将对属性的引用放入您的代码中。
- 保持一致。属性
host
必须是一个列表,即使该列表只有一项。
- 使用
loop
代替with_subelements
loop: "{{lookup('subelements', my_pools, 'host', {'skip_missing': True})}}"
以下是应该可以正常工作的最小更改的任务
- name: Pool members
bigip_pool_member:
state: present
pool: "{{ item.0.pool }}"
partition: Common
host: "{{ item.1 }}"
port: "{{ item.0.port }}"
provider:
server: bigip.domain.com
user: admin
password: admin
validate_certs: no
delegate_to: localhost
with_subelements:
- - {pool: pool, host: [10.10.10.10, 10.10.10.11], port: 80}
- {pool: pool2, host: [10.10.10.10], port: 80}
- host
(未测试)
寻求帮助尝试遍历多个池,而某些池可能有超过 1 个主机。
我尝试过 with_nested
和 with_subelements
,但两者似乎都没有达到我的要求。
- name: Pool members
bigip_pool_member:
state: present
pool: "{{ item.pool }}"
partition: Common
host: "{{ item.host }}"
port: "{{ item.port }}"
provider:
server: bigip.domain.com
user: admin
password: admin
validate_certs: no
delegate_to: localhost
with_items:
- { pool: pool, host: [10.10.10.10, 10.10.10.11], port: 80 }
- { pool: pool2, host: 10.10.10.10, port: 80 }
我相信主机字段一次只能取一个值,所以我需要循环遍历一个池的主机。输出将类似于
bigip_pool_member:
state: present
pool: pool
partition: Common
host: 10.10.10.10
port: 80
bigip_pool_member:
state: present
pool: pool
partition: Common
host: 10.10.10.11
port: 80
bigip_pool_member:
state: present
pool: pool2
partition: Common
host: 10.10.10.10
port: 80
循环 subelements 就是您要找的。例如,请参阅下面的剧本如何引用您需要的属性
- hosts: localhost
vars:
my_pools:
- {pool: pool, host: [10.10.10.10, 10.10.10.11], port: 80}
- {pool: pool2, host: [10.10.10.10], port: 80}
tasks:
- debug:
msg: "pool:{{ item.0.pool }} port:{{ item.0.port }} host:{{ item.1 }}"
with_subelements:
- "{{ my_pools }}"
- host
给予
"msg": "pool:pool port:80 host:10.10.10.10"
"msg": "pool:pool port:80 host:10.10.10.11"
"msg": "pool:pool2 port:80 host:10.10.10.10"
备注
- 只需将对属性的引用放入您的代码中。
- 保持一致。属性
host
必须是一个列表,即使该列表只有一项。 - 使用
loop
代替with_subelements
loop: "{{lookup('subelements', my_pools, 'host', {'skip_missing': True})}}"
以下是应该可以正常工作的最小更改的任务
- name: Pool members
bigip_pool_member:
state: present
pool: "{{ item.0.pool }}"
partition: Common
host: "{{ item.1 }}"
port: "{{ item.0.port }}"
provider:
server: bigip.domain.com
user: admin
password: admin
validate_certs: no
delegate_to: localhost
with_subelements:
- - {pool: pool, host: [10.10.10.10, 10.10.10.11], port: 80}
- {pool: pool2, host: [10.10.10.10], port: 80}
- host
(未测试)