When interacting with a new network, we need a way to automatically configure all network devices. And the Nornir framework can help solve this problem.
Nornir framework is fully written in the Python programming language and is intended for use with Python.
Please note that below scripts/libraries or configurations are for learning purposes only, do not use them in production environment.
pip3 install nornir"<3"
We will need to generate a configuration template according to the lab work described below using the Nornir framework(version2!) and apply the configuration to network devices(switches).
Conditions of hands-on lab:
Jinja2 is a modern, user-friendly, full-featured template creation language for Python. Two configuration templates are used in this lab work. These are hostnames.j2 and interfaces.j2, each of which will create configurations for hostname and device interfaces respectively.
They are located in the "templates" directory.
The hostnames.j2 template looks like this:
{% for hostname in host["about"] %} hostname {{the hostname.caption }}. {% endfor %}
The part of interfaces.j2 template looks like this:
{% for interface in host["interfaces"] %} interface {{ interface.name }}{{ interface.port_slot }} {% if interface.unused is defined %} shutdown {% elif interface.vlan is defined %} switchport mode {{ interface.mode }} switchport access vlan {{ interface.vlan }} no shutdown {% elif interface.allowed_vlan is defined and interface.negotiation is defined %} switchport trunk allowed vlan {{ interface.allowed_vlan }} switchport trunk encapsulation {{ interface.encapsulation }} switchport trunk native vlan {{ interface.native_vlan }} switchport mode {{ interface.mode }} switchport {{ interface.negotiation }} no shutdown
The "Text" plugin is responsible for using Jinja templates in Nornir. The function "text.template_file" is used in this lab work. It renders the file data in the file "hosts.yaml" and converts the inventory data in this file into configuration using the template file.
The part of the code responsible for this task looks like this:
def hostname_config(task): var = task.run(task=text.template_file, name="Switches hostname configuration", template="hostnames.j2", path="templates") task.host["hostname_config"] = var.result task.run(task=networking.netmiko_send_config, name="Configure hostname on the device", config_commands=task.host["hostname_config"],)
As a result of running this part of the code, the «hostname» will be configured for all devices.
The following part of the code is responsible for configuring the interfaces of network devices.
def interfaces_config(task): var = task.run(task=text.template_file, name="Switches interface configuration", template="interfaces.j2", path="templates") task.host["interfaces_config"] = var.result task.run( task=networking.netmiko_send_config, name="Loading Configuration on the device", config_commands=task.host["interfaces_config"].splitlines())
As a result of running this part of the code, the interfaces will be configured for all devices according to conditions of laboratory work.
The following part of the code is responsible for sending ping from SW1 to ip address 255.255.255.255
def send_ping(task): task.run( task=networking.netmiko_send_command, delay_factor=2, max_loops=500, command_string='ping 255.255.255.255', name="Sending Ping on the device")
As a result of running this part of the code, the ping will be sent to all devices.
git clone https://github.com/emilgaripov/nornir-hands-on-lab
cd nornir-hands-on-lab/device_configuration
python3 devices_config.py
and the script will execute.My name is Emil Garipov - Network automation enthusiast, teacher, pythonist, Cisco Champion 2020.
Code Exchange Community
Get help, share code, and collaborate with other developers in the Code Exchange community.View Community