• Skip to main content
  • Skip to header right navigation
  • Skip to site footer

Roger Perkin

Learn Network Automation

  • Network Automation
    • Ansible Network Automation
      • Ansible Hosts File
      • What is Ansible?
    • Python Network Automation
      • Python for Network Engineers
    • Python Tutorial for Beginners
    • Terraform
    • pyATS
    • Docker
    • GIT
  • Cisco
    • ISE
    • SD WAN Training
    • Password Recovery
    • Software-Upgrade-Guides
    • BGP
    • Data Center
    • WIRELESS
  • CCIE
  • Blog
  • About
    • My Red Special Guitar
  • COURSES

Nornir Training – Learn the Python Automation Framework

Home » Network Automation » Python Network Automation

getting started with the nornir python automation framework

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.

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 course banner

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

Installing Nornir the Python Automation Framework using 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.

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

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 training course banner

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.

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!

Page Contents

  • What is Nornir?
  • Nornir Training Video
  • Nornir Course
    • How to install Nornir
    • Nornir hosts.yaml
    • Nornir groups.yaml
    • Nornir defaults.yaml
  • Nornir Example
  • Frequently asked questions
    • What is Nornir Python?
    • Is Nornir better than Ansible?
    • What does a network automation engineer do?
  • Conclusion
Category: Python Network Automation Tutorials by CCIE #50038
Previous Post: « ansible vs python featured image Ansible vs Python Scripts
Next Post: Skype Camera Not Working Windows 10 »

Reader Interactions

Comments

  1. Serena Martin

    March 23, 2020 at 6:22 am

    This provided information is really so nice, thanks for giving this post and the more skills to develop

    Reply
  2. Mithun

    September 10, 2020 at 12:44 am

    do you have the full course for nornir

    Reply
    • Roger Perkin

      September 11, 2020 at 9:56 am

      Hi Mithun, I am currently recording it. Should be completed by the end of September 2020

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Sidebar

Hi I'm Roger Perkin,
Based in the UK working as a Technical Architect focussed on Network Automation CCIE #50038
About Roger | Twitter | Linkedin

More Python

  • Python Network Automation
  • pyATS Tutorial for Beginners
  • Python for Network Engineers Course
  • Python Network Automation Certification
  • Python for Network Engineers Scripts
  • Python VENV Tutorial

Recent Posts

  • Hashicorp Vault Tutorial
  • Ansible Tower vs Ansible Automation Platform
  • Cisco Certified DevNet Expert Getting Started Guide
  • Python for Network Engineers Course
  • Gitlab vs Github the Differences Explained

Topics

Network Automation
Ansible
Python for Network Automation
CCIE
Cisco ISE
F5 Certification
BGP
OSPF

Git for Network Engineers

Ansible vs Nornir

Start learning today with my Network Automation Courses

Master Ansible, Python, Git, Nornir, Jenkins and more..


Buy me a coffeeBuy me a coffee

ansible training course

Have you seen my YouTube Channel?

YouTube Subscribe

Let’s get started

Take a look at my premium courses on Ansible, Nornir & Git or buy them all with the Network Automation Bundle!

Network Automation Courses

Navigation

Home

Blog

About

Contact

Network Tools

Python VENV Tutorial

Contact

Get in touch with me here

[email protected]

Buy me a coffeeBuy me a coffee

YouTube

Don’t forget to take a look at my YouTube Channel

youtube button

Tech

Best Vertical Mouse for RSI

Copyright © 2022 · Roger Perkin · All Rights Reserved · Powered by Mai Theme