[英]Ansible Loop within the same loop using with_items
Looking for help trying to loop through multiple pools while some pools may have more than 1 host.寻求帮助尝试遍历多个池,而某些池可能有超过 1 个主机。 I've attempted to do
with_nested
and with_subelements
but both don't seem to do what I want.我尝试过
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 }
I believe the host field can only take one value at a time, so I need to loop through the hosts for one pool.我相信主机字段一次只能取一个值,所以我需要循环访问一个池的主机。 The output would be something along the lines of
output 将类似于以下内容
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
Loop subelements is what you're looking for.循环子元素是您正在寻找的。 For example, see the playbook below how to reference the attributes you need
例如,请参阅下面的剧本如何引用您需要的属性
- 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
give给
"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"
Notes笔记
host
must be a list even if the list has one item only.host
也必须是列表。loop
instead of with_subelements
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
(not tested) (未测试)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.