Date created: 06/23/22 10:10:11. Last modified: 06/23/22 10:22:42

Nornir - Hello World

config.yaml

---
inventory:
    plugin: SimpleInventory
    options:
        host_file: "hosts.yaml"
runner:
    plugin: threaded
    options:
        num_workers: 1
logging:
  enabled: True

 

hosts.yaml

Note: After much trial and error, the below works but removing extras and everything doesn't, and extra's can't be empty, and option_args can't be empty. Also the password must have a password defined or it must be added to the config passed to Nornir before the connection attempt, it won't prompty for a password if none is specified, it will just try and connect with no password and fail, and throws a non-descript error.

---
lab01:
    hostname: 192.0.2.1
    username: user.name.here
    password: abc123
    port: 22
    platform: iosxr
    connection_options:
      napalm:
        extras:
          optional_args:
            dummy: foo

Potential option_args are:

            allow_agent: False
            ssh_strict: False
            ssl_verify: False
            use_keys: False
            look_for_keys: False
            timeout: 20
            auth_timeout: 20
            banner_timeout: 20
            conn_timeout: 20
            fallback: False

 

nornir_test.py

#!/usr/bin/env python3

"""
pip install nornir nornir_utils nornir_napalm invoke
"""

from nornir import InitNornir
from nornir.core.task import Task, Result
from nornir_utils.plugins.functions import print_result
from nornir_napalm.plugins.tasks import napalm_get, napalm_cli

import logging
logging.basicConfig(filename='test.log', level=logging.DEBUG)
logger = logging.getLogger("netmiko")

nr = InitNornir(config_file="config.yaml")
print(nr.inventory.hosts)

results = nr.run(
    task=napalm_cli,
    commands=["show version"]
)
print_result(results)