×


Use the Jinja2 Template in Ansible - Step by step guide ?

Jinja2 is one of the most powerful and famous python-based template engines. It is most popular for Ansible users because of the same programming language. It helps a lot of IT personnel in many ways. Jinja2 template is able to access the variables in ansible. In Ansible, it is needed to change a lot of things to each server which consumes a lot of time. So, using jinja2 templating, one can change variables only to that template which results in saving a lot of time and an easy environment.

In ansible, a jinja2 template is used for accessing the variables. So changing the variable as per needed at a time can help ansible playbooks to save a lot of time. Jinja2 template comes with inbuilt filters, and users can only manipulate such filters in configuring many files for the smooth playbooks.

Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to perform related Ansible queries.

In this context, we shall look into the uses of the jinja2 template and the process to configure it.


Main feature of Jinja2 Template in Ansible:

  • It is quite simple on debugging purpose.
  • Template inheritance is well supported.
  • It is very fast and easily compiled.


Variables accessed by Jinja2 template

Jinja2 template can access the variables in ansible. On an ansible control node, write a jinja2 template file which can easily access the variables defined in the same directory or in playbook.

We are going to write an example which shows the jinja2 template accessing and manipulating the values by accessing the variables. Here, the playbook is jinja2_var_example.yml:

hosts: all
become: true
gather_facts: false
vars:
j2_var: jinja2 variable
tasks:
- name: A Jinja2 example
debug:
msg: "A example of {{ j2_var}}"

Now, let's execute the playbook jinja2_var_example.yml.

Here, you can notice the value gets changed on the output. As {{ }} is also a syntax of jinja2 template, it can access the variable and change the value to the actual one.

Another example showing the jinja2 template accessing the variable. Here, we are going to create a folder then write a jinja2 template "nginx.conf.j2" as j2 is the extension for jinja2 template:

$ mkdir jinja2_variable
$ cd jinja2_variable/
$ vim nginx.conf.j2

Then we are going to write a file using jinja2 templating where variables are used. Such variables can be defined on different files or on playbooks which can be manipulated as per needed at that time.

You must have noticed the variables {{ http_port }} and {{ http_host }}.

Now, we are going to write the playbook which consists of the actual value of the variables included in the jinja2 template file.

After the playbook is successfully executed, the variables will be replaced by actual values in the file of the destination path.

You can notice the values are changed.


Conditional in jinja2 template

Conditional statements like for loops can be used by the jinja2 template. Loops help iterate over items in a range, list, and so on. Here, we are writing an example to show the for loop used in jinja2 template.

For example, we will write our template file "server_hosts.j2":

$ cat server_hosts.j2

Output:

{% for host in groups['all'] %}
{{ hostvars[host].ansible_facts.default_ipv4.address }} {{ hostvars[host].ansible_facts.fqdn }} {{ hostvars[host].ansible_facts.hostname }}
{% endfor %}

It will iterate over all the hosts in the inventory. For each hosts, value of all variables used in the template will be displayed.

On control node, we are going to write a playbook “conditional.yml” including the above jinja2 template file in it.

$ sudo vim conditional.yml

Now, we are running the playbook "conditional.yml"

$ ansible-playbook conditional.yml

Now, we can see the result on the host node if the /etc/hosts file is being updated or not.


Filters used in jinja2 template

If you want the output to be different or formatted, you can use filters. You can just pipe the variables with the arguments:

{{ variable | argument }}

Here, we are going to write a jinja2 template file "jinja_filters.j2" to show the filters used on the jinja2 template file.

$ vim jinja_filters.j2

We are going to write a playbook "filters.yml" which consists of a jinja2 template file:

$ vim filters.yml

Now, we are running the playbook "filters.yml":

$ ansible-playbook filters.yml

Now, we can see the result on the host node if the destination file is created with the content on uppercase or not.

Here, you can see all the text is in uppercase as we have used filter by piping the variables item into the uppercase argument.


[Need to fix Ansible issues ? We can help you. ]


Conclusion

This article covers how to use Jinja2 templating engine to carry out more involved and dynamic file modifications with Ansible. In fact, Jinja2 is a powerful and easy to use python-based templating engine that comes in handy in an IT environment with multiple servers where configurations vary every other time. Creating static configuration files for each of these nodes is tedious and may not be a viable option since it will consume more time and energy.