This blog is part of my series on Devops for Networking. Typically, Network device configurations for CLI based systems are stored as text files and when its necessary to change parameters like gateway address, vlan, ntp server etc, the script is manually edited and then reapplied to the device. This process is manual and prone to errors. In this blog, I will cover how to automate generation of configuration scripts using Jinja2 and YAML. I will also provide an sample application that I created. For more details, please refer to the references section below.
Tools overview:
Jinja2:
Jinja2 is a Python library for creating configuration based on templates. Jinja2 defines a templating language with which templates are created. The templates can be as simple as a hostname variable that needs to be updated or it can be an array of vlans that needs to be populated. Jinja2 also provides complex templates to cover different scenarios. Following is a very simple example of a template which says ntp_server is a variable that needs to be updated dynamically. We will see later how we can feed in the dynamic values to update.
ntp server {{ ntp_server }}
YAML:
YAML is a human friendly data serialization standard for all programming languages. It is not a markup language. It is more human-friendly compared to XML or JSON. Python has a YAML library to parse the YAML files into dictionaries. Once the dictionaries are created, these can be used to create the configuration from templates using Jinja2. Following is a simple YAML example for defining miscellaneous router related information that illustrates hierarchical structure. YAML uses indenting to create hierarachy, so indenting needs to be strictly followed.
misc:
defaultgw: 40.1.1.1
snmp:
user: admin
trap:
trapServer: 1.1.1.1
version: 3
Prerequisites:
I have Python 2.7.3 in Ubuntu machine. YAML Python library was already installed. I installed Jinja2 using:
pip install Jinja2
Sample project:
To get a feel for these tools, I created a sample project to create a simple network device configuration using Cisco CLI format. The project source code is available here. There are 3 files in the project directory:
template.text - Template definition config.yaml - Dynamic data in YAML format generate_config.py - Python script to generate configuration
To run the script:
python generate_config.py
Following is the script output:
conf
#hostname config
hostname testrouter
# ntp server config
ntp server 10.1.1.1
ntp server 11.1.1.1
#vlan config
vlan 100
name accessvlan
vlan 200
name trunkvlan
#default gateway
ip route 0.0.0.0 0.0.0.0 40.1.1.1
#snmp parameters
snmp-server user admin
snmp-server host 1.1.1.1 version 3
The script reads the YAML file into a dictionary and then creates configuration by rendering the dictionary contents into the template.
This website was… how do I say it? Relevant!!
Finally I’ve found something that helped me. Many thanks!
Thanks!
Hi Sreenivas,
excellent summary of this topic, many thanks … helped me to move in the right direction.
I did some minor minor changes to get it under python3 working. this is posted on http://www.lieberth.net/doc007.htm
Thanks Stephen for sharing your link, looks good. Glad the blog helped.
Sreenivas