Last updated on December 8th, 2022 at 11:29 pm
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.
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.
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_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.
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.
We can also check the network device itself to make sure of the configuration result.
Nornir netmiko_send_config Example2: via File
In the second script, we send the configuration commands through a file.
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.
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.
The script result shows that OSPF is run on the device and then the configuration changes are saved to the device.