Ejemplo n.º 1
0
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}
Ejemplo n.º 2
0
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
Ejemplo n.º 3
0
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
Ejemplo n.º 4
0
def _ip(ip):
    """ Test if something in an IP address or network """

    params = {"ip": ip}
    _validate_args("ip", DOCUMENTATION, params)

    try:
        ip_network(ip)
        return True
    except Exception:
        return False
Ejemplo n.º 5
0
def _ipv4(ip):
    """Test if something in an IPv4 address or network"""

    params = {"ip": ip}
    _validate_args("ipv4", DOCUMENTATION, params)

    try:
        return ip_network(ip).version == 4
    except Exception:
        return False
Ejemplo n.º 6
0
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
Ejemplo n.º 7
0
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