Table of Contents
Jinja2 Template in netconf help us separate network device configurations from automation script.
Jinja2 Template is a common configuration template for many network devices, in which in its simplest form, data from network devices are replaced with variables.
When applying the configuration template to network devices, variables in the configuration template are replaced with data of each network device.
Jinja2 Template Review
But just to review, we used nornir plugin to take inventory advantage of this plugin which help us to separate inventory information and credentials from the automation script.
With the use of jinja2 configuration template, we create a common configuration template for many network devices and also separate it from the automation script.
In the simplest form, the configuration template uses variables that are replaced by the data of the respective network device at the time of the configuration deployment.
Jinja2 template also has other programming features like loops and conditionals.
The data of each network device is also kept separate from the network automation script, replacing the variables at the time that configuration templates are deployed to the network devices.
All these concepts can also be used to change the configuration of network devices with NETCONF protocol. The only difference is that we use XML-based and YANG-structured configuration template instead of CLI based configuration template to deploy to the network devices.
Jinja2 Template in NETCONF Example
Edit Config with NETCONF and Jinja2 Template
This is the automation script used to change the configuration of network devices using jinja2 configuration template and netconf protocol.
The task “load_yaml” from “nornir_utils” plugin is used to load YAML data into a dictionary structure.
Here, there is a YAML data file for each network device inside “host_vars” folder. the loaded YAML file will be kept inside a dictionary variable with the name of network device and “hdata” key.
In other words “host.hdata” is used to access to the root of data for each network devices.
The task “template_file” from “nornir_jinja2” is used to load the jinja2 configuration template and apply loaded data to the configuration template to extract the configuration for each network device.
And the final configuration will be deployed to the network devices using “netconf_edit_config” from “nornir_scrapli” or “nornir_netconf” plugins.
YAML Data File
The data of router R1 is located inside “host_vars\R1.yaml” YAML file.
The information related to OSPF routing protocol including “process_id”, “router_id”, “network” and area numbers are stored inside this file.
The “process_id” and “network” numbers are a list of data which can be accessed using a loop logic.
YANG Strcutured Jinja2 Configuration Template
This is the OSPF configuration in the format of YANG data structure that we have used in the previous section to deploy through netconf protocol.
Exactly the same configuration is converted to jinja2 template format using variable substitution and for loops.
This is the jinja2 configuration template which we use in this section to apply to the network devices.
“host.hdata” is used to access to root of data dictionary of each network device.
For example, “host.hdata.ospf.process_id” is used to access the list of OSPF process id for each network device.
The variable “host.hdata.ospf.networks” is to access the list of network numbers to be configured in each network device.
The variables “host.hdata.ospf.networks.wildmask” and “host.hdata.ospf.networks.area” are to access the wild mask and area number for each network.
Two for loops are used to access the list of OSPF processes and OSPF networks.
All other parts of the jinja2 template are exactly the same as what we deployed on network devices without jinja2 template.
To use jinja2 template in automation script, beforehand we have to install “nornir_jinja2” plugin.
python3 -m pip install nornir_jinja2
Before we run the automation script, let’s check the current OSPF configuration of network device.
router ospf 1 router-id 18.104.22.168 network 192.168.11.0 0.0.0.255 area 0 network 192.168.12.0 0.0.0.255 area 0 network 192.168.13.0 0.0.0.255 area 1 network 192.168.14.0 0.0.0.255 area 1
Now we run the script to see the result of the configuration. Before that we change a network in R1.yaml data file.
As we expected, by default the configuration is merged with the current configuration.
router ospf 1 router-id 22.214.171.124 network 192.168.11.0 0.0.0.255 area 0 network 192.168.12.0 0.0.0.255 area 0 network 192.168.13.0 0.0.0.255 area 1 network 192.168.14.0 0.0.0.255 area 1 network 192.168.111.0 0.0.0.255 area 1
Now we can add operation=”replace” at <router-ospf> or at <ospf> level, to replace the current configuration with the new configuration.
Then we run again the script to make sure that the OSPF configuration is replaced.
This the result of OSPF configuration after running the script. And as we expected the configuration is replaced.
router ospf 1 router-id 126.96.36.199 network 192.168.12.0 0.0.0.255 area 0 network 192.168.13.0 0.0.0.255 area 1 network 192.168.14.0 0.0.0.255 area 1 network 192.168.111.0 0.0.0.255 area 1