def _usable_range(ip): """Expand the usable IP addresses""" params = {"ip": ip} _validate_args("usable_range", DOCUMENTATION, params) try: if ip_network(ip).version == 4: ips = [ to_text(usable_ips) for usable_ips in IPv4Network(ensure_text(ip)) ] no_of_ips = IPv4Network(ensure_text(ip)).num_addresses if ip_network(ip).version == 6: ips = [ to_text(usable_ips) for usable_ips in IPv6Network(ensure_text(ip)) ] no_of_ips = IPv6Network(ensure_text(ip)).num_addresses except Exception as e: raise AnsibleFilterError( "Error while using plugin 'usable_range': {msg}".format( msg=to_text(e))) return {"usable_ips": ips, "number_of_ips": no_of_ips}
def _supernet_of(network_a, network_b): """ Test if an network is a supernet of another network """ params = {"network_a": network_a, "network_b": network_b} _validate_args("supernet_of", DOCUMENTATION, params) try: return _is_subnet_of(ip_network(network_b), ip_network(network_a)) except Exception: return False
def _ipv6_address(ip): """Test if something in an IPv6 address""" params = {"ip": ip} _validate_args("ipv6_address", DOCUMENTATION, params) try: return ip_address(ip).version == 6 except Exception: return False
def _in_network(ip, network): """Test if an address or network is in a network""" params = {"ip": ip, "network": network} _validate_args("in_network", DOCUMENTATION, params) try: return _is_subnet_of(ip_network(ip), ip_network(network)) except Exception: return False
def _private(ip): """ Test if an IP address is private """ params = {"ip": ip} _validate_args("private", DOCUMENTATION, params) try: return ip_address(ip).is_private except Exception: return False
def _reserved(ip): """ Test for a reserved IP address """ params = {"ip": ip} _validate_args("reserved", DOCUMENTATION, params) try: return ip_address(ip).is_reserved except Exception: return False
def _multicast(ip): """ Test for a multicast IP address """ params = {"ip": ip} _validate_args("multicast", DOCUMENTATION, params) try: return ip_address(ip).is_multicast except Exception: return False
def _ipv4_netmask(mask): """ Test for a valid IPv4 netmask""" params = {"mask": mask} _validate_args("ipv4_netmask", DOCUMENTATION, params) try: network = ip_network("10.0.0.0/{mask}".format(mask=mask)) return str(network.netmask) == mask except Exception: return False
def _ipv4_hostmask(ip): """Test if an address is a hostmask""" params = {"ip": ip} _validate_args("ipv4_hostmask", DOCUMENTATION, params) try: ipaddr = ip_network("10.0.0.0/{ip}".format(ip=ip)) return str(ipaddr.hostmask) == ip except Exception: return False
def _ip_address(ip): """Test if something in an IP address""" params = {"ip": ip} _validate_args("ip_address", DOCUMENTATION, params) try: ip_address(ip) return True except Exception: return False
def _ipv6_teredo(ip): """ Test if something is an IPv6 teredo address """ params = {"ip": ip} _validate_args("ipv6_teredo", DOCUMENTATION, params) try: if ip_address(ip).teredo is None: return False return True except Exception: return False
def _ipv6_sixtofour(ip): """ Test if something appears to be a 6to4 address """ params = {"ip": ip} _validate_args("ipv6_sixtofour", DOCUMENTATION, params) try: if ip_address(ip).sixtofour is None: return False return True except Exception: return False
def _resolvable(host): """ Test if an IP or name can be resolved via /etc/hosts or DNS """ params = {"host": host} _validate_args("resolvable", DOCUMENTATION, params) try: ipaddress.ip_address(host) ip = True except Exception: ip = False if ip: try: socket.gethostbyaddr(host) return True except Exception: return False else: try: socket.getaddrinfo(host, None) return True except Exception: return False