CLI based Network Automation using Python Nornir


CLI based network automation using python nornir based on cisco devices and python version 3.7+, covers the concept and implementation of network automation with practical examples using nornir scrapli, netmiko and napalm plugins and some other important topics. how to use jinja2 template in network automation, how to filter devices to apply automation, how to avoid clear text password and …

– With the purchase of this course, the first part of the videos can be downloaded immediately. The second part of the videos will be processed exclusively for you and will take 2 working days.
– You will also get access to the protected lessons of the purchased course.


CLI based Network Automation Introduction 

Network Automation Tools help us to automate monitoring and configuration of network devices which can be useful especially in a large scale network environments.

As a network engineer, I classify automation tools into two main categories. Those based on native Python programming language which bring mainly flexibility and those not based on Python which bring mainly simplicity.

There are two categories of Python-based network automation tools. Those through which we send CLI commands to monitor and configure network devices and those that are based on the YANG data structure.

The target of this course is to automate network devices based on CLI-based Python automation libraries. Therefore, our comparison mainly focuses on the tools classified in this category.

YANG-based python automation tools will be discussed in another course.

Network automation with Ansible, a non-Python based automation tool, is already exist in the list of RAYKA courses.

Network Automation Lab requires at least one controller to manage devices and one device to be managed.

We will be using Ubuntu with Visual Studio Code and Python as the controller and a Cisco IOS XE device managed through the controller throughout the course.

In this section, you will learn how to prepare these components for the rest of the course.

Python Nornir Inventory Managament and Multi-Threding

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 Netmiko Plugin

Nornir netmiko is the first plugin that we’ll be discussing to send command to the router to monitor something.

In each plugin we have different tasks to send monitoring commands and configuration commands to the device.

In this section we have a configuration example to send monitoring commands via nornir netmiko plugin. The next section covers sending configuration commands via nornir netmiko.

Nornir netmiko send config plugin is used to send configurations to the network devices.

In the previous section nornir netmiko send command was used to send monitoring commands to the network devices, but this section uses the same plugin to send configuration commands.

Python Nornir Scrapli Plugin

Nornir scrapli is another nornir plugin that we can use to send monitoring and configuration commands.

Nornir scrapli send command is used to send monitoring and troubleshooting commands which will be discussed in this section.

Nornir scrapli configuration commands is used to send configuration commands which will be discussed in the next section.

Nornir scrapli send config tasks allows us to change configuration of network devices. It has the capability to send a single command, send multiple commands via a list, and send multiple commands via a file.
Make Configuration Data and Configuration Itself Seperate in Nornir

Network automation data structure for separating data and configuration is what we discuss in this module.

Each configuration has some data that may be different or the same between devices. SNMP community, IP address of NTP servers, network addresses in EIGRP configuration and IP address and AS number of neighbors in BGP configuration are some examples of data in network automation.

How to store and access the data of the configuration of various network devices is what we call network automation data structure

In this section, we use inventory files to store and access the configuration data as well.

But in the next section we will store and access data of configurations via its own specific data structure.

Nornir data structure in network automation gives us the ability to separate the data configuration from the configuration itself and also from the automation Python code.

In the previous section, we used Nornir inventory files to keep also configuration data. In this section we use Nornir specific data structure to store and acces configuration data.

Python Nornir Napalm Plugin

Nornir Napalm getter and Send Commands are two methods to send monitoring and troubleshooting commands to network devices in napalm plugin.

Nornir Napalm getters are vendor independent show commands common to all supported vendors and it is invented by Napalm. This is the topic of this section.

Nornir napalm send command is normal CLI command that we send to network devices when no getter is provided and it is the topic of the next section.

We also discuss the structured output of Napalm getter and CLI commands and how it differs from Netmiko and Scrapli plugin.

Nornir Napalm CLI commands gives the capability to send normal monitoring and troubleshooting commands to network devices, especially when no getter is supported.

Nornir Napalm structured output is another benefit of the Napalm plugin, which gives the capability to parse the output of show commands without using regular expressions.

structured ouput applies to both napalm getters and CLI commands.

Nornir Napalm send configuration through “napalm_configure” task is what we demonstrate in this section.

We will also discuss the difference between “merge” and “replace” when sending the configuration to network devices.

“Napalm backup config” is the keyword.

Automating config backup and restore is one of the most important applications of network automation that is the topic of this section.

We will learn how to backup configuration of network devices using Nornir getter, “napalm_get” from “nornir_napalm” plugin and “write_file” from “nornir_utils” plugin and also restore the configuration to the network devices using “napalm_configure” with “replace” option from “nornir_napalm” plugin.

Nornir napalm replace config is the topic of this section, in which we replace the configuration of network devices instead of the default merge behavior.

Nornir Napalm validate task is used to automate compliance check of configuration of network devices.

Compliance check means to make sure that the network device have compatible configuration for example in DNS and NTP servers.

avoid clear text passwords in nornir inventory

Avoid clear-text passwords in Nornir inventory, which we didn’t consider from the start of the course, but which in practice must be considered as a very important security concern.

In this section, we will learn the methods to avoid storing clear text passwords in Nornir inventory files.

Python sys argv or system arguments are used to get the arguments from the command line in Python scripts.

We use system arguments in network automation to get the password of network devices from the command line to avoid storing clear text passwords in Nornir inventory files.

GPG process to encrypt password is another method to avoid clear text password in nornir inventory files or python script.

In this section we will learn to use GPG to encrypt passwords used to connect to network devices in network automation.

Public key authentication is another method of authentication in which we do not need to give any password. Therefore there is no need to store any clear text password in network automation inventory or script.

In this section we will implement public key authentication on cisco IOS device and use nornir scrapli plugin to connect to devices through public key authentication.

Nornir Inventory Filter

Nornir filter inventory gives the capability to limit the execution of automation scripts on a specific network device or group of network devices.

In this section we will learn about two different types of Nornir filter, basic filtering using the filter method and advanced filtering using the F object.

Text parsers to structure output of Nornir send commands

Using google TextFSM parser in Nornir automation scripts helps us get structured output when sending show monitoring commands to network devices from which any information can be easily extracted.

In this section we show how to use textfsm in nornir Netmiko and Scrapli plugins.

In the next section, we will introduce the Genie parser, which belongs to the Cisco Company.

Cisco genie parser in network automation gives the capability to convert the output of monitoring commands into a structured JSON output where any specific information can be easily extracted without using regular expressions.

In this section, we demonstrate the genie parser in the nornir netmiko and nornir scrapli automation scripts.

    23.1. Genie Dq to easily query pthon dirctionary
Genie Dq (Dictionary Query) is a Python pyATS library and part of genie.utils to easily query a python dictionary without having to traverse through the dictionary to get a value.
Python Jinja2 Template

Python Jinja2 template in the simplest form is a file containing the configuration of devices, but using variables instead of configuration data that differ between devices.

Python jinja2 template help us to keep the configuration and data separate from the automation script.

In this section, we will learn how to create and use a simple jinja2 template configuration file alongside any Python Nornir automation script to make the automation more professional.

In the next section, we will introduce more advanced capability of jinja2 template configuration file.

Python Jinja2 template also has loops, conditionals, and some other features in addition to the variable substitution discussed in the previous section.

In this section, we demonstrate a jinja2 configuration template with all these features.

Netbox as Inventory Source in Network Automation
Nornir Netbox Inventory is another solution for inventory management in Nornir based network automation.
In the previous section, we installed Netbox and added inventory information.
In this section, we read netbox inventory information through Nornir and then send monitoring commands to network devices that have already been read from the netbox inventory.
Netbox is another Nornir Inventory solution in network automation.
In this section we will install Netbox on Ubuntu 22.04 and prepare it with the minimum configuration to be used as an inventory source in Nornir-based network automation.
In the next section, we’ll use Netbox as an inventory source in the Nornir automation script.


There are no reviews yet.

Be the first to review “CLI based Network Automation using Python Nornir”

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

Post comment