Jinja2 in Python – Render Config

Jinja2 is a full-featured template engine for Python. The first time I used it with Flask, it comes packaged with the powerful Jinja templating language. But it also could be used for rendering configuration.

Jinja templates use:

  • {{ … }} – for variable
  • {% … %} – for expressions or logic (like for loops)

Jinja2 also allows for conditional statements:

{% if new_year %}
<h1>Yes! Happy New Year!</h1>
{% else %}
<h1>No.</h1>
{% endif %}

Jinja2 Loops:

{% for name in names %}
<li>{{ name }}</li>
{% endfor %}

Installation

pip install Jinja2

Most basic way – Template

from jinja2 import Template
template = Template('Hello {{ name }}!')
print(template.render(name='Dmitry Golovach'))

good for simple templates that are loaded from strings (not recommended way if from the file system or another data source)

Main usage – Environment

Environment contains important shared variables like configuration, filters, tests, globals, and others. It is also used to load templates from the file system or other locations

from jinja2 import Environment, FileSystemLoader
env = Environment(
    loader=FileSystemLoader('./'),
)

int_template = env.get_template('int_template.j2')

class jinja2.FileSystemLoader(searchpath, encoding=’utf-8′, followlinks=False) – Loads templates from the file system. This loader can find templates in folders on the file system and is the preferred way to load them.

The loader takes the path to the templates as a string, or if multiple locations are wanted a list of them which is then looked up in the given order:

loader = FileSystemLoader(['/path/to/templates', '/other/path'])

get_template method – load a template from the loader

int_template.j2 – template file:

{% for interface, int_cfg in interfaces.items() %}
interface {{ interface }}
 descripion {{ int_cfg.descripion }}
 ip address {{ int_cfg['ipaddr'] }}
 {{ "no shutdown" if int_cfg.state=='up' else "shutdown"}}
{% endfor %}

r_config.yml – file with device info:

---
r1:
  interfaces:
    GigabitEhternet1:
      ipaddr: 10.10.10.10 255.255.255.0
      description: Wide Area Network
      state: up
    GigabitEhternet2:
      ipaddr: 10.10.11.10 255.255.255.0
      description: Local Area Network
      state: up
r2:
  interfaces:
    GigabitEhternet1:
      ipaddr: 20.20.20.10 255.255.255.0
      description: Wide Area Network
      state: up
    GigabitEhternet2:
      ipaddr: 20.20.21.10 255.255.255.0
      description: Local Area Network
      state: up

This tool becomes very handy if you need to generate configuration or do it multiple times by changing slight details. If you have a good YAML file with device information, you can do a lot with Jinja and templates in terms of config generation.

Share

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *