# need help with nested loop

**URL:** <https://forum.ansible.com/t/need-help-with-nested-loop/30491>\
**Category:** Ansible Project\
**Created:** [June 5, 2019, 6:29am UTC](https://forum.ansible.com/t/need-help-with-nested-loop/30491 "2019-06-05T06:29:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![eric\_sandgren](https://avatars.discourse-cdn.com/v4/letter/e/58f4c7/32.png) [@eric\_sandgren](https://forum.ansible.com/u/eric_sandgren)\
**Post date:** [June 5, 2019, 6:29am UTC](https://forum.ansible.com/t/need-help-with-nested-loop/30491/1 "2019-06-05T06:29:24Z")

</div>

First I’m a sysadmin not a developer I often use ansible to set up things in f5 -ltm i often need to add several hosts to a number of pools. The loadbalancing setup in f5 products in a glance consist of av VIP (virtual server) that is linked to a Pool containing one or more physical servers.  
I get stuck in how to set up the loop, i define this first  
var\_node:

- { host: 1.2.3.4, name: [server1.com](http://server1.com) }
- { host: 1.2.3.5, name: [server2.com](http://server2.com) }  
var\_pool:
- { pool: pool1, port: 80 }
- { pool: pool2, port: 8080 }
- { pool: pool3, port: 443 }

The module ii’m trying to use

- name: add nodes to pools  
bigip\_pool\_members:  
provider: “{{ provider }}”  
partition: “{{ partition }}”  
pool:{{ item.pool }} "here I want to loop var\_pool

name: "{{ item.name }} “here I want to loop var\_node”  
port: "{{ item.port}} “and again …var\_pool”  
loop: ???

I realize I’m probably on a dead end thinking this way any good input from a developer-mind would be extremely appreciated

/Eric

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [June 5, 2019, 7:19am UTC](https://forum.ansible.com/t/need-help-with-nested-loop/30491/2 "2019-06-05T07:19:18Z")

</div>

You are looking for with\_together  
[https://docs.ansible.com/ansible/2.4/playbooks\_loops.html#looping-over-parallel-sets-of-data](https://docs.ansible.com/ansible/2.4/playbooks_loops.html#looping-over-parallel-sets-of-data)

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [June 6, 2019, 6:59am UTC](https://forum.ansible.com/t/need-help-with-nested-loop/30491/3 "2019-06-06T06:59:06Z")

</div>

Yeah with\_together would work in your scenario.

Strangely I was looking to do something similar but the very helpful F5 team provided me with this, so I thought I’d share it.

I was trying to find a way to switch the pool members between 2 sets of servers.

One difference here is we’re associating the port with the pool member, rather than the pool in your case.

vars:

f5\_pools:  
middleware:  
red:

- host: 192.168.27.1  
port: 80  
green:
- host: 192.168.27.2  
port: 80  
webfront:  
red:
- host: 192.168.27.1  
port: 80
- host: 192.168.27.2  
port: 80  
green:
- host: 192.168.28.1  
port: 80
- host: 192.168.28.2  
port: 80

tasks:

- name: RED  
bigip\_pool\_member:  
pool: “{{ item }}”  
aggregate: “{{ f5\_pools[item].red }}”  
replace\_all\_with: yes  
provider: “{{ provider }}”  
delegate\_to: localhost  
loop: “{{ f5\_pools.keys() }}”  
tags: red

- name: GREEN  
bigip\_pool\_member:  
pool: “{{ item }}”  
aggregate: “{{ f5\_pools[item].green }}”  
replace\_all\_with: yes  
provider: “{{ provider }}”  
delegate\_to: localhost  
loop: “{{ f5\_pools.keys() }}”  
tags : green
