Download as pdf or txt
Download as pdf or txt
You are on page 1of 24

Nornir Training – Learn the Python Automation

Framework
You are here: Home / Network Automation Blog / Network Automation with Python / Nornir Training – Learn the Python Automation Framework

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Get my Free guide on
getting started with
Network Automation

Your rst name

Your email address

Send me the guide

We respect your privacy. Unsubscribe


Page Contents [show] at anytime.

Powered By ConvertKit

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 exibility 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

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
and Ansible, in this video I try to answer the very
common question of Get my Free guide on
“Nornir vs Ansible“ Whichgetting
is best?started with
Network Automation

Your rst name

Your email address


Nornir allows you to utilise already tried and tested Python libraries such as Netmiko and Napalm to
perform network automation directly from Python scripts.
Send me the guide
You will need to be familiar with Python to use Nornir, however if you have a goodWe
appreciation of Python
respect your privacy. Unsubscribe
and are nding that tools like Ansible are slower than you want and are limiting you with certain tasks then
at anytime.

Nornir is de nitely the tool for you. Powered By ConvertKit

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 nd all the Nornir documentation here:


https://nornir.readthedocs.io/en/latest

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Nornir Training Video Get my Free guide on
getting started with
If you want to learn NornirNetwork Automation
by watching videos, the
entire contents of this blog post are covered in this
video. Your rst name

Your email address

Send me the guide

We respect your privacy. Unsubscribe


at anytime.

If you like learning by reading scroll down! Powered By ConvertKit

In this Nornir demo I will take you from the very beginning. We will install Nornir, con gure all
the dependencies and run your rst task.

If you are looking for a Nornir training course check the link below.

Nornir Course
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Get my Free guide on
getting started with
Network Automation

Your rst name

Your email address

How to install Nornir Send me the guide

Nornir needs to be installed on Linux and for this tutorial I am using an Ubuntu 18.04 desktop
We respect image.Unsubscribe
your privacy.
at anytime.

You simply run the command pip3 install nornir Powered By ConvertKit

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Get my Free guide on
getting started with
Network Automation

Your rst name

Your email address

Send me the guide


Installing Nornir the Python Automation Framework using pip3 install nornir We respect your privacy. Unsubscribe
at anytime.

Once it’s installed you can verify it’s there with the command pip3 freeze which will list all yourByinstalled
Powered ConvertKit

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 les.

defaults.yml
groups.yml
hosts.yml

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
con g.yml
Get my Free guide on
getting started with
You can download the les here from my Github Network Automation

Your rst name


Let’s start with con g.yml this le contains all the information about where the other three les are and you
also specify how many hosts you want Nornir to hit at once. Your email address

For this tutorial I have created a folder called Nornir on my machine and we will create the con g.yml in the
root of that folder. Send me the guide

We respect your privacy. Unsubscribe


## Nornir Config File at anytime.
---
Powered By ConvertKit
core:

num_workers: 100

inventory:

plugin: "nornir.plugins.inventory.simple.SimpleInventory"

options:

host_file: "/inventory/hosts.yml"

group_file: "/inventory/groups.yml"

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
defaults_file: "/inventory/defaults.yml"
Get my Free guide on
getting started with
You will need to change the le path if you have your les elsewhere. Network Automation

Within this le the num_workers speci es how many devices you want Nornir to connect to at once.
Your rst name

Then the inventory plugin is de ned and under the options you specify where the Your
other three les are.
email address

Then I have created a folder called inventory where I have the other three les.

Send me the guide

Nornir hosts.yaml We respect your privacy. Unsubscribe


at anytime.

One of the most important les within Nornir is the inventory or hosts le, this lists all thePowered
devices that you
By ConvertKit

want to connect to and is written in YAML. Inventory management is a big feature of Nornir!

I have saved the le as hosts.yml

The example le below contains 2 devices.

# Nornir Hosts File


---
CSR1:

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
hostname: 192.168.1.220

groups: Get my Free guide on


getting started with
- CSR_Routers Network Automation

CSR2:
Your rst name

hostname: 192.168.1.221
Your email address
groups:

- CSR_Routers
Send me the guide

We respect your privacy. Unsubscribe


This le contains the bare minimum you need to run Nornir which is a hostname and a group. I have
at anytime.
de ned a group called CSR_Routers and put both routers in that group
Powered By ConvertKit

Nornir groups.yaml
The next le is groups.yml in this le 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 le looks like this

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
## Nornir Groups File
---
Get my Free guide on
CSR_Routers:
getting started with
Network Automation
data:

ntp: Your rst name

servers:
Your email address

- 1.1.1.1

Send me the guide


I have only speci ed one piece of data an NTP server
We respect your privacy. Unsubscribe
at anytime.

Nornir defaults.yaml Powered By ConvertKit

The nal le is the defaults.yml

This le 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 le or even down to a host le level.

My defaults le contains the platform and username and password.

## Nornir Defaults File


---

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
platform: ios
Get my Free guide on
username: roger getting started with
Network Automation
password: cisco

Your rst name


So that’s it! We have all the les created let’s see if it works?
Your email address
For this example I am using EVE-NG which is running 5 x CSR Routers

Send me the guide

We respect your privacy. Unsubscribe


at anytime.

Powered By ConvertKit

Nornir Example
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
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
Get my so youguide
Free can on
bene t from the inventory and the native use of Netmiko behind the scenes. getting started with
Network Automation

This gives you the bene t of using Python for Network Automation within theYour
Nornir Framework
rst name
which gives you the bene ts of inventory and connection handling.
Your email address

So now we have all the pieces in place we need one more le and that is the Python script we are going to
run. Send me the guide

We respect your privacy. Unsubscribe


Let’s create that – I am going to call mine start.py this is saved in my Nornir directory.
at anytime.

This le will reference the con g.yml le we created earlier, import a few libraries and then runBythe
Powered ConvertKit

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")

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
print_result(result)
Get my Free guide on
getting started with
This le imports the relevant libraries from Nornir and then speci es the commands we are going to send
Network Automation
to our routers. In this case sh ip int brief
Your rst name
So running this Python script will do a sh ip int brief on each router and then print the results to the screen.
Your email address
Let’s drop into our command line and run the Python script!

Just type python3 start.py and you should see this Send me the guide

We respect your privacy. Unsubscribe


at anytime.

Powered By ConvertKit

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Get my Free guide on
getting started with
Network Automation

Your rst name

Your email address

Frequently asked questions Send me the guide

We respect your privacy. Unsubscribe

What is Nornir?
at anytime.

Powered By ConvertKit

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 exibility 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

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
What does a network automation engineer
Get do?
my Free guide on
getting started with
Network engineers of the future are going to have to become pro cient in using Python,
NetworkGit,Automation
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 ef ciency andYour
productivity
rst name when

con guring network devices.


Your email address

Conclusion Send me the guide

So hopefully at this point you should have been able to connect to a few devices and run a your
We respect simple
privacy. Unsubscribe

command using Nornir! at anytime.

Powered By ConvertKit

 Share  Tweet S 0

Filed Under: Network Automation with Python

About Roger Perkin


Hi I'm Roger
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Hi I'm Roger.
CCIE #50038
Currently based in the UK working for a Cisco Gold Partner as a Senior Network & Security
Get my Free guide on
getting started with
Consultant. Network Automation
About Roger | Twitter | Linkedin
Your rst name

Your email address

Send me the guide

Get started with Network Automation We respect your privacy. Unsubscribe


at anytime.

Powered By ConvertKit
Sign up to my FREE
Your rst name
7 Day Network Automation
Course
Your email address
You will receive one short lesson each
day to get you started on the road to
network automation Subscribe

We respect your privacy. Unsubscribe at any


time.

Powered By ConvertKit

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Get my Free guide on
getting started with
Network Automation

Your rst name

Your email address

Send me the guide

Comments
We respect your privacy. Unsubscribe
at anytime.

Powered By ConvertKit

Serena Martin says

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

Mithun says
September 10, 2020 at 12:44 am

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
do you have the full course for nornir
Get my Free guide on
Reply getting started with
Network Automation

Roger Perkin says


Your rst name
September 11, 2020 at 9:56 am

Your email address


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

Reply
Send me the guide

We respect your privacy. Unsubscribe


at anytime.

Powered By ConvertKit

Leave a Reply
Your email address will not be published. Required elds are marked *

Comment

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Get my Free guide on
getting started with
Network Automation
Name *
Your rst name

Your email address

Email *

Send me the guide

We respect your privacy. Unsubscribe


at anytime.
Website

Powered By ConvertKit

Post Comment

Hi I'm Roger.
CCIE #50038

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Currently based in the UK working for a Cisco Gold Partner as a Senior Network & Security Consultant.
About Roger | Twitter | Linkedin
Get my Free guide on
getting started with
Network Automation

Your rst name

Your email address

Send me the guide

We respect your privacy. Unsubscribe


at anytime.

Powered By ConvertKit

Get my tips on how to survive studying for the CCIE Lab Exam
This is not a technical guide. These tips are from my journey to become a CCIE. Learn from my mistakes on how to survive studying and
preparing for the Lab Exam.

Claim your Guide

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Get my Free guide on
Recent Posts getting started with
Network Automation
Should Network Engineers Learn Python?
pyATS Genie Tutorial Your rst name

7 Best Python Network Automation Videos on YouTube


Your email address

Send me the guide

We respect your privacy. Unsubscribe


at anytime.

Powered By ConvertKit

Git for Network Engineers

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Get my Free guide on
getting started with
Network Automation

Your rst name

Your email address

Send me the guide

We respect your privacy. Unsubscribe


at anytime.

Powered By ConvertKit

Ansible vs Nornir

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Get my Free guide on
getting started with
Network Automation

Your rst name

Your email address

Send me the guide

We respect your privacy. Unsubscribe


at anytime.

Powered By ConvertKit

Start learning today with my Network Automation


Courses COURSES

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

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Get my Free guide on
getting started with
Network Automation
About Roger CCIE Blog

I'm Roger, a UK based network consultant My CCIE Journey Your rst name
currently working for a Cisco Gold Partner. My CCIE Study Plan
I am CCIE #50038 and am a Network Cisco Continuing
Your email address
Automation envalgelist. Education Program

ROUTING
PROTOCOLS Send me the guide

BGP We respect your privacy. Unsubscribe


OSPF at anytime.

Powered By ConvertKit
Network Tools

Airconsole Bluetooth
Console Cable

Copyright © 2020 Roger Perkin. All rights reserved. Return to top

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD

You might also like