def ipaddr(value, query='', version=False, alias='ipaddr'):
 query_func_extra_args = {'': ('vtype',), '6to4': ('vtype', 'value'), 'cidr_lookup': ('iplist', 'value'), 'int': ('vtype',), 'ipv4': ('value',), 'ipv6': ('value',), 'link-local': ('value',), 'loopback': ('value',), 'lo': ('value',), 'multicast': ('value',), 'private': ('value',), 'public': ('value',), 'unicast': ('value',), 'wrap': ('vtype', 'value')}
 query_func_map = {'': _empty_ipaddr_query, '6to4': _6to4_query, 'address': _ip_query, 'address/prefix': _gateway_query, 'bool': _bool_ipaddr_query, 'broadcast': _broadcast_query, 'cidr': _cidr_query, 'cidr_lookup': _cidr_lookup_query, 'gateway': _gateway_query, 'gw': _gateway_query, 'host': _host_query, 'host/prefix': _gateway_query, 'hostmask': _hostmask_query, 'hostnet': _gateway_query, 'int': _int_query, 'ip': _ip_query, 'ipv4': _ipv4_query, 'ipv6': _ipv6_query, 'link-local': _link_local_query, 'lo': _loopback_query, 'loopback': _loopback_query, 'multicast': _multicast_query, 'net': _net_query, 'netmask': _netmask_query, 'network': _network_query, 'prefix': _prefix_query, 'private': _private_query, 'public': _public_query, 'revdns': _revdns_query, 'router': _gateway_query, 'size': _size_query, 'subnet': _subnet_query, 'type': _type_query, 'unicast': _unicast_query, 'v4': _ipv4_query, 'v6': _ipv6_query, 'version': _version_query, 'wrap': _wrap_query}
 vtype = None
 if (not value):
  return False
 elif (value == True):
  return False
 elif isinstance(value, (list, tuple, types.GeneratorType)):
  _ret = []
  for element in value:
   if ipaddr(element, str(query), version):
    _ret.append(ipaddr(element, str(query), version))
  if _ret:
   return _ret
  else:
   return list()
 elif str(value).isdigit():
  try:
   if ((not version) or (version and (version == 4))):
    v = netaddr.IPNetwork('0.0.0.0/0')
    v.value = int(value)
    v.prefixlen = 32
   elif (version and (version == 6)):
    v = netaddr.IPNetwork('::/0')
    v.value = int(value)
    v.prefixlen = 128
  except:
   try:
    v = netaddr.IPNetwork('::/0')
    v.value = int(value)
    v.prefixlen = 128
   except:
    return False
  value = str(v)
  vtype = 'address'
 else:
  try:
   v = netaddr.IPNetwork(value)
   try:
    (address, prefix) = value.split('/')
    vtype = 'network'
   except:
    vtype = 'address'
  except:
   try:
    (address, prefix) = value.split('/')
    address.isdigit()
    address = int(address)
    prefix.isdigit()
    prefix = int(prefix)
   except:
    return False
   try:
    v = netaddr.IPNetwork('0.0.0.0/0')
    v.value = address
    v.prefixlen = prefix
   except:
    try:
     v = netaddr.IPNetwork('::/0')
     v.value = address
     v.prefixlen = prefix
    except:
     return False
   value = str(v)
   vtype = 'network'
 try:
  if (query and ((query not in query_func_map) or (query == 'cidr_lookup')) and ipaddr(query, 'network')):
   iplist = netaddr.IPSet([netaddr.IPNetwork(query)])
   query = 'cidr_lookup'
 except:
  pass
 if (version and (v.version != version)):
  return False
 extras = []
 for arg in query_func_extra_args.get(query, tuple()):
  extras.append(locals()[arg])
 try:
  return query_func_map[query](v, *extras)
 except KeyError:
  try:
   float(query)
   if (v.size == 1):
    if (vtype == 'address'):
     return str(v.ip)
    elif (vtype == 'network'):
     return str(v)
   elif (v.size > 1):
    try:
     return ((str(v[query]) + '/') + str(v.prefixlen))
    except:
     return False
   else:
    return value
  except:
   raise errors.AnsibleFilterError((alias + (': unknown filter type: %s' % query)))
 return False

def ipaddr ( value , query = '' , version = False , alias = 'ipaddr' ) : query_func_extra_args = { '' : ( 'vtype' , ) , '6to4' : ( 'vtype' , 'value' ) , 'cidr_lookup' : ( 'iplist' , 'value' ) , 'int' : ( 'vtype' , ) , 'ipv4' : ( 'value' , ) , 'ipv6' : ( 'value' , ) , 'link-local' : ( 'value' , ) , 'loopback' : ( 'value' , ) , 'lo' : ( 'value' , ) , 'multicast' : ( 'value' , ) , 'private' : ( 'value' , ) , 'public' : ( 'value' , ) , 'unicast' : ( 'value' , ) , 'wrap' : ( 'vtype' , 'value' ) } query_func_map = { '' : _empty_ipaddr_query , '6to4' : _6to4_query , 'address' : _ip_query , 'address/prefix' : _gateway_query , 'bool' : _bool_ipaddr_query , 'broadcast' : _broadcast_query , 'cidr' : _cidr_query , 'cidr_lookup' : _cidr_lookup_query , 'gateway' : _gateway_query , 'gw' : _gateway_query , 'host' : _host_query , 'host/prefix' : _gateway_query , 'hostmask' : _hostmask_query , 'hostnet' : _gateway_query , 'int' : _int_query , 'ip' : _ip_query , 'ipv4' : _ipv4_query , 'ipv6' : _ipv6_query , 'link-local' : _link_local_query , 'lo' : _loopback_query , 'loopback' : _loopback_query , 'multicast' : _multicast_query , 'net' : _net_query , 'netmask' : _netmask_query , 'network' : _network_query , 'prefix' : _prefix_query , 'private' : _private_query , 'public' : _public_query , 'revdns' : _revdns_query , 'router' : _gateway_query , 'size' : _size_query , 'subnet' : _subnet_query , 'type' : _type_query , 'unicast' : _unicast_query , 'v4' : _ipv4_query , 'v6' : _ipv6_query , 'version' : _version_query , 'wrap' : _wrap_query } vtype = None if ( not value ) : return False elif ( value == True ) : return False elif isinstance ( value , ( list , tuple , types . GeneratorType ) ) : _ret = [ ] for element in value : if ipaddr ( element , str ( query ) , version ) : _ret . append ( ipaddr ( element , str ( query ) , version ) ) if _ret : return _ret else : return list ( ) elif str ( value ) . isdigit ( ) : try : if ( ( not version ) or ( version and ( version == 4 ) ) ) : v = netaddr . IPNetwork ( '0.0.0.0/0' ) v . value = int ( value ) v . prefixlen = 32 elif ( version and ( version == 6 ) ) : v = netaddr . IPNetwork ( '::/0' ) v . value = int ( value ) v . prefixlen = 128 except : try : v = netaddr . IPNetwork ( '::/0' ) v . value = int ( value ) v . prefixlen = 128 except : return False value = str ( v ) vtype = 'address' else : try : v = netaddr . IPNetwork ( value ) try : ( address , prefix ) = value . split ( '/' ) vtype = 'network' except : vtype = 'address' except : try : ( address , prefix ) = value . split ( '/' ) address . isdigit ( ) address = int ( address ) prefix . isdigit ( ) prefix = int ( prefix ) except : return False try : v = netaddr . IPNetwork ( '0.0.0.0/0' ) v . value = address v . prefixlen = prefix except : try : v = netaddr . IPNetwork ( '::/0' ) v . value = address v . prefixlen = prefix except : return False value = str ( v ) vtype = 'network' try : if ( query and ( ( query not in query_func_map ) or ( query == 'cidr_lookup' ) ) and ipaddr ( query , 'network' ) ) : iplist = netaddr . IPSet ( [ netaddr . IPNetwork ( query ) ] ) query = 'cidr_lookup' except : pass if ( version and ( v . version != version ) ) : return False extras = [ ] for arg in query_func_extra_args . get ( query , tuple ( ) ) : extras . append ( locals ( ) [ arg ] ) try : return query_func_map [ query ] ( v , * extras ) except KeyError : try : float ( query ) if ( v . size == 1 ) : if ( vtype == 'address' ) : return str ( v . ip ) elif ( vtype == 'network' ) : return str ( v ) elif ( v . size > 1 ) : try : return ( ( str ( v [ query ] ) + '/' ) + str ( v . prefixlen ) ) except : return False else : return value except : raise errors . AnsibleFilterError ( ( alias + ( ': unknown filter type: %s' % query ) ) ) return False
filters and returns only valid ip objects .