Jinja2 Templates

Template (filename port_vlan.j2):

interface vlan {{ vlan_name }}
ip address {{ ip }} {{ mask }}
exit

Python code to build configuration which adds an IP address to a VLAN…

python

import jinja2
import os
loader = jinja2.FileSystemLoader(os.getcwd())
loader
jenv = jinja2.Environment(loader=loader, trim_blocks=True, lstrip_blocks=True)
template = jenv.get_template(‘port_vlan.j2′)
template
print (template.render(vlan_name=10, ip=’10.10.10.1′, mask=’255.255.255.0’))

Output:

interface vlan 10
ip address 10.10.10.1 255.255.255.0
exit

 

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Connecting to %s