Jinja 2 Templates

Jinja 2 Templates
JInja2 is a widely-used and fully featured template engine for Python. Being modern it is hence also design-friendly language for Python, modelled after Django’s templates. Ansible uses Jinja2 templating to enable dynamic expressions and access to variables. Ansible controller, where JInja2 comes in picture, is where all the templating takes place before the command is sent and implemented on the target machine. Now, let us look at some syntax that will be helpful with Ansible.

VARIABLES:

We can print variables by simply using the {{ VARIABLE_NAME }} syntax. If you want to print just an element of an array, you can use {{ ARRAY_NAME[‘KEY’] }}, and if you want to print a property of an object, you can use {{ ARRAY_NAME[‘KEY’] }} .

Refer the following for an example;

<html>
<body>
<h1>Hello World!</h1>
<p>This page was created on {{ ansible_date_time.date }}.</p>
</body>
</html>

FILTERS:

From time to time, you would want to change the style of a string a little bit, without writing specific code for it. For example, you may want to capitalize some text. For doing so, you can make usage of one of the Jinja’s filters, such as {{ VARIABLE_NAME | capitalize }}.

CONDITIONALS:

One thing that can often be proven useful in a template engine is the possibility of printing different strings, depending on the content (or existence) of string. Take a cue from this example to add an interesting element to your static web page:

<html>
<body>
<h1>Hello World!</h1>
<p>This page was created on {{ ansible_date_time.date }}.</p>
{% if ansible_eth0.active == True %}
<p>eth0 address {{ ansible_eth0.ipv4.address }}.</p>
{% endif %}
</body>
</html>

In the above, it is clear that the capability to print the main IPv4 address is added for the eth0, if the connection is active.  With conditionals, tests can also be used.

So, to obtain the same result, the following can also be written:

<html>
<body>
<h1>Hello World!</h1>
<p>This page was created on {{ ansible_date_time.date }}.</p>
{% if ansible_eth0.active is equalto True %}
<p>eth0 address {{ ansible_eth0.ipv4.address }}.</p>
{% endif %}
</body>
</html>

CYCLES:

The jinja2 template system gives you the option of creating sycles . Let us add a feature to our page that will print the main IPv4 network address for each device, instead of only eth0. Refer the following code for the same:

<html>
<body>
<h1>Hello World!</h1>
<p>This page was created on {{ ansible_date_time.date }}.</p>
<p>This machine can be reached on the following IP addresses</p>
<ul>
{% for address in ansible_all_ipv4_addresses %}
<li>{{ address }}</li>
{% endfor %}
</ul>
</body>
</html>

As you can see, the syntax for cycles will seem familiar if you already know Python.

But this information on Jinja2 templating was not a substitute for the official documentation. In fact, Jinja2 templates are much more powerful than we have explored here.

To know more about Ansible and the application of the same, head on ‘Learning Ansible 2.7 – Third Edition.’ By Fabio Alessandro Locati, who is a senior consultant at Red Hat, a public speaker, an author, and an open source contributor. Let him take you through the fundamentals and practical aspects of Ansible by introducing you to topics that include playbooks, modules, BSD, Windows support, etc. You can definitely look forward to be equipped with the Ansible skills that are required to automate complex tasks for your organization.

Related Posts
Leave a Reply

Your email address will not be published.Required fields are marked *