CentOS Debian Mint openSUSE Red Hat Ubuntu

How to Use the Jinja2 Template in Ansible

How to Use the Jinja2 Template in Ansible

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.

It has many other features. Some of them are listed below.

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

In this article, we are going to discuss the uses of the jinja2 template and the process to configure it.

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

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 }} on the above screenshot.

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 on the below screenshot.

These are the examples showing the jinja2 template is accessing the variables and displaying the actual values on the final output.

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” as the following screenshot.

$ cat server_hosts.j2


{% 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.

It is the final result of our variables in the jinja2 template file iterated over all the hosts in the inventory. So the conditional statements like for loops can also be used in jinja2 template files.

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 as shown below.

{{ 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”

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.


You can use such jinja2 template files as per your requirements to manipulate the variables and data to save time and smooth running of playbooks on ansible. You can also use filters for manipulating the numbers from high to low and so on. Try exploring the jinja2 template for more features. Thank you!


Similar Posts