Network device configuration using templates with Jinja2 and YAML

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 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 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.

    user: admin
      version: 3


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 - Python script to generate configuration

To run the script:


Following is the script output:

#hostname config
hostname testrouter
# ntp server config

ntp server

ntp server

#vlan config
vlan 100
    name accessvlan
vlan 200
    name trunkvlan

#default gateway
ip route
#snmp parameters
snmp-server user admin 
snmp-server host version 3

The script reads the YAML file into a dictionary and then creates configuration by rendering the dictionary contents into the template.


4 thoughts on “Network device configuration using templates with Jinja2 and YAML

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s