Beispiel #1
0
def InitNornsible(nr: Nornir) -> Nornir:
    """
    Patch nornir object based on cli arguments

    Arguments:
        nr: Nornir object

    Returns:
        nr: Nornir object; modified if cli args dictate the need to do so; otherwise passed as is

    Raises:
        N/A  # noqa

    """
    cli_args = parse_cli_args(sys.argv[1:])

    nr.run_tags = cli_args.pop("run_tags")
    nr.skip_tags = cli_args.pop("skip_tags")

    if any(a for a in cli_args.values()):
        nr.config = patch_config(cli_args, nr.config)
        nr.inventory = patch_inventory(cli_args, nr.inventory)

    if not cli_args["disable_delegate"]:
        nr.inventory = patch_inventory_delegate(nr.inventory)

    return nr
Beispiel #2
0
 def nornir_ping_job(self, args):
     task, node, results = args
     nornir_inventory = {node.name: {'nornir_ip': node.ip_address}}
     external = Nornir(inventory=Inventory(nornir_inventory), dry_run=True)
     ping_result = external.run(networking.tcp_ping, ports=[23, 443])
     results[node.name] = str(ping_result[node.name].result)
     return all(res for res in ping_result[node.name].result.keys())
Beispiel #3
0
def job(script, task, device, results, incoming_payload):
    '''Script that uses Nornir to ping a device.'''
    nornir_inventory = {device.name: {'nornir_ip': device.ip_address}}
    external = Nornir(inventory=Inventory(nornir_inventory), dry_run=True)
    ping_result = external.run(networking.tcp_ping, ports=[23, 443])
    return (
        all(res for res in ping_result[device.name].result.keys()),
        str(ping_result[device.name].result),
        incoming_payload,
    )
Beispiel #4
0
def job(args):
    # Script that uses Nornir to ping a device
    task, node, results = args
    nornir_inventory = {node.name: {'nornir_ip': node.ip_address}}
    external = Nornir(inventory=Inventory(nornir_inventory), dry_run=True)
    ping_result = external.run(networking.tcp_ping, ports=[23, 443])
    results[node.name] = {
        'success': all(res for res in ping_result[node.name].result.keys()),
        'logs': str(ping_result[node.name].result)
    }
Beispiel #5
0
def test_tcp_ping_external_hosts():
    external = Nornir(inventory=SimpleInventory(ext_inv_file, ""),
                      dry_run=True)
    result = external.run(networking.tcp_ping, ports=[23, 443])

    assert result
    for h, r in result.items():
        if h == "www.github.com":
            assert r.result[23] is False
            assert r.result[443]
        else:
            assert r.result[23] is False
            assert r.result[443] is False
Beispiel #6
0
def inventory_selector(
        nr: Nornir,
        resync: bool = True,
        hostname: Optional[Union[str, List[str]]] = None,
        device_type: Optional[str] = None,
        group: Optional[str] = None) -> Tuple[Nornir, int, List[str]]:
    """Return a filtered Nornir inventory with only the selected devices

    Args:
        nr: Nornir object
        resync: Set to false if you want to filter out devices that are synchronized
        hostname: Select device by hostname (string) or list of hostnames (list)
        device_type: Select device by device_type (string)
        group: Select device by group (string)

    Returns:
        Tuple with: filtered Nornir inventory, total device count selected,
                    list of hostnames that was skipped because of resync=False
    """
    skipped_devices = []
    if hostname:
        if isinstance(hostname, str):
            nr_filtered = nr.filter(name=hostname).filter(managed=True)
        elif isinstance(hostname, list):
            nr_filtered = nr.filter(
                filter_func=lambda h: h.name in hostname).filter(managed=True)
        else:
            raise ValueError(
                "Can't select hostname based on type {}".type(hostname))
    elif device_type:
        nr_filtered = nr.filter(F(groups__contains='T_' +
                                  device_type)).filter(managed=True)
    elif group:
        nr_filtered = nr.filter(F(groups__contains=group)).filter(managed=True)
    else:
        # all devices
        nr_filtered = nr.filter(managed=True)

    if resync or hostname:
        return nr_filtered, len(nr_filtered.inventory.hosts), skipped_devices
    else:
        pre_device_list = list(nr_filtered.inventory.hosts.keys())
        nr_filtered = nr_filtered.filter(synchronized=False)
        post_device_list = list(nr_filtered.inventory.hosts.keys())
        skipped_devices = [
            x for x in pre_device_list if x not in post_device_list
        ]
        return nr_filtered, len(post_device_list), skipped_devices
def InitNornir(
    config_file: str = "",
    dry_run: bool = False,
    **kwargs: Any,
) -> Nornir:
    """
    Arguments:
        config_file(str): Path to the configuration file (optional)
        dry_run(bool): Whether to simulate changes or not
        configure_logging: Whether to configure logging or not. This argument is being
            deprecated. Please use logging.enabled parameter in the configuration
            instead.
        **kwargs: Extra information to pass to the
            :obj:`nornir.core.configuration.Config` object

    Returns:
        :obj:`nornir.core.Nornir`: fully instantiated and configured
    """
    ConnectionPluginRegister.auto_register()

    if config_file:
        config = Config.from_file(config_file, **kwargs)
    else:
        config = Config.from_dict(**kwargs)

    data = GlobalState(dry_run=dry_run)

    config.logging.configure()

    return Nornir(
        inventory=load_inventory(config),
        runner=load_runner(config),
        config=config,
        data=data,
    )
Beispiel #8
0
def check_devices_connectivity(nr: Nornir) -> bool:
    """
    This function will test the connectivity to each devices

    :param nr:
    :return bool: True if ALL devices are reachable
    """

    devices = nr.filter()

    if len(devices.inventory.hosts) == 0:
        raise Exception(f"[{ERROR_HEADER}] no device selected.")

    data = devices.run(task=is_alive, num_workers=100)
    # print_result(data)

    for device in devices.inventory.hosts:
        if data[device].failed:
            print(f"\t--> Connection to {device} has failed.")

    if not data.failed:
        print("All devices are reachable :) !")
    else:
        print(f"\nPlease check credentials in the inventory")

    printline()
    return not data.failed
def execute_ping(nr: Nornir, filters={}, level=None, own_vars={}):

    devices = nr.filter()

    if len(devices.inventory.hosts) == 0:
        raise Exception(f"[{HEADER_GET}] no device selected.")

    path_url = f"{PATH_TO_VERITY_FILES}{PING_SRC_FILENAME}"

    data_retrieve = devices.run(task=retrieve_ping_from_yaml,
                                on_failed=True,
                                num_workers=10)
    print_result(data_retrieve)

    generate_cmd = devices.run(task=_generic_generate_ping_cmd,
                               on_failed=True,
                               num_workers=10)
    print_result(generate_cmd)

    execute_cmd = devices.run(task=_execute_ping_cmd,
                              on_failed=True,
                              num_workers=10)
    print_result(execute_cmd)

    return (not execute_cmd.failed)
Beispiel #10
0
def get_bgp(nr: Nornir, options={}):
    if ('from_cli' in options.keys() and options.get('from_cli') is not None
            and options.get("from_cli") is True
            and isinstance(options.get("from_cli"), bool)):
        devices = nr.filter(F(groups__contains="netests"))
        os.environ["NETESTS_VERBOSE"] = LEVEL1
    else:
        devices = nr.filter()

    if len(devices.inventory.hosts) == 0:
        print(f"[{HEADER}] no device selected.")

    get_vrf(nr, options)
    data = devices.run(task=generic_bgp_get, on_failed=True, num_workers=10)
    if verbose_mode(user_value=os.environ.get("NETESTS_VERBOSE", NOT_SET),
                    needed_value=LEVEL4):
        print_result(data)
Beispiel #11
0
def get_mtu(nr: Nornir, filters={}, level=None, own_vars={}):

    devices = nr.filter()

    if len(devices.inventory.hosts) == 0:
        raise Exception(f"[{HEADER_GET}] no device selected.")

    data = devices.run(task=generic_mtu_get, on_failed=True, num_workers=10)
Beispiel #12
0
def InitNornir(
    config_file: str = "",
    dry_run: bool = False,
    configure_logging: Optional[bool] = None,
    **kwargs: Dict[str, Any],
) -> Nornir:
    """
    Arguments:
        config_file(str): Path to the configuration file (optional)
        dry_run(bool): Whether to simulate changes or not
        configure_logging: Whether to configure logging or not. This argument is being
            deprecated. Please use logging.enabled parameter in the configuration
            instead.
        **kwargs: Extra information to pass to the
            :obj:`nornir.core.configuration.Config` object

    Returns:
        :obj:`nornir.core.Nornir`: fully instantiated and configured
    """
    register_default_connection_plugins()

    if callable(kwargs.get("inventory", {}).get("plugin", "")):
        kwargs["inventory"]["plugin"] = cls_to_string(kwargs["inventory"]["plugin"])

    if callable(kwargs.get("inventory", {}).get("transform_function", "")):
        kwargs["inventory"]["transform_function"] = cls_to_string(
            kwargs["inventory"]["transform_function"]
        )

    conf = Config.load_from_file(config_file, **kwargs)

    data = GlobalState(dry_run=dry_run)

    if configure_logging is not None:
        msg = (
            "'configure_logging' argument is deprecated, please use "
            "'logging.enabled' parameter in the configuration instead: "
            "https://nornir.readthedocs.io/en/stable/configuration/index.html"
        )
        warnings.warn(msg, DeprecationWarning)

    if conf.logging.enabled is None:
        if configure_logging is not None:
            conf.logging.enabled = configure_logging
        else:
            conf.logging.enabled = True

    conf.logging.configure()

    inv = conf.inventory.plugin.deserialize(
        transform_function=conf.inventory.transform_function,
        transform_function_options=conf.inventory.transform_function_options,
        config=conf,
        **conf.inventory.options,
    )

    return Nornir(inventory=inv, config=conf, data=data)
Beispiel #13
0
def get_bond(nr: Nornir, options={}):

    devices = nr.filter()

    if len(devices.inventory.hosts) == 0:
        raise Exception(f"[{HEADER_GET}] no device selected.")

    data = devices.run(task=generic_bond_get,
                       filters=filters,
                       on_failed=True,
                       num_workers=10)
Beispiel #14
0
def nornir(request):
    """Initializes nornir"""
    dir_path = os.path.dirname(os.path.realpath(__file__))

    nornir = Nornir(
        inventory=SimpleInventory(
            "{}/inventory_data/hosts.yaml".format(dir_path),
            "{}/inventory_data/groups.yaml".format(dir_path),
        ),
        dry_run=True,
    )
    return nornir
 def test_processor(self, nornir: Nornir) -> None:
     data = {}
     nornir.with_processors([MockProcessor(data)]).run(task=mock_task)
     assert data == {
         "mock_task": {
             "started": True,
             "dev1.group_1": {
                 "started": True,
                 "subtasks": {},
                 "completed": True,
                 "failed": False,
             },
             "dev2.group_1": {
                 "started": True,
                 "subtasks": {},
                 "completed": True,
                 "failed": False,
             },
             "dev3.group_2": {
                 "started": True,
                 "subtasks": {},
                 "completed": True,
                 "failed": True,
             },
             "dev4.group_2": {
                 "started": True,
                 "subtasks": {},
                 "completed": True,
                 "failed": False,
             },
             "dev5.no_group": {
                 "started": True,
                 "subtasks": {},
                 "completed": True,
                 "failed": False,
             },
             "completed": True,
         }
     }
Beispiel #16
0
def get_ipv4(nr: Nornir, filters={}, level=None, own_vars={}):

    devices = nr.filter()

    if len(devices.inventory.hosts) == 0:
        raise Exception(f"[{HEADER_GET}] no device selected.")

    get_vrf(nr, save_vrf_name_as_list=True)

    data = devices.run(task=generic_ipv4_get,
                       filters=filters,
                       on_failed=True,
                       num_workers=10)
Beispiel #17
0
def get_vlan(nr: Nornir, options={}):

    devices = nr.filter()

    if len(devices.inventory.hosts) == 0:
        raise Exception(f"[{HEADER}] no device selected.")

    if (options.get('filters') is not None
            and options.get('filters').get("get_bond", True)):
        get_bond(nr=nr, options=options.get('filters'))

    data = devices.run(task=generic_vlan_get, options=options, on_failed=True)
    if verbose_mode(user_value=os.environ.get("NETESTS_VERBOSE", NOT_SET),
                    needed_value=LEVEL2):
        print_result(data)
Beispiel #18
0
def filtering(args: Namespace, norns: Nornir) -> Nornir:
    """
    Provide inventory filtering based on attributes from args.

    :param args: The populated Namespace object returned by argparser.parse_args()
    :param norns: An instantiated Nornir object with our full inventory for filtering
    :return:
    """

    print("Filtering Target Hosts")

    def is_cli_selected_host(host):
        return host.hostname in args.addresses

    if args.addresses:
        return norns.filter(filter_func=is_cli_selected_host)
    else:
        return norns
Beispiel #19
0
def get_bgp_up(nr: Nornir, filters={}, level=None, vars={}):

    devices = nr.filter()

    if len(devices.inventory.hosts) == 0:
        raise Exception(f"[{HEADER_GET}] no device selected.")

    get_bgp(nr)

    devices.run(task=check_if_all_bgp_sessions_are_up,
                on_failed=True,
                num_workers=10)

    return_value = True

    for device in devices.inventory.hosts:
        if devices.inventory.hosts[device].get(BGP_ALL_BGP_UP_KEY) is False:
            return_value = False

    return return_value
Beispiel #20
0
def InitNornir(config_file="",
               dry_run=False,
               configure_logging=True,
               **kwargs):
    """
    Arguments:
        config_file(str): Path to the configuration file (optional)
        dry_run(bool): Whether to simulate changes or not
        **kwargs: Extra information to pass to the
            :obj:`nornir.core.configuration.Config` object

    Returns:
        :obj:`nornir.core.Nornir`: fully instantiated and configured
    """
    register_default_connection_plugins()

    if callable(kwargs.get("inventory", {}).get("plugin", "")):
        kwargs["inventory"]["plugin"] = cls_to_string(
            kwargs["inventory"]["plugin"])

    if callable(kwargs.get("inventory", {}).get("transform_function", "")):
        kwargs["inventory"]["transform_function"] = cls_to_string(
            kwargs["inventory"]["transform_function"])

    conf = Config.load_from_file(config_file, **kwargs)

    data = GlobalState(dry_run=dry_run)

    if configure_logging:
        conf.logging.configure()

    inv = conf.inventory.plugin.deserialize(
        transform_function=conf.inventory.transform_function,
        config=conf,
        **conf.inventory.options,
    )

    return Nornir(inventory=inv, config=conf, data=data)
def execute_socket(nr: Nornir, filters={}, level=None, own_vars={}):

    devices = nr.filter()

    if len(devices.inventory.hosts) == 0:
        raise Exception(f"[{HEADER_GET}] no device selected.")

    path_url = f"{PATH_TO_VERITY_FILES}{SOCKET_SRC_FILENAME}"

    data_retrieve = devices.run(
        task=retrieve_socket_from_yaml,
        on_failed=True,
        num_workers=10
    )
    #print_result(data_retrieve)

    generate_cmd = devices.run(
        task=_generic_generate_socket_cmd,
        on_failed=True,
        num_workers=10
    )
    #print_result(generate_cmd)

    execute_cmd = devices.run(
        task=_execute_socket_cmd,
        on_failed=True,
        num_workers=10
    )
    #print_result(execute_cmd)

    socket_works = True
    for device in nr.inventory.hosts:
        if SOCKET_WORKS_KEY in nr.inventory.hosts[device].keys():
            if nr.inventory.hosts[device][SOCKET_WORKS_KEY] is False:
                socket_works = False

    return (not execute_cmd.failed and socket_works)
Beispiel #22
0
def nornir(request):
    """Initializes nornir"""
    nr = Nornir(inventory=inventory_from_yaml(),
                runner=SerialRunner(),
                data=global_data)
    return nr