How to use the nornir.core.filter.F function in nornir

To help you get started, we’ve selected a few nornir examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github twin-bridges / nornir_course / tests / test_class4.py View on Github external
def remove_ex2_flash_files():
    # prep to ensure test files do not exist on devices
    nornir_inventory = gen_inventory_dict("~/nornir_inventory/")
    nr = InitNornir(inventory=nornir_inventory, logging=NORNIR_LOGGING)
    eos = nr.filter(F(groups__contains="eos"))

    # remove test files from eos flash
    eos.run(task=networking.netmiko_send_command, command_string="terminal dont-ask")
    eos.run(
        task=networking.netmiko_send_command,
        command_string="delete flash:arista_zzzzz.txt",
    )
github nornir-automation / nornir / tests / core / test_filter.py View on Github external
def test_negate_or_both_negate(self, nornir):
        f = ~F(site="site1") | ~F(role="www")
        filtered = sorted(list((nornir.inventory.filter(f).hosts.keys())))

        assert filtered == [
            "dev2.group_1",
            "dev3.group_2",
            "dev4.group_2",
            "dev5.no_group",
        ]
github twin-bridges / nornir_course / bonus1 / exercises / exercise5 / exercise5a.py View on Github external
def main():
    nr = InitNornir(config_file="config.yaml")
    nr = nr.filter(F(groups__contains="nxos"))
    agg_result = nr.run(task=get_checkpoint_file)
    print_result(agg_result)
github twin-bridges / nornir_course / class4 / exercises / exercise2 / exercise2c.py View on Github external
def main():
    nr = InitNornir(config_file="config.yaml")
    nr = nr.filter(F(groups__contains="eos"))
    result = nr.run(task=file_copy, num_workers=10)
    print_result(result)
github twin-bridges / nornir_course / class4 / exercises / exercise2 / exercise2a.py View on Github external
def main():
    nr = InitNornir(config_file="config.yaml")
    nr = nr.filter(F(groups__contains="eos"))
    result = nr.run(task=file_copy)
    print_result(result)
github twin-bridges / nornir_course / class2 / exercises / exercise5 / exercise5b.py View on Github external
def main():
    nr = InitNornir(config_file="config.yaml")
    ios_filt = F(groups__contains="ios")
    nr = nr.filter(ios_filt)
    nr.inventory.hosts["cisco3"].password = "bogus"
    my_results = nr.run(task=netmiko_send_command, command_string="show ip int brief")
    print_result(my_results)
    print()
    print(f"Task failed hosts: {my_results.failed_hosts}")
    print(f"Global failed hosts: {nr.data.failed_hosts}")
    print()
github twin-bridges / nornir_course / class6 / exercises / exercise3 / exercise3b.py View on Github external
def main():
    LOGGER.info("Script initalizing...")
    nr = InitNornir(config_file="config.yaml")
    nr = nr.filter(F(groups__contains="eos"))
    for hostname, host_obj in nr.inventory.hosts.items():
        host_obj.password = PASSWORD
    LOGGER.critical("Passwords set, commence automation!")
    nr.run(
        task=networking.netmiko_send_command, command_string="show mac address-table"
    )
    LOGGER.error("Oh no! We're all out of automation tasks :(")
    LOGGER.debug("Hey, what are you still doing here?")
github twin-bridges / nornir_course / class2 / exercises / exercise5 / exercise5a.py View on Github external
def main():
    nr = InitNornir(config_file="config.yaml")
    ios_filt = F(groups__contains="ios")
    nr = nr.filter(ios_filt)
    my_results = nr.run(task=netmiko_send_command, command_string="show ip int brief")
    print_result(my_results)
github twin-bridges / nornir_course / class3 / exercises / exercise2 / exercise2.py View on Github external
print(arista1.inventory.hosts)
    print("-" * 20)

    print("\nExercise 2b")
    print("-" * 20)
    wan_devs = nr.filter(role="WAN")
    print(wan_devs.inventory.hosts)
    wan_devs = wan_devs.filter(port=22)
    print(wan_devs.inventory.hosts)
    print("-" * 20)
    # Alternatively:
    # wan_devs = nr.filter(role="WAN").filter(port=22)

    print("\nExercise 2c")
    print("-" * 20)
    sfo_f_obj = nr.filter(F(groups__contains="sfo"))
    print(sfo_f_obj.inventory.hosts)
    print("-" * 20)
    print()
github twin-bridges / nornir_course / class3 / exercises / exercise4 / exercise4.py View on Github external
def main():
    # Exercise 4a
    nr = InitNornir(config_file="config.yaml")
    # Exercise 4b
    nr = nr.filter(F(groups__contains="eos"))
    agg_result = nr.run(
        task=netmiko_send_command, command_string="show int status", use_textfsm=True
    )

    # Verify structured data (pick a device and check)
    print()
    print("Exercise 4b - verify structured data")
    print("-" * 20)
    print(type(agg_result["arista1"][0].result))
    print("-" * 20)

    print("\nExercise 4c - final dictionary")
    print("-" * 20)
    combined_data = {}
    for device_name, multi_result in agg_result.items():
        combined_data[device_name] = {}