zip the two list to obtain a list of host/ports tuples
$ ansible localhost -m debug \
-e '{"hostnames":["host1","host2"],"ports":[[1,2,3],[4,5]]}' \
-a msg="{{ hostnames | zip(ports) }}"
gives
"msg": [
[
"host1",
[
1,
2,
3
]
],
[
"host2",
[
4,
5
]
]
]
- create a dict from that list of tuples: keyname is the host, value is the list of ports:
$ ansible localhost -m debug \
-e '{"hostnames":["host1","host2"],"ports":[[1,2,3],[4,5]]}' \
-a msg="{{ dict(hostnames | zip(ports)) }}"
gives
"msg": {
"host1": [
1,
2,
3
],
"host2": [
4,
5
]
}
- tranform that dict to a list of
{host: x, ports: [y,z]} elements
$ ansible localhost -m debug \
-e '{"hostnames":["host1","host2"],"ports":[[1,2,3],[4,5]]}' \
-a msg="{{ dict(hostnames | zip(ports)) | dict2items(key_name='host', value_name='ports') }}"
gives
"msg": [
{
"host": "host1",
"ports": [
1,
2,
3
]
},
{
"host": "host2",
"ports": [
4,
5
]
}
]
- loop over that list with a subelement loop on ports. Here is a full playbook to test the entire scenario:
---
- hosts: localhost
gather_facts: false
vars:
hostnames:
- host1
- host2
- host3
ports:
- [1, 2, 3]
- [4, 5, 6, 7]
- [8, 9, 10]
hosts_list: "{{ dict(hostnames | zip(ports)) | dict2items(key_name='host', value_name='ports') }}"
tasks:
- name: loop over our host list for every declared port
debug:
msg: "Host {{ item.0.host }} port {{ item.1 }}"
loop: "{{ hosts_list | subelements('ports') }}"
which gives:
PLAY [localhost] ****************************************************************************************************************************************************************************
TASK [loop over our host list for every declared port] **************************************************************************************************************************************
ok: [localhost] => (item=[{'host': 'host1', 'ports': [1, 2, 3]}, 1]) => {
"msg": "Host host1 port 1"
}
ok: [localhost] => (item=[{'host': 'host1', 'ports': [1, 2, 3]}, 2]) => {
"msg": "Host host1 port 2"
}
ok: [localhost] => (item=[{'host': 'host1', 'ports': [1, 2, 3]}, 3]) => {
"msg": "Host host1 port 3"
}
ok: [localhost] => (item=[{'host': 'host2', 'ports': [4, 5, 6, 7]}, 4]) => {
"msg": "Host host2 port 4"
}
ok: [localhost] => (item=[{'host': 'host2', 'ports': [4, 5, 6, 7]}, 5]) => {
"msg": "Host host2 port 5"
}
ok: [localhost] => (item=[{'host': 'host2', 'ports': [4, 5, 6, 7]}, 6]) => {
"msg": "Host host2 port 6"
}
ok: [localhost] => (item=[{'host': 'host2', 'ports': [4, 5, 6, 7]}, 7]) => {
"msg": "Host host2 port 7"
}
ok: [localhost] => (item=[{'host': 'host3', 'ports': [8, 9, 10]}, 8]) => {
"msg": "Host host3 port 8"
}
ok: [localhost] => (item=[{'host': 'host3', 'ports': [8, 9, 10]}, 9]) => {
"msg": "Host host3 port 9"
}
ok: [localhost] => (item=[{'host': 'host3', 'ports': [8, 9, 10]}, 10]) => {
"msg": "Host host3 port 10"
}
PLAY RECAP **********************************************************************************************************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0