1. Quick Jinja2 Demo using the Python Interpreter
2. Simple Python script with a Jinja2 Template
3. Python Script with Jinja2 using YAML files
1. Quick Jinja2 Demo using the Python Interpreter
2. Simple Python script with a Jinja2 Template
3. Python Script with Jinja2 using YAML files
This is just a short demo to quickly show how Jinja2 templates work with Python
Using a Docker container greatly simplifies the environment setup for Python, PyEz and Ansible... It also keeps things clean and contained
docker pull juniper/pyez-ansible
docker run -it --rm -v $(pwd):/project juniper/pyez-ansible
<<<<<<< HEAD See Docker Hub for more info:
If you need access to some Junos devices for lab / testing purposes, you can run vSRX or vQFX vagrant images:
- vSRX image on Vagrant Cloud:
- vQFX image on Vagrant Cloud:
- Examples of multi VMs topologies:
======= See Docker Hub for more info https://hub.docker.com/r/juniper/pyez-ansible/
809b69b4d307698472ee5ce6e9918a42b4adaa32
python
Import the "Template" method from the jinja2 module
from jinja2 import Template
Create an instance of Template with my interface address variables. Jinja2 identifies the variables by using double curly brackets.
For example, if I want to configure a number of interfaces on a Junos device with an IPv4 address, I can use the following syntax with 3 variables: the IFD (physical interface name), the unit and an IP address:
template = Template('set interface {{ ifd }} unit {{ unit }} family inet address {{ ip }}')
The template object provides a method called render() which, when called with a dict or keyword arguments, expands the template
Now you can "expand" the variables with some values:
template.render(ifd='ge-0/0/0',unit='101',ip='10.0.1.1/24’)
And you can repeat that many times with different values:
template.render(ifd='ge-0/0/0',unit='102',ip='10.0.2.1/24’)
Use Ctrl-D or:
exit()
Now instead of using the Python interpreter I can write a script that does the same thing.
Write a script called "genconfig.py" that includes the same jinja2 template. We then expand the variables with render() method for two logical interfaces and print the results. <<<<<<< HEAD
#!/usr/bin/env python
from jinja2 import Template
template = Template('set interface {{ ifd }} unit {{ unit }} family inet address {{ ip }}')
ifa1 = template.render(ifd='ge-0/0/0',unit='101',ip='10.0.1.1/24')
print ifa1
ifa2 = template.render(ifd='ge-0/0/0',unit='102',ip='10.0.2.1/24')
print ifa2
Make sure the access permissions are properly set to execute this script
chmod +x genconfig.py
Now you're ready to call that script:
./genconfig.py
or
python genconfig.py
Done.
Writing variables within the script means you need to update the script for every variable change. Instead you can store the variables in a separate file written in YAML format which is easy to read/update and much cleaner. It's also a good idea to write your jinja2 template in a separate file while we're at it.
Write a jinja2 template called "conf.j2":
In 'set commands' format:
{% for item in ifd %}
set interfaces {{ item.name }} unit {{ item.unit }} family int address {{ item.ip }}
{% endfor %}
Or in 'curly bracket' style:
interfaces {
{% for item in ifd %}
{{ item.name }} {
unit {{ item.unit }} {
family inet {
address {{ item.ip }};
}
}
}
{% endfor %}
}
Store the variables in a YAML file called "ifvars.yml":
---
ifd:
- name: 'ge-0/0/1'
unit: 0
ip: 10.0.1.1/24
- name: ge-0/0/2
unit: 0
ip: 10.0.2.1/24
- name: ge-0/0/3
unit: 0
ip: 10.0.3.1/24
- name: ge-0/0/4
unit: 0
ip: 10.0.4.1/24
- name: ge-0/0/5
unit: 0
ip: 10.0.5.1/24
Write a script called "genconfig.py" that includes the same jinja2 template. We then expand the variables with render() method for two logical interfaces and print the results. <<<<<<< HEAD
#!/usr/bin/env python
import yaml
from jinja2 import Template
mytemplate = "setconf.j2"
myinput = "ifd.yml"
### Print the Jinja2 Template file
myj2 = open(mytemplate.read())
#print "\n### DEBUG: Print the Jinja2 template:"
#print myj2
### Print the YAML input file
mydata = yaml.load(open(myinput).read())
#print "\n### DEBUG: Print the YAML file:"
#print mydata
### Render the jinja2 template
mytemplate = Template(myj2)
myconfig = mytemplate.render(mydata)
print "\n### Here's the full config:"
print myconfig
Make sure the access permissions are properly set to execute this script
chmod +x genconfig.py
Now you're ready to call that script:
./genconfig.py
or
python genconfig.py
Done.