Table of Contents
Nornir inventory and multi-threading are the features that are very useful and handy in network automation and as we have discussed earlier, they are the main features of the python nornir library.
With inventory management, we keep a list of devices, device groups, and their properties in separate inventory files, rather than in the Python code itself.
Python script can run on a specific device, list of devices or group of devices configured already in inventory.
Multi-threading, another feature of the Nornir library, allows us to run Python scripts on multiple devices at the same time.
Python Nornir Simple Inventory Configuration
what is Inventory in Network Automation?
In order to connect to a device and automate it, we need the IP address of the device and the username and password to connect to the device.
It is also important to know which platform each device has. It is a Cisco IOS XE device, IOS XR device, or Juniper Junos device.
If you want to configure and automate multiple devices from your Python code, you need to run the script on the group name that those devices are a member of.
All of these information can be configured in inventory files and not in the Python code itself.
Python Nornir Simple Inventory
Simple Inventory is the embedded version of inventory management in Python Nornir that we will use throughout the course.
However, there are a few other inventory management options, which we introduce in summary in this section and in more detail in a specific and separate section.
In nornir simple inventory, we have three inventory files to list the devices, group of devices and their properties mostly defined in “hosts.yaml”, “groups.yaml” and “defaults.yaml”.
As the names suggest, the list of devices, the IP address of each device, the group names each device belongs to, and any other properties unique to each device are configured in the hosts.yaml inventory file.
Group names can be arbitrary names based on device type, device vendor, device geographic location, and any other types useful for automation infrastructure. A device can be a member of multiple groups.
All properties shared by devices in a group are configured in the groups.yaml inventory file.
As an an example. All devices with the same device type and vendor have the same platform. So the “platform” property can be configured in the groups.yaml file.
And finally, the properties shared by all devices are configured in defaults.yaml inventory file.
As an example, we’ll probably use the same username and password to connect to all devices. In this case, the username and password can be configured in the defaults.yaml inventory file.
A specific property can be configured in multiple inventory files. In this case hosts.yaml is preferred over groups.yaml and groups.yaml over defaults.yaml.
For example, if all devices have the same username and password, but they differ on a specific device, the username and password properties can be configured in defaults.yaml and will be overridden by the username and password in that specific device in hosts.yaml inventory file.
Python Nornir Simple Inventory Sample Configuration
These are inventory and config files that I have created already.
majid@majid-ubuntu:~/devnet/pyhton_nornir/2023/1.inventory_threading$ ls config.yaml defaults.yaml groups.yaml hosts.yaml
If we look at inside hosts.yaml, three devices, R1,R2 and R3 is created.
R1 and R2 devices are cisco devices but R3 is a juniper device. A group based on vendor and a group based on device type is created. All three devices are member of router group. At the same time, routers R1 and R2 are member of cisco group and R3 is member of juniper group.
For each device username and password is created but it is commented in all routers except router R3. It is because all devices except router R3 have the same username and password. Therefore it can be configured in defaults.yaml file. Only the username and password for router R3 is separately configured in hosts.yaml and it is preferred over what we configure in defaults.yaml.
majid@majid-ubuntu:~/devnet/pyhton_nornir/2023/1.inventory_threading$ cat hosts.yaml --- R1: hostname: "192.168.1.11" # username: "rayka" # password: "rayka-co.com" groups: - cisco - router R2: hostname: "192.168.1.12" # username: "rayka" # password: "rayka-co.com" groups: - cisco - router R3: hostname: "192.168.1.13" username: "RAYKA" password: "RAYKA-CO.COM" groups: - juniper - router
In “groups.yaml”, we configure properties shared by devices in each group.
Here all devices in cisco group have the same platform property. They are all based on cisco “ios” platform. And all juniper devices have the same “junos” platform. For router group no specific property is configured. But the group itself must be configured in groups.yaml inventory file.
Group names help also to run a script in all devices in the same group with calling just the group name inside the python code which we will see in the next sections.
majid@majid-ubuntu:~/devnet/pyhton_nornir/2023/1.inventory_threading$ cat groups.yaml --- cisco: platform: ios juniper: platform: junos router: {}
And finally inside defaults.yaml, default username, default password and default platform is configured. Any device or device group with different username, password or platform will be configured inside device specific section in hosts.yaml or group specific section in groups.yaml inventory file.
majid@majid-ubuntu:~/devnet/pyhton_nornir/2023$ cat defaults.yaml --- username: "rayka" password: "rayka-co.com" platform: "ios"
Other Nornir Inventory plugins
“SimpleInventory” is the embedded version of the Nornir library, details of which you can see in the Github Nornir project at this link.
But in addition to the simple inventory, there are also other inventory options to be used with Nornir.
The Nornir project website is https://nornir.tech/.
Under Projects -> Nornir -> and then Plugins you will find the list of plugins that are integrated into Nornir project.
There are different types of plugins. Between them there are some plugins than can be used as inventory inside nornir library.
One of the most popular inventory plugins is “nornir_netbox”, which we will implement at the end of this course. The other famous option is “nornir_ansible”.
Python Nornir Config file and Multi-threading
Another feature of Python Nornir library is the capability to automate multiple devices at once.
This feature is called “runner” in nornir and there are two types of runner in nornir.
“SerialRunner” in which each devices is automated after the other and serially. The other option is “ThreadRunner”, in which devices are automated using threads and simultaneously.
You can find the details of nornir runners in this link.
Configuration of which runner to use in the Nornir library is done through the Nornir configuration, “config.yaml” file.
majid@majid-ubuntu:~/devnet/pyhton_nornir/2023/1.inventory_threading$ cat config.yaml --- inventory: plugin: SimpleInventory options: host_file: "hosts.yaml" group_file: "groups.yaml" defaults_file: "defaults.yaml" runner: plugin: threaded options: num_workers: 10
In the “config.yaml” file and in the section “inventory -> plugin” line, we define the type of inventory to be used. We will be using the “SimpleInventory” plugin throughout the course, except for the section related to netbox inventory.
In the “options” section under Inventory, we define the name of inventory files that we use in Nornir projects that we have already created.
In the “runner” section of “config.yaml” file, we define the type of runner to be used in nornir projects. By default serial runner is used.
Here we enable “threaded”, which means nornir script will be run simultaneously over multiple devices.
The number of concurrent devices can be defined under “num_workers” options. The more devices you automate at the same time with Nornir, the more resources you need in the automation control machine.
You can download sample nornir inventory project and multi- threading from the this link.