Table of Contents

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.

Nornir netmiko_send_config Configuration Examples

As we have seen and discussed in the previous section, you can see the details of nornir netmiko tasks in GitHub open source community.

You can find the source of the nornir netmiko plugin in the GitHub through https://nornir.tech or by searching “nornir netmiko github” in Google.

As you can see in the list of the tasks, “netmiko_send_command” is the task that you can send monitoring commands to the network devices and it is what we have given two configuration examples in the previous section.

Two other tasks of nornir netmiko plugin, “netmiko_send_config” and “netmiko_save_config” will be discussed in this section.

Nornir netmiko_send_config Task Review

If we look at inside “netmiko_send_config” task, the name of the task and the configuration commands are required parameters that must be sent to the device.

There are two methods to send configuration commands. Through a list or through a file.

Use the “config_commands” parameter to send a list of configuration commands to the device.

And through “config-file” parameter, you can send the list of configuration commands via a file where each command is on a separate line.

def netmiko_send_config(
    task: Task,
    config_commands: Optional[List[str]] = None,
    config_file: Optional[str] = None,
    enable: bool = True,
    dry_run: Optional[bool] = None,
    **kwargs: Any
) -> Result:
    """
    Execute Netmiko send_config_set method (or send_config_from_file)
    Arguments:
        config_commands: Commands to configure on the remote network device.
        config_file: File to read configuration commands from.
        enable: Attempt to enter enable-mode.
        dry_run: Whether to apply changes or not (will raise exception)
        kwargs: Additional arguments to pass to method.
    Returns:
        Result object with the following attributes set:
          * result (``str``): string showing the CLI from the configuration changes.
    """

Nornir netmiko_send_config Pre-Requirements

Before running the script and sending any configuration commands, you must ensure that the Nornir inventory files and configuration file are already prepared. We discussed how these files are prepared in the previous section. So I do not repeat the discussion here and you are referred to the previous section.

Nornir Inventory and Config Files
Nornir Inventory and Config Files

Also ensure that the required Python libraries “nornir”, “nornir-netmiko” and “nornir-utils” are installed. What each of these Python libraries are used for and how to install them, is discussed again in the previous section.

Nornir Netmiko Packages and Plugins Requirements
Nornir Netmiko Packages and Plugins Requirements

Nornir netmiko_send_config Example1: via List

Now we are ready to review and run the script.

As you know there are two methods to send configuration commands through nornir netmiko plugin. Giving the configuration commands in a list or through a file. In the first script, the commands will be pushed through a list.

from nornir import InitNornir
from nornir_netmiko.tasks import netmiko_send_config
from nornir_utils.plugins.functions import print_result

nr = InitNornir(config_file="config.yaml")
commands=["router ospf 1", "router-id 1.1.1.1", "network 10.0.0.0 0.255.255.255 area 0"]

def netmiko_send_config_exmaple(task):
    task.run(task=netmiko_send_config, config_commands=commands)

results=nr.run(task=netmiko_send_config_exmaple)
print_result(results)

Exactly like nornir netmiko send commands script, three first lines are to import the required task to send the configuration commands to network devices.

The task “InitNornir” from “nornir” plugin is to initialize nornir.

In the previous section the task “netmiko_send_command” was used to send monitoring commands to network devices. In this section the task “netmiko_send_config” from “nornir_netmiko” plugin, is used to send configuration commands to network devices.

And finally we use the task “print_result” from “nornir_utils” plugin to print the result of sending commands to network devices.

In the next line, nornir is initialized in the variable nr.

Then a list of configuration commands are prepared in commands variable which is used to configure OSPF in network devices.

Then a function is created with two parameters, the name of the task “netmiko_send_config” and the list of configuration commands, which is already prepared in the variable “commands”.

Then the function is run through initialized nornir, “nr” variable.

Finally “print_result” task is used to print the result of sending configuration commands.

Now we can run the script and see the result of configuration. But before that let’s delete any OSPF configuration from the router to make sure of any changes through the script.

majid@majid-ubuntu:~/devnet/pyhton_nornir/2023/2.nornir_netmiko$ python3 2.2.nornir_netmiko_send_config.py
netmiko_send_config_exmaple*****************************************************
* R1 ** changed : True *********************************************************
vvvv netmiko_send_config_exmaple ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvv INFO
---- netmiko_send_config ** changed : True ------------------------------------- INFO
configure terminal
Enter configuration commands, one per line.  End with CNTL/Z.
R1(config)#router ospf 1
R1(config-router)#router-id 1.1.1.1
R1(config-router)#network 10.0.0.0 0.255.255.255 area 0
R1(config-router)#end
R1#
^^^^ END netmiko_send_config_exmaple ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

We can also check the network device itself to make sure of the configuration result.

R1#show runn | sec router ospf
router ospf 1
 router-id 1.1.1.1
 network 10.0.0.0 0.255.255.255 area 0

Nornir netmiko_send_config Example2: via File

In the second script, we send the configuration commands through a file.

from nornir import InitNornir
from nornir_netmiko.tasks import netmiko_send_config
from nornir_netmiko.tasks import netmiko_save_config
from nornir_utils.plugins.functions import print_result

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

def netmiko_send_config_from_file_exmaple(task):
    task.run(task=netmiko_send_config, config_file="config_file.txt")

def netmiko_save_config_exmaple(task):
    task.run(task=netmiko_save_config)

results=nr.run(task=netmiko_send_config_from_file_exmaple)
print_result(results)

results=nr.run(task=netmiko_save_config_exmaple)
print_result(results)

If we look at the script, we see that instead of “config_commands”, we use “config_file” parameter to send the configuration commands through a file instead of a list. The “config_file.txt” is passed through this parameter.

We look inside the file, you will see exactly the same OSPF configuration as the previous script.

majid@majid-ubuntu:~/devnet/pyhton_nornir/2023/2.nornir_netmiko$ cat config_file.txt
router ospf 1
 router-id 1.1.1.1
 network 10.0.0.0 0.255.255.255 area 0

Another difference you see in this script is the use of a different task, “netmiko_save_config”, to save the configuration after the changes are applied to the device.

This is equivalent to “write memory” or “copy running-config startup-config” in cisco devices.

At the end of the script, the first function is run to apply the configuration changes. Then the second function is run to save the configuration changes.

Now we can run the scrip to see the result. But before running the script, let’s delete OSPF configuration from the router to make sure of any changes through the script.

majid@majid-ubuntu:~/devnet/pyhton_nornir/2023/2.nornir_netmiko$ python3 2.3.nornir_netmiko_send_config_from_file.py
netmiko_send_config_from_file_exmaple*******************************************
* R1 ** changed : True *********************************************************
vvvv netmiko_send_config_from_file_exmaple ** changed : False vvvvvvvvvvvvvvvvvv INFO
---- netmiko_send_config ** changed : True ------------------------------------- INFO
configure terminal
Enter configuration commands, one per line.  End with CNTL/Z.
R1(config)#router ospf 1
R1(config-router)# router-id 1.1.1.1
R1(config-router)# network 10.0.0.0 0.255.255.255 area 0
R1(config-router)#end
R1#
^^^^ END netmiko_send_config_from_file_exmaple ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
netmiko_save_config_exmaple*****************************************************
* R1 ** changed : True *********************************************************
vvvv netmiko_save_config_exmaple ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvv INFO
---- netmiko_save_config ** changed : True ------------------------------------- INFO
write mem
Building configuration...
[OK]
R1#
^^^^ END netmiko_save_config_exmaple ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

The script result shows that OSPF is run on the device and then the configuration changes are saved to the device.

You can download nornir netmiko send configuration commands examples from this link.

Back to: CLI based Network Automation using Python Nornir > Python Nornir Netmiko Plugin

Leave a Reply

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


Post comment