In today’s fast-paced digital landscape, network automation has become a cornerstone of efficient IT operations. Among the myriad of tools available, Cisco Nornir shines as a versatile and powerful framework for automating network tasks. In this article, we’ll delve into the world of network automation using Cisco Nornir, accompanied by a live example that demonstrates its practical application and benefits.

Getting Started with Cisco Nornir: Before diving into our live example, let’s take a brief overview of Cisco Nornir and its key components. Nornir, built on Python, provides a programmable and modular approach to network automation. It enables administrators to automate tasks across multi-vendor environments, offering features like inventory management, configuration management, and task parallelism.

Key Features and Capabilities:

  1. Multi-Vendor Support: One of Nornir’s defining features is its ability to work seamlessly with devices from multiple vendors, including Cisco, Juniper, Arista, and more. This versatility enables organizations to automate tasks across heterogeneous network infrastructures, eliminating vendor lock-in and fostering interoperability.
  2. Programmatic Approach: Nornir allows administrators to interact with network devices programmatically using Python, a widely adopted programming language in the networking community. This enables the creation of customized automation scripts tailored to specific requirements, thereby enhancing flexibility and control.
  3. Inventory Management: With Nornir, maintaining an accurate inventory of network devices becomes effortless. Administrators can programmatically retrieve device information, such as device type, IP addresses, hardware specifications, and software versions, and store it in a structured format for easy access and reference.
  4. Task Parallelism: Nornir leverages the power of concurrency and parallelism to execute tasks concurrently across multiple devices, thereby optimizing performance and reducing execution time. This capability is particularly beneficial for large-scale network deployments where efficiency is paramount.
  5. Extensibility and Customization: Nornir’s modular architecture allows for seamless integration with existing automation tools and frameworks, such as Ansible, SaltStack, and Puppet. Moreover, administrators can extend Nornir’s functionality by developing custom plugins and extensions to suit specific use cases and requirements.

Benefits of Using Cisco Nornir:

  1. Increased Efficiency: By automating repetitive tasks and workflows, Nornir enables administrators to focus on higher-value activities, such as network optimization and innovation, thereby boosting overall efficiency and productivity.
  2. Improved Reliability: Manual errors are a common source of network downtime and disruptions. Nornir helps mitigate this risk by enforcing consistency and accuracy across network configurations, reducing the likelihood of configuration errors and misconfigurations.
  3. Scalability and Flexibility: Whether managing a small-scale deployment or a complex, multi-site network infrastructure, Nornir scales effortlessly to meet the evolving needs of modern enterprises. Its flexible architecture accommodates diverse use cases and adapts to changing requirements with ease.
  4. Enhanced Troubleshooting: Nornir simplifies the troubleshooting process by providing real-time insights into network health and performance. Administrators can leverage its robust capabilities for gathering diagnostic information, analyzing network data, and identifying potential issues proactively.
  5. Cost Savings: By automating routine network tasks and streamlining operations, Nornir helps reduce operational costs associated with manual labor, downtime, and human errors. Additionally, its open-source nature eliminates the need for costly licensing fees, making it a cost-effective solution for organizations of all sizes.

Live Example: Automating Configuration Backups with Cisco Nornir

Scenario: Imagine you’re tasked with ensuring regular backups of configurations for all network devices in your organization’s infrastructure. Traditionally, this might involve logging into each device manually and saving configurations individually—a time-consuming and error-prone process. With Cisco Nornir, however, you can automate this task effortlessly.

Step 1: Setting Up Your Environment First, ensure you have Python and the necessary dependencies installed. You can install Nornir using pip:

pip install nornir

Next, create a Python script (e.g., backup_configs.py) to define your inventory and tasks.

Step 2: Define Inventory In your script, define the inventory of network devices you want to manage. This includes details such as device IP addresses, credentials, and platform information. Here’s an example:

from nornir import InitNornir
from nornir.core.inventory import Inventory

inventory = {
"host1": {"hostname": "192.168.1.1", "username": "admin", "password": "password"},
"host2": {"hostname": "192.168.1.2", "username": "admin", "password": "password"},
# Add more devices as needed
}

nr = InitNornir(inventory=Inventory(**inventory))

Step 3: Define Tasks Next, define the task you want to automate. In this case, we’ll create a task to backup configurations:

from nornir.plugins.tasks.networking import napalm_get

def backup_configs(task):
result = task.run(task=napalm_get, getters=["config"])
task.host["config"] = result.result["config"]["running"]

Step 4: Execute Tasks Now, execute the defined task across all devices in your inventory:

result = nr.run(task=backup_configs)

Step 5: Verify Results Finally, verify that the configurations have been backed up successfully:

for host, task_result in result.items():
if task_result.failed:
print(f"Failed to backup configuration for {host}")
else:
print(f"Configuration backed up successfully for {host}")

Are you ready to take your network automation skills to the next level? Join our upcoming instructor-led batch for “Master Python Network Automation using Python, Ansible, REST API, and Cisco DevNet”!

Course Outline > Master Python Network Automation using Python, Ansible, Rest API and CISCO DEVNET

Regards

Sagar
Linkedin