Getting started with Jinja2#

You can install Jinja2 using pip:

pip install jinja2


Further, terms Jinja and Jinja2 are used interchangeably.

The main idea of Jinja is to separate data and template. This allows you to use the same template but not the same data. In the simplest case, template is simply a text file that specifies locations of Jinja variables.

Example of Jinja template:

hostname {{name}}
interface Loopback255
 description Management loopback
 ip address 10.255.{{id}}.1
interface GigabitEthernet0/0
 description LAN to {{name}} sw1 {{int}}
 ip address {{ip}}
router ospf 10
 router-id 10.255.{{id}}.1
 auto-cost reference-bandwidth 10000
 network area 0

Comments to template:

  • In Jinja, variables are written in double curly braces.

  • When script is executed, these variables are replaced with desired values.

This template can be used to generate configuration of different devices by substituting other sets of variables.