Hi everyone,
I wanted to write this blog about a simple yet immensely productive tool. Let me introduce you to — The Jinja2 templates. It is a Python based template engine. I have used it on Ansible as well as Python scripts. By default , it is present in Ansible and in case on Python scripts, we just need to import the necessary packages/modules.
Use Case:
Have you ever had situations, where you need to update just certain values in a file and the rest remains the same? Here, we create a template and provide some variables, where we supply each values to be replaced. This is done in a certain format as we see later on.
How to use :
This is used just as expected — we need 2 things , a variable definition and a placeholder . So lets say ‘name’ is the placeholder and ‘linu’ is the variable definition. Then , the placeholder could look something like this :
name = 'linu'
printf(" My name is {{ name }} .")
Ofcourse , its not just as it seems. We need to add some statements to import the values — either using some packages or some statements
Sample script Ansible script:
---
- name: Sample script for Jinja2
hosts: localhost
vars:
- text : jinja2
tasks :
- name: Print a sample text
debug:
msg: "This is a line using {{ text }} template."
Output:
Sample script using Python Script:
In python , we require certain built in methods for replacing values in the placeholder.
# jinja2_sample.py
from jinja2 import Template
variable_name = "Jinja2"
template = Template("This is a line using {{ name }} template.")
msg = template.render(name=variable_name)
print(msg)
Output:
This is a line using Jinja2 template
Output:
This is a line using Jinja2 template
String manipulation
String manipulation, as the name suggest, enables us to transform the string to a variation of itself. Say we have a string , that accepts name from the user. But what if I need to use the same string which has to be used in a Template in all caps, we could do this with a minor change on the script. Lets see how.
We use a pipe ( | ) symbol and provide the modifications needed. So now if we have name which is defined by the user , to modify it, we do —
name = "Linu"
t = Template(" My name in CAPS is {{ name | upper }} .")
msg = t.render(name=name)
print(msg)
Output :
My name is LINU .
Similarly, we have other build in keywords :
- lower — for lowercase letters
- title — for capitalising first letter of each word
- replace (“old_word”,”new_word”) — replaces the 1st string with the 2nd string
- default(“default_word”) — Uses the default word if no word was initialized.
Similarly, we have operations that we can perform on lists :
- min/max — To fetch the minimum/max value
- unique — To fetch the unique value
- random — To fetch random values
We can also perform mathematical operations on list like :
- union
- intersection
Loops
Loops concepts , like in all other programming languages, is very productive in terms of execution and well as to decrease the number of code lines.
In Jinja2 templates, we utilize it for the same.
If-else Loop :
- Notice that we open and close with {% and %} resp.
- Like in Bash, we need to close for the if condition, which is by using endif keyword.
{% if <cond> %}
<stmt1>
{% elif <cond> %}
<stmt2>
{% else %}
<stmt3>
{% endif %}
For Loop :
- We need to close for the for condition, which is by using endfor keyword.
# Sample template to print numbers in a list
{% for n in [1,2,3] %}
{{ n }}
{% endfor %}
And that’s a wrap on the Basics of Jinja2 Template!
Cheers!!