How to use the nornir.plugins.tasks.networking 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 nornir-automation / nornir / tests / plugins / tasks / networking / test_napalm_configure.py View on Github external
def test_napalm_configure_change_commit(self, nornir):
        opt = {"path": THIS_DIR + "/test_napalm_configure_change_commit/step1"}
        configuration = "hostname changed-hostname"
        d = nornir.filter(name="dev3.group_2")
        d.run(connections.napalm_connection, optional_args=opt)
        result = d.run(
            networking.napalm_configure, dry_run=False, configuration=configuration
        )
        assert result
        for h, r in result.items():
            assert "+hostname changed-hostname" in r.diff
            assert r.changed
        opt = {"path": THIS_DIR + "/test_napalm_configure_change_commit/step2"}
        d.run(connections.napalm_connection, optional_args=opt)
        result = d.run(
            networking.napalm_configure, dry_run=True, configuration=configuration
        )
        assert result
        for h, r in result.items():
            assert "+hostname changed-hostname" not in r.diff
            assert not r.changed
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 twin-bridges / nornir_course / class4 / exercises / exercise5 / exercise5b.py View on Github external
nr = InitNornir(config_file="config.yaml")
    nr = nr.filter(name="arista4")

    # Current running config
    agg_result = nr.run(
        task=networking.napalm_get, getters=["config"], retrieve="running"
    )
    arista4_result = agg_result["arista4"][0].result
    arista4_running_config = arista4_result["config"]["running"]  # noqa

    # New config
    config = """
interface loopback123
  description verycoolloopback
    """
    agg_result = nr.run(task=networking.napalm_configure, configuration=config)
    print_result(agg_result)
github twin-bridges / nornir_course / class6 / exercises / exercise5 / exercise5b.py View on Github external
task.run(task=networking.netmiko_send_command, command_string=cmd)
    except NornirSubTaskError as e:
        if isinstance(e.result.exception, NetMikoAuthenticationException):
            # Remove the failed task (so ultimately the Nornir print output is cleaner)
            task.results.pop()

            # For failed devices reset the password to the correct value using environment var
            task.host.password = os.environ["NORNIR_PASSWORD"]

            # Force Nornir to close stale connections
            try:
                task.host.close_connections()
            except ValueError:
                pass

            task.run(task=networking.netmiko_send_command, command_string=cmd)
        else:
            return f"Unhandled exception: {e}"
github twin-bridges / nornir_course / class6 / exercises / exercise1 / exercise1a.py View on Github external
def send_command(task):
    task.run(
        task=networking.netmiko_send_command,
        command_string="set cli complete-on-space off",
    )
    task.run(task=networking.netmiko_send_command, command_string="show ip interface")
github twin-bridges / nornir_course / class6 / exercises / exercise1 / exercise1b.py View on Github external
def send_command(task):
    task.run(
        task=networking.netmiko_send_command,
        command_string="set cli complete-on-space off",
    )
    mul_result = task.run(
        task=networking.netmiko_send_command, command_string="show ip interface"
    )
    if "syntax error" in mul_result.result:
        raise ValueError("Invalid Junos command")
github twin-bridges / nornir_course / bonus1 / exercises / exercise2 / exercise2a.py View on Github external
def main():
    nr = InitNornir(config_file="config_a.yaml")
    nr = nr.filter(~F(name__contains="localhost"))
    agg_result = nr.run(
        task=networking.netmiko_send_command, command_string="show version"
    )
    print_result(agg_result)
github twin-bridges / nornir_course / class4 / collateral / custom_tasks_and_results / custom_tasks_p4.py View on Github external
def my_task(task):
    changed = False
    if task.host.groups[0] == "ios":
        cmd = "show run | i hostname"
        task.run(task=networking.netmiko_send_command, command_string=cmd)
        changed = True
    elif task.host.groups[0] == "junos":
        cmd = "show configuration system host-name "
        task.run(task=networking.netmiko_send_command, command_string=cmd)
        changed = True
    return Result(host=task.host, changed=changed, failed=False, result="ran command")