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

92 views
Skip to first unread message

nusenu

unread,
Feb 11, 2016, 7:12:31 PM2/11/16
to ansible...@googlegroups.com
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

signature.asc

nusenu

unread,
Feb 12, 2016, 6:13:52 PM2/12/16
to ansible...@googlegroups.com
> 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:

- set_fact:
ips:
ipv4: "{{ item.0 }}"
ipv6: "{{ item.1 }}"
with_together:
- "{{ ansible_all_ipv4_addresses }}"
- "{{ ansible_all_ipv6_addresses }}"
register: ips

- set_fact: tor_ips="{{ ips.results |
map(attribute='ansible_facts.ips')|list}}"


nicer solutions are still appreciated :)

Brian Coca

unread,
Feb 12, 2016, 6:24:49 PM2/12/16
to ansible...@googlegroups.com
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')))}}"


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/56BD235A.2070107%40openmailbox.org.
For more options, visit https://groups.google.com/d/optout.



--
----------
Brian Coca

nusenu

unread,
Feb 12, 2016, 6:44:27 PM2/12/16
to ansible...@googlegroups.com
> 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.

signature.asc

Brian Coca

unread,
Feb 12, 2016, 6:52:30 PM2/12/16
to ansible...@googlegroups.com
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')] }}"


----------
Brian Coca

nusenu

unread,
Feb 12, 2016, 7:19:48 PM2/12/16
to ansible...@googlegroups.com


Brian Coca:
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:
tcpports:
- 1
- 2
tasks:

- debug: msg="going to bind to port {{ item.0 }} ipv4 {{ item.1.0 }}
ipv6 {{ item.1.1 }} "
with_nested:
- "{{tcpports}}"
- "{{lookup('together', [ansible_all_ipv4_addresses,
ansible_all_ipv6_addresses]) }}"

Brian Coca

unread,
Feb 12, 2016, 8:57:39 PM2/12/16
to ansible...@googlegroups.com
sorry, did not test, surprised it does not take a list, maybe it has to be quoted ?


----------
Brian Coca

nusenu

unread,
Feb 14, 2016, 6:32:19 AM2/14/16
to ansible...@googlegroups.com
> 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

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


signature.asc
Reply all
Reply to author
Forward
0 new messages