Nornir Fundamentals and Configuration

Pedro DPedro D
4 min read

Introduction to Network Automation

This chapter delves into the fundamentals of network automation with Nornir, a powerful Python-based framework designed to simplify and accelerate network management operations.

Nornir empowers network engineers to automate a wide range of tasks, including device configuration, inventory management, troubleshooting, and security audits. By leveraging the power of Python and its rich ecosystem of libraries, Nornir provides a flexible and extensible platform for automating complex network operations.

Understanding Nornir

Nornir is a multi-threaded network automation framework that simplifies inventory and task execution. Its multi-threaded design allows it to manage the configuration of multiple network devices at the same time (default is 20), unlike native Netmiko, which processes devices one by one. It uses a separate thread for each host in the inventory to execute tasks concurrently and track their progress.

Nornir tasks perform actions on the data. Tasks can include shared community plugins, your own custom plugins, or native Python code. The Nornir framework connects the inventory (data) and tasks, allowing them to run on a subset of devices while managing data, parallelization, and tracking the output and/or errors.

There are network plugins for connecting to devices (nornir-napalm, etc.), inventory plugins for building the inventory (nornir-netbox, nornir-.), and nornir-utils, which contains some former core tasks like print_result and load_yaml.

Key Components of Nornir

  • Inventory: A central repository that defines the network devices and their connection details.

  • Plugins: Community-contributed plugins that are located as separate Python packages. They expand nornir basic functionality to build the inventory and connection to devices.

  • Tasks: Functions that encapsulate specific actions to be performed on network devices.

  • Results: Data structures that store the output of executed tasks, providing insights into the results of automation operations.

Setting Up Nornir Environment

Before embarking on your network automation journey with Nornir, it's essential to set up a suitable environment. This involves installing the necessary dependencies and configuring your Nornir environment.

Installing Nornir

Nornir is readily available through the Python Package Index (PyPI). You can install it using the pip package manager:

pip install nornir

Creating an Inventory

An inventory defines the network devices you want to manage. Nornir supports various inventory formats, including YAML, JSON, and Python dictionaries. Here's an example of a simple YAML inventory:

hosts:
  router1:
    hostname: router1
    platform: ios
    username: admin
    password: password
  switch1:
    hostname: switch1
    platform: cisco
    username: admin
    password: password

Configuring Plugins

Nornir's plugins provide functionality for specific tasks. You can install plugins using pip or by specifying them in your requirements.txt file. For example, to install the napalm plugin for interacting with network devices:

pip install nornir-napalm

Basic Nornir Operations

Once your Nornir environment is set up, you can start performing basic operations. This section covers essential concepts and examples to get you started with Nornir.

Connecting to Devices

Nornir provides a convenient way to connect to network devices using plugins. The napalm plugin, for instance, allows you to connect to devices using various protocols, such as SSH, Telnet, and NETCONF.

from nornir import InitNornir
from nornir_napalm.plugins.tasks import napalm_get

nr = InitNornir(config_file='config.yaml')

result = nr.run(task=napalm_get, getters=['facts'])

Running Tasks

Tasks are the core of Nornir's automation capabilities. They encapsulate specific actions to be performed on network devices. You can define your own custom tasks or use pre-built tasks from Nornir's plugin ecosystem.

from nornir import InitNornir
from nornir_napalm.plugins.tasks import napalm_configure

nr = InitNornir(config_file='config.yaml')

config = {'interface': {'GigabitEthernet0/0': {'description': 'To the internet'}}}

result = nr.run(task=napalm_configure, configuration=config)

Working with Results

Nornir's results provide valuable information about the execution of tasks. They contain data about the task's success or failure, the output of the task, and other relevant details.

Accessing Results

You can access the results of a task using the results attribute of the Nornir object. The results are organized by device and task.

from nornir import InitNornir
from nornir_napalm.plugins.tasks import napalm_get

nr = InitNornir(config_file='config.yaml')

result = nr.run(task=napalm_get, getters=['facts'])

print(result['router1']['napalm_get']['facts'])

Analyzing Results

Nornir provides various methods for analyzing and processing results. You can use the failed attribute to check for failed tasks, iterate over the results, and filter results based on specific criteria.

from nornir import InitNornir
from nornir_napalm.plugins.tasks import napalm_get

nr = InitNornir(config_file='config.yaml')

result = nr.run(task=napalm_get, getters=['facts'])

for host, host_result in result.items():
  if host_result.failed:
    print(f'Task failed on {host}: {host_result.exception}')

Customizing Nornir

Nornir's modular design allows you to customize its behavior and extend its functionality to meet specific requirements. This section explores techniques for tailoring Nornir to your automation needs.

Creating Custom Tasks

You can define your own custom tasks to encapsulate specific actions or workflows. Custom tasks allow you to reuse code and streamline complex automation processes.

from nornir import InitNornir
from nornir.core.task import Task

def my_custom_task(task: Task) -> None:
  task.host.data['custom_data'] = 'This is custom data'

nr = InitNornir(config_file='config.yaml')

result = nr.run(task=my_custom_task)

Extending Plugins

Nornir's plugin ecosystem provides a wide range of functionality. You can extend existing plugins or create your own plugins to address specific needs. For example, you might create a plugin to interact with a custom network management system.

Nornir's flexibility and extensibility make it a powerful tool for automating complex network operations. By understanding its core concepts and customization capabilities, you can leverage Nornir to streamline your network management tasks and enhance your overall network efficiency.

0
Subscribe to my newsletter

Read articles from Pedro D directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Pedro D
Pedro D