Table of Contents
Nornir Napalm send configuration through “napalm_configure” task is what we demonstrate in this section.
We will also discuss the difference between “merge” and “replace” when sending the configuration to network devices.
Send Configuration through Nornir Napalm Plugin
Nornir Napalm “napalam_configure” parameters
To start sending configuration commands to network devices throuhg Nornir Napalm, let’s first look at the corresponding task “napalam_configure” parameters in the Github source.
def napalm_configure( task: Task, dry_run: Optional[bool] = None, filename: Optional[str] = None, configuration: Optional[str] = None, replace: bool = False, commit_message: str = None, ) -> Result: """ Loads configuration into a network devices using napalm Arguments: dry_run: Whether to apply changes or not filename: filename containing the configuration to load into the device configuration: configuration to load into the device replace: whether to replace or merge the configuration Returns: Result object with the following attributes set: * changed (``bool``): whether the task is changing the system or not * diff (``string``): change in the system """
As you can see, you can upload a list of configurations through the “filename” parameter or a single command through the “configuration” parameter.
The parameter “dry_run” is used to check if the configuration is correct or not but the configuration will not be applied to the devices.
There is also a “replace” option that we can use to replace the configuration. By default, the new configurations are merged as Netmiko and Scrapli plugins.
"merge" versus "replace" in Sending Configuration
To understand the difference between “merge” and “replace”, suppose that we have already an OSPF configuration in a network device.
Then we send a new OSPF configuration through nornir napalm once without “replace” option and once with “replace” option.
By default the behavior is to merge the new configuration to the old configuration. It means the new configuration will be added to the old configuration.
But when we use the “replace” option, the behavior is so that any new configuration lines will be added to the device and any current extra configuration lines will be deleted from the device.
Note that it is not the same if you first delete the old configuration and then add the new configuration to the device since when you delete the configuration, all existing neighborships will be disrupted.
In this section, we only discuss the default method of sending configurations that merge the configuration. Replacing the configuration will be discussed in the next sections.
Nornir Napalm Send Configuration Prerequisites
According to napalm documentation, there are some prerequisite to send configuration commands through napalm plugin.
Here I will mention just four of them, but all you can find in napalm documentation.
First, Cisco IOS doesn’t have a native API, so Napalm can’t connect to it directly. Because of this, Napalm uses the Netmiko library to interact with Cisco IOS devices. Therefore, the installation of the Netmiko plugin is a prerequisite for sending the configuration through Napalm.
Nornir netmiko plugin can be installed with the command “python3 -m pip install nornir-netmiko”.
The second pre-requisite is that NAPALM-ios driver requires SCP to be enabled on the managed device. SCP server functionality is disabled in IOS by default, and is configured using “ip scp server enable”.
The third and the fourth pre-requisite are for the time that we use “replace” option in sending configuration as we will see in the next sections and we don’t need them here to run the script that sends the configuration with the default “merge” behavior.
But just to complete the discussion of napalm prerequisites, we’ll mention them here.
The third prerequisite is that the archive functionality must be enabled before sending configuration through napalm to perform auto-rollback in case of any error. Make sure it’s enabled and set to a local filesystem, for example ‘flash:’ or ‘bootflash:’.
Archive feature can be enabled with the following commands.
archive
path flash:archive
write-memory
And the last prerequisite is for the time that we have banner configuration in cisco devices and we also use the “replace” parameter to replace the configuration.
In the configuration file there is “ctrl+c” in banner configuration as two characters and it must be sent as a single character.
If you edit the configuration file through “vim” editor, then we can use “type <ctrl>+v, release only the v, then type c” instead of normal “ctrl+c”.
If you don’t have a banner configuration in cisco IOS device, you can just ignore the fourth prerequisite.
Preparing Nornir Inventory and Configuration Files
To run the script which send configuration to network devices through nornir napalm, as we have said earlier, nornir-netmiko is a prerequisite. Therefore make sure that in addition to nornir, nornir-napalm and nornir-utils, nornir-netmiko is also installed since it is a prerequisite.
sudo apt-get update
sudo apt-get install python3 python3-venv python3-pip
python3 -m pip install nornir
python3 -m pip install nornir-netmiko
python3 -m pip install nornir-napalm
python3 -m pip install nornir-utils
Regarding nornir inventory and configuration files, I have changed nothing and all files are the same as the first section of nornir napalm module.
To know more about it, I will refer you to that section.
majid@majid-ubuntu:~/devnet/pyhton_nornir/2023/5.nornir_napalm$ cat hosts.yaml --- R1: hostname: "192.168.1.11" # username: "rayka" # password: "rayka-co.com" groups: - cisco majid@majid-ubuntu:~/devnet/pyhton_nornir/2023/5.nornir_napalm$ majid@majid-ubuntu:~/devnet/pyhton_nornir/2023/5.nornir_napalm$ cat groups.yaml --- cisco: platform: ios majid@majid-ubuntu:~/devnet/pyhton_nornir/2023/5.nornir_napalm$ majid@majid-ubuntu:~/devnet/pyhton_nornir/2023/5.nornir_napalm$ cat defaults.yaml --- username: "rayka" password: "rayka-co.com" platform: "ios" majid@majid-ubuntu:~/devnet/pyhton_nornir/2023/5.nornir_napalm$ majid@majid-ubuntu:~/devnet/pyhton_nornir/2023/5.nornir_napalm$ cat config.yaml --- inventory: plugin: SimpleInventory options: host_file: "hosts.yaml" group_file: "groups.yaml" defaults_file: "defaults.yaml" runner: plugin: threaded options: num_workers: 10
Nornir Napalm Send Configuration Example
This is the script to send configuration commands through nornir napalm plugin.
from nornir import InitNornir from nornir_napalm.plugins.tasks import napalm_configure from nornir_utils.plugins.functions import print_result nr = InitNornir(config_file="config.yaml") def nornir_napalm_send_config_exmaple(task): task.run(task=napalm_configure, filename="config_file_merge.txt") #task.run(task=napalm_configure, filename="config_file_replace.txt") results=nr.run(task=nornir_napalm_send_config_exmaple) print_result(results)
The first three lines are to import, “InitNornir” from “nornir” plugin to initialize the nornir, “napalm_configure” from “nornir_napalm” plugin to send configuration commands and “print_result” from “nornir_utils” to print the result of the configuration.
The next line is to initialize nornir in the variable “nr”.
In the function “nornir_napalm_send_config_exmaple”, we will send two set of configuration commands.
In the first example, we will send the configuration lines through the file with the name of “config_file_merge.txt” to show that the configuration will be merged with existing configuration.
In the second example, we will simulate to replace the configuration, with deleting the old configuration and then sending the new configuration through the configuration lines in the file with the name of “config_file_replace.txt”. This method is mostly disruptive and must be used carefully.
Example 1
To run the first example, let’s check first the current OSPF configuration in cisco device and what we will send through the file “config_file_merge.txt”.
!!! current configuration of cisco device
R1(config-archive)#do sh runn | sec router ospf
router ospf 1
router-id 1.1.1.1
network 10.2.0.0 0.0.255.255 area 1
network 10.3.0.0 0.0.255.255 area 1
network 10.0.0.0 0.255.255.255 area 0
network 193.168.1.11 0.0.0.0 area 0
!!! the configuration changes to be applied to the device
majid@majid-ubuntu:~/devnet/pyhton_nornir/2023/5.nornir_napalm$ cat config_file_merge.txt
router ospf 1
router-id 1.1.1.1
network 10.0.0.0 0.255.255.255 area 0
network 10.4.0.0 0.0.255.255 area 1
With running the script, it is expected that current device OSPF configuration will be merged with the new configuration.
majid@majid-ubuntu:~/devnet/pyhton_nornir/2023/5.nornir_napalm$ python3 5.4.nornir_napalm_send_config.py
nornir_napalm_send_config_exmaple***********************************************
* R1 ** changed : True *********************************************************
vvvv nornir_napalm_send_config_exmaple ** changed : False vvvvvvvvvvvvvvvvvvvvvv INFO
---- napalm_configure ** changed : True ---------------------------------------- INFO
+router ospf 1
+ network 10.4.0.0 0.0.255.255 area 1
^^^^ END nornir_napalm_send_config_exmaple ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
!!! the result of sending configuration on the device
R1(config-archive)#do sh runn | sec router ospf
router ospf 1
router-id 1.1.1.1
network 10.2.0.0 0.0.255.255 area 1
network 10.3.0.0 0.0.255.255 area 1
network 10.4.0.0 0.0.255.255 area 1
network 10.0.0.0 0.255.255.255 area 0
network 193.168.1.11 0.0.0.0 area 0
The result of the configuration shows that new networks are added and no network is deleted from OSPF configuration as we expected.
Example 2
Now let’s send the configuration lines which first delete the old configuration and then add new configuration which simulate the replace parameter. The replace parameter itself will be discussed in the next sections.
!!! the configuration changes to be applied to the device
majid@majid-ubuntu:~/devnet/pyhton_nornir/2023/5.nornir_napalm$ cat config_file_replace.txt
no router ospf 1
router ospf 1
router-id 1.1.1.1
network 10.0.0.0 0.255.255.255 area 0
network 10.4.0.0 0.0.255.255 area 1
Now we can run the script and check the result of sending configuration.
majid@majid-ubuntu:~/devnet/pyhton_nornir/2023/5.nornir_napalm$ python3 5.4.nornir_napalm_send_config.py
nornir_napalm_send_config_exmaple***********************************************
* R1 ** changed : True *********************************************************
vvvv nornir_napalm_send_config_exmaple ** changed : False vvvvvvvvvvvvvvvvvvvvvv INFO
---- napalm_configure ** changed : True ---------------------------------------- INFO
-no router ospf 1
^^^^ END nornir_napalm_send_config_exmaple ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
!!! the result of sending configuration on the device
R1(config-archive)#do sh runn | sec router ospf
router ospf 1
router-id 1.1.1.1
network 10.4.0.0 0.0.255.255 area 1
network 10.0.0.0 0.255.255.255 area 0
As we expect, exactly sending configuration will replace the old configuration.
you can download nornir napalm send configuration example python code from here.