I encountered a similar issue and managed to solve it by creating a simple filter plugin:
def ip_in_net(list, net):
from netaddr import IPAddress, IPNetwork
return [ip for ip in list if IPAddress(ip) in IPNetwork(net)]
class FilterModule(object):
def filters(self):
return {
'ip_in_net': ip_in_net
}
and then use it like that:
ip: "{{ ansible_all_ipv4_addresses | ip_in_net('172.16.0.0/24') | first }}"
Now this obviously does not work with your data, and requires a 'netaddr' Python package (apt-get install python-netaddr in my Ubuntu 14.04), but quite likely you could use the interface provided by that package to suit your needs exactly, it's very friendly.