# iterating over IPv4/IPv6 (parallel) + TCP ports (nested)

**URL:** <https://forum.ansible.com/t/iterating-over-ipv4-ipv6-parallel-tcp-ports-nested/20553>\
**Category:** Ansible Project\
**Created:** [February 12, 2016, 12:12am UTC](https://forum.ansible.com/t/iterating-over-ipv4-ipv6-parallel-tcp-ports-nested/20553 "2016-02-12T00:12:10Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nusenu1](https://avatars.discourse-cdn.com/v4/letter/n/db5fbb/32.png) [@Nusenu1](https://forum.ansible.com/u/Nusenu1)\
**Post date:** [February 12, 2016, 12:12am UTC](https://forum.ansible.com/t/iterating-over-ipv4-ipv6-parallel-tcp-ports-nested/20553/1 "2016-02-12T00:12:10Z")

</div>

Hi,

similar to a question from about a year ago [1], I'm looking for the  
best way to loop over 3 lists:

- {{ ansible\_all\_ipv4\_addresses }}  
- {{ ansible\_all\_ipv6\_addresses | ipv6('public') }}  
- tcpports

ipv4 and ipv6 IPs should iterate in parallel (I'll check that their list  
lengths are matching) while tcpports should be combined in nested mode.

I guess this is a common use-case?

[1] [https://groups.google.com/d/msg/ansible-devel/aLrH\_SC8HyY/Zz91ifV5M-0J](https://groups.google.com/d/msg/ansible-devel/aLrH_SC8HyY/Zz91ifV5M-0J)

---

<div class="post-metadata">

**Author:** ![Nusenu1](https://avatars.discourse-cdn.com/v4/letter/n/db5fbb/32.png) [@Nusenu1](https://forum.ansible.com/u/Nusenu1)\
**Post date:** [February 12, 2016, 11:13pm UTC](https://forum.ansible.com/t/iterating-over-ipv4-ipv6-parallel-tcp-ports-nested/20553/2 "2016-02-12T23:13:33Z")

</div>

> similar to a question from about a year ago [1], I'm looking for the  
> best way to loop over 3 lists:
> 
> - {{ ansible\_all\_ipv4\_addresses }}  
> - {{ ansible\_all\_ipv6\_addresses | ipv6('public') }}  
> - tcpports
> 
> ipv4 and ipv6 IPs should iterate in parallel (I'll check that their list  
> lengths are matching) while tcpports should be combined in nested mode.

My dirty fix was to put them into a dict:

&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ips:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ipv4: "{{ item.0 }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ipv6: "{{ item.1 }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_together:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "{{ ansible\_all\_ipv4\_addresses }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "{{ ansible\_all\_ipv6\_addresses }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: ips

&nbsp;&nbsp;&nbsp;- set\_fact: tor\_ips="{{ ips.results |  
map(attribute='ansible\_facts.ips')|list}}"

nicer solutions are still appreciated 🙂

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [February 12, 2016, 11:24pm UTC](https://forum.ansible.com/t/iterating-over-ipv4-ipv6-parallel-tcp-ports-nested/20553/3 "2016-02-12T23:24:41Z")

</div>

with\_items:

- {{ ansible\_all\_ipv4\_addresses }}
- {{ ansible\_all\_ipv6\_addresses | ipv6(‘public’) }}
- tcpports

^ will flatten to single list

or you can use union to get unique single list:

with\_items: “{{ tcpports|union(ansible\_all\_ipv4\_addresses|union(ansible\_all\_ipv6\_addresses | ipv6(‘public’)))}}”

---

<div class="post-metadata">

**Author:** ![Nusenu1](https://avatars.discourse-cdn.com/v4/letter/n/db5fbb/32.png) [@Nusenu1](https://forum.ansible.com/u/Nusenu1)\
**Post date:** [February 12, 2016, 11:44pm UTC](https://forum.ansible.com/t/iterating-over-ipv4-ipv6-parallel-tcp-ports-nested/20553/4 "2016-02-12T23:44:14Z")

</div>

> with\_items:  
> - {{ ansible\_all\_ipv4\_addresses }}  
> - {{ ansible\_all\_ipv6\_addresses | ipv6('public') }}  
> - tcpports
> 
> ^ will flatten to single list
> 
> or you can use union to get unique single list:
> 
> with\_items: "{{  
> tcpports\>union(ansible\_all\_ipv4\_addresses|union(ansible\_all\_ipv6\_addresses  
> \> ipv6('public')))}}"

This is not exactly what I wanted to achieve since it doesn't advance  
IPv4/6 in parallel while combining that combination with every port but  
the dirty workaround works and should do it.

Thanks for your reply anyway.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [February 12, 2016, 11:52pm UTC](https://forum.ansible.com/t/iterating-over-ipv4-ipv6-parallel-tcp-ports-nested/20553/5 "2016-02-12T23:52:19Z")

</div>

ah, misunderstood the question, you wan to combine the nested and together lookups (possible example):

with\_nested:

- “{{tcpports}}”
- “{{lookup(‘together’, [ansible\_all\_ipv4\_addresses, ansible\_all\_ipv6\_addresses | ipv6(‘public’)] }}”

---

<div class="post-metadata">

**Author:** ![Nusenu1](https://avatars.discourse-cdn.com/v4/letter/n/db5fbb/32.png) [@Nusenu1](https://forum.ansible.com/u/Nusenu1)\
**Post date:** [February 13, 2016, 12:19am UTC](https://forum.ansible.com/t/iterating-over-ipv4-ipv6-parallel-tcp-ports-nested/20553/6 "2016-02-13T00:19:38Z")

</div>

Brian Coca:

> ah, misunderstood the question, you wan to combine the nested and together  
> lookups (possible example):
> 
> with\_nested:  
> - "{{tcpports}}"  
> - "{{lookup('together', [ansible\_all\_ipv4\_addresses,  
> ansible\_all\_ipv6\_addresses | ipv6('public')] }}"

Since I certainly will need this again, I actually gave it a try, but  
got this error:

Failed to template {{lookup('together', [ansible\_all\_ipv4\_addresses,  
ansible\_all\_ipv6\_addresses]) }}: an unexpected type error occurred.  
Error was sequence item 0: expected string, list found

vars:  
&nbsp;&nbsp;&nbsp;&nbsp;tcpports:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- 1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- 2  
tasks:

&nbsp;&nbsp;&nbsp;- debug: msg="going to bind to port {{ item.0 }} ipv4 {{ item.1.0 }}  
ipv6 {{ item.1.1 }} "  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_nested:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "{{tcpports}}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "{{lookup('together', [ansible\_all\_ipv4\_addresses,  
ansible\_all\_ipv6\_addresses]) }}"

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [February 13, 2016, 1:57am UTC](https://forum.ansible.com/t/iterating-over-ipv4-ipv6-parallel-tcp-ports-nested/20553/7 "2016-02-13T01:57:29Z")

</div>

sorry, did not test, surprised it does not take a list, maybe it has to be quoted ?

---

<div class="post-metadata">

**Author:** ![Nusenu1](https://avatars.discourse-cdn.com/v4/letter/n/db5fbb/32.png) [@Nusenu1](https://forum.ansible.com/u/Nusenu1)\
**Post date:** [February 14, 2016, 11:31am UTC](https://forum.ansible.com/t/iterating-over-ipv4-ipv6-parallel-tcp-ports-nested/20553/8 "2016-02-14T11:31:17Z")

</div>

> sorry, did not test, surprised it does not take a list, maybe it has to be  
> quoted ?

I'm not able to get this to work, if anyone is, please let me know.

There is no documentations about lookup('together', ..) on  
[https://docs.ansible.com/ansible/playbooks\_lookups.html](https://docs.ansible.com/ansible/playbooks_lookups.html)

I made a request for documentation here:  
[https://github.com/ansible/ansible/issues/14467](https://github.com/ansible/ansible/issues/14467)
