Nornir Python Automation Framework
What is Nornir?
Nornir is a Python based automation framework, it uses an inventory of hosts on your network and then executes tasks against those devices, Nornir is agentless and open source. 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.
Nornir vs 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
“Ansible vs Python“ 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.
Written by David Barroso with some help from Kirk Byers, Nornir allows you to use any feature available to you within Python to perform your network automation.
You can find all the Nornir documentation here:
https://nornir.readthedocs.io/en/latest
Nornir Training Video
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 Nornir demo I will take you from the very beginning. We will install Nornir, configure all the dependencies and run your first task.
If you are looking for a Nornir training course check the link below.
Nornir Course
Nornir Basics
- How to Install Nornir
- Nornir Components
- Nornir Inventory
- Running a Task
Nornir Inventory
- Static Inventory
- Dynamic Inventory
- Basic Inventory Filtering
- Advanced Filtering
Nornir Tasks
- How to run a task
- Build a task from scratch
- Netmiko Configuration
Nornir Config File
Nornir Network Automation
How to install Nornir
Nornir needs to be installed on Linux and for this Nonir 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.
- defaults.yml
- groups.yml
- hosts.yml
- config.yml
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.
Inventory
Then I have created a folder called inventory where I have the other three files.
Nornir hosts.yaml
One of the most important files within Nornir is the inventory or hosts file, (check this guide on the Ansible 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
Nornir groups.yaml
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: - 1.1.1.1
I have only specified one piece of data an NTP server
Nornir defaults.yaml
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
Nornir Example Python
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")
print_result(result)
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
Nornir vs Ansible
Nornir is not Ansible whilst they do perform the same tasks, Nornir is a pure Python application whilst Ansible is written in Python but requires you write your automation in it’s DSL (Domain Specific Language) which is YAML.
Ansible does have a lower barrier to entry, but if you are serious about your automation and speed then you need to look into Nornir.
Nornir Course
Nornir Platform List
Frequently asked questions
What is Nornir Python?
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.
Nornir is a Python-based automation framework that is commonly used in network automation and infrastructure as code. Nornir provides a set of Python libraries that can be used to automate network tasks, such as provisioning, configuring, and testing network devices. It allows you to define tasks as Python functions, which can be easily reused and integrated into larger automation workflows.
One of the main benefits of Nornir is that it is highly customizable and extensible. Nornir provides a flexible architecture that allows you to define and configure tasks and workflows to meet your specific needs. Additionally, Nornir integrates well with other Python libraries and tools, which allows you to build complex and sophisticated automation workflows that leverage the power of Python.
Nornir is similar to other popular network automation frameworks such as Ansible and Salt, but it is designed specifically for use with Python. If you are comfortable with Python, Nornir can provide a flexible and customizable framework for building network automation workflows. However, if you are not familiar with Python, Nornir may have a steeper learning curve compared to other network automation frameworks like Ansible.
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.
Conclusion
So hopefully at this point you should have been able to connect to a few devices and run a simple command using Nornir!
Serena Martin
This provided information is really so nice, thanks for giving this post and the more skills to develop
Mithun
do you have the full course for nornir
Roger Perkin
Hi Mithun, I am currently recording it. Should be completed by the end of September 2020