What is Nornir?
The Nornir Python automation framework is written in Python and designed to be used for network automation tasks. It is much faster and gives you more flexibility than it’s friend Ansible.
Ansible, which is also written in Python is currently the most popular network automation tool in use today but it does have some limitations which Nornir has been written to address.
If you want to know the differences between Nornir and Ansible, in this video I try to answer the very common question of
“Nornir vs Ansible” Which is best?
Nornir allows you to utilise already tried and tested Python libraries such as Netmiko and Napalm to perform network automation directly from Python scripts.
You will need to be familiar with Python to use Nornir, however if you have a good appreciation of Python and are finding that tools like Ansible are slower than you want and are limiting you with certain tasks then Nornir is definitely the tool for you.
You can find all the Nornir documentation here:
If you want to learn Nornir by watching videos, the entire contents of this blog post are covered in this video.
If you like learning by reading scroll down!
In this tutorial I will take you from the very beginning. We will install Nornir, configure all the dependencies and run your first task.
How to install Nornir
Nornir needs to be installed on Linux and for this tutorial I am using an Ubuntu 18.04 desktop image.
You simply run the command pip3 install nornir
Once it’s installed you can verify it’s there with the command pip3 freeze which will list all your installed libraries.
nornir==2.4.0 should be there (or later version)
Now we have Nornir installed we need to setup a few things. We need to create four files.
You can download the files here from my Github
Let’s start with config.yml this file contains all the information about where the other three files are and you also specify how many hosts you want Nornir to hit at once.
For this tutorial I have created a folder called Nornir on my machine and we will create the config.yml in the root of that folder.
## Nornir Config File --- core: num_workers: 100 inventory: plugin: "nornir.plugins.inventory.simple.SimpleInventory" options: host_file: "/inventory/hosts.yml" group_file: "/inventory/groups.yml" defaults_file: "/inventory/defaults.yml"
You will need to change the file path if you have your files elsewhere.
Within this file the num_workers specifies how many devices you want Nornir to connect to at once.
Then the inventory plugin is defined and under the options you specify where the other three files are.
Then I have created a folder called inventory where I have the other three files.
One of the most important files within Nornir is the inventory or hosts file, this lists all the devices that you want to connect to and is written in YAML. Inventory management is a big feature of Nornir!
I have saved the file as hosts.yml
The example file below contains 2 devices.
# Nornir Hosts File --- CSR1: hostname: 192.168.1.220 groups: - CSR_Routers CSR2: hostname: 192.168.1.221 groups: - CSR_Routers
This file contains the bare minimum you need to run Nornir which is a hostname and a group. I have defined a group called CSR_Routers and put both routers in that group
The next file is groups.yml in this file you can specify data that is relevant to that group. Nornir operates a very hierarchical structure so values are inherited down so anything in defaults affects all devices, and then anything in groups affects devices in that group only.
My groups file looks like this
## Nornir Groups File --- CSR_Routers: data: ntp: servers: - 18.104.22.168
I have only specified one piece of data an NTP server
The final file is the defaults.yml
This file contains all the information that is relevant to all devices. Any value in here can be overridden by specifying it lower down the tree i.e in the groups file or even down to a host file level.
My defaults file contains the platform and username and password.
## Nornir Defaults File --- platform: ios username: roger password: cisco
So that’s it! We have all the files created let’s see if it works?
For this example I am using EVE-NG which is running 5 x CSR Routers
Now whilst you could do all of this with pure Python and use the libraries Netmiko and Napalm yourself. Nornir takes all the hard work out of that and lets you use Python but within a framework so you can benefit from the inventory and the native use of Netmiko behind the scenes.
This gives you the benefit of using Python for Network Automation within the Nornir Framework which gives you the benefits of inventory and connection handling.
So now we have all the pieces in place we need one more file and that is the Python script we are going to run.
Let’s create that – I am going to call mine start.py this is saved in my Nornir directory.
This file will reference the config.yml file we created earlier, import a few libraries and then run the commands we want using Netmiko
from nornir import InitNornir
nr = InitNornir("config.yml")
from nornir.plugins.tasks.networking import netmiko_send_command
from nornir.plugins.functions.text import print_result
result = nr.run(netmiko_send_command, command_string="sh ip int brief")
This file imports the relevant libraries from Nornir and then specifies the commands we are going to send to our routers. In this case sh ip int brief
So running this Python script will do a sh ip int brief on each router and then print the results to the screen.
Let’s drop into our command line and run the Python script!
Just type python3 start.py and you should see this
If you want to find out more about Nornir and what it can do check out my upcoming course here:
Frequently asked questions
What is Nornir?
Nornir is a Python Automation Framework, it utilises already established Python libraries like Netmiko and Napalm to perform network automation tasks. Allowing you to focus on writing Python scripts for your tasks, whilst letting Nornir take care of the inventory and connectivity for devices.
Is Nornir better than Ansible?
Nornir is faster than Ansible and gives you more flexibility as you are writing tasks directly in Python. To say if it’s better than Ansible is a matter of opinion and dependant on your skills with Python. Ansible is easier to use and get started with but as you progress
What does a network automation engineer do?
Network engineers of the future are going to have to become proficient in using Python, Git, Nornir Ansible and to learn coding to be able to do their jobs. A network automation engineer is someone who has embraced these skills and is using them in their daily job to improve efficiency and productivity when configuring network devices.
So hopefully at this point you should have been able to connect to a few devices and run a simple command using Nornir!