How to filter list of IP addresses based on network in Ansible
Published in
Mar 3, 2022
Recent transition from ‘the ansible’ to ‘ansible-core + collections’ rendered documentation very fragmented. The problem I solved just now is following:
Given the list of IP addresses select only those belonging to a given network.
Example of input:
all_ipv4_addresses:
- 188.42.54.219
- 10.26.93.166network: 10.0.0.0/8
Expected output:
["10.26.93.166"]
The solution
It’s in the docs, but, as I said, docs are fragmented and hard to find.
{{ all_ipv4_addresses | ansible.utils.reduce_on_network(network) }}
Output:
ok: [localhost] => {
"msg": [
"10.26.93.166"
]
}