Ansible Templating (Jinja2)
Jinja2 is a fast, reliable, and designer-friendly templating language. Ansible uses Jinja2 templating to enable dynamic expressions, dynamic file generation based on its parameter, and access to variables.
All templating happens on the Ansible controller before the task is sent and executed on the target machine. This approach minimizes the package requirements on the target (jinja2 is only required on the controller). It also limits the amount of data Ansible passes to the target machine.
In a Jinja2 template file, you will find the following tags:
{% ... %}
: for control statements (conditions)
{{ ... }}
: for expressions (variables)
{# ... #}
for comments (describe the task)
Most Jinja2 template files are used for creating files or replacing configuration files on servers. These template files have .j2 extension.
Here's an example Jinja expressions:
- hosts: webserver
vars_files:
- vars.yml
tasks:
- name: Checking the employee name
debug:
msg: "Employee name {{ name }}"
In the above example, definitions are required name.
Providing default values
You can provide default values for variables directly in your templates using the Jinja2 'default' filter. This is often useful when the value of a variable is not defined.
{ { variable | default(10) } }
Making variables optional
By default, Ansible requires values for all variables in a templated expression. However, you can make specific variables optional. For example, you might want to use a system default for some items. To make a variable optional, set the default value to the special variable omit.
{ { age | default(omit) } }
Defining mandatory values
You may want to define some values as mandatory. By default, Ansible throws an error if a variable in your playbook or command is undefined. You can do this with-
{ { variable | mandatory } }
Jinja2 template with filters
Filters are used to alter the appearance of output. Jinja2 uses piping as below:
{ { variable | argument } }
Use list filters to display maximum, & minimum values as below:
{{ [ 12, 13, 45, 54, 80, 97] | min }} => 12
Replacing a string value with another
{{ "Gaurav Sharma" | replace ("Gaurav", "Heena" }} ="Heena Sharma"
The Jinja2 templating is an ideal solution when handling dynamic variables in configuration files. It's inbuilt functions gives the ability to handle these dynamic changes in an efficient way.
Example Playbook:
- name: this is our first play.
hosts: webserver1
vars:
your_name: Gaurav
dummy_list:
- 1
- 10
- 20
- 30
- 10
dummy_list2:
- 30
- 40
- 10
- 50
tasks:
- debug:
msg: "Hello {{ your_name }}"
- debug:
msg: "Hello {{ your_name | upper }}"
- debug:
msg: "Hello {{ your_name | lower }}"
- debug:
msg: "Hello {{ your_name | replace('Gaurav','Saurav') }}"
- debug:
msg: "{{ dummy_list | min }}"
- debug:
msg: "{{ dummy_list | max }}"
- debug:
msg: "{{ dummy_list | unique }}"
- debug:
msg: "{{ dummy_list | union(dummy_list2) }}"
- debug:
msg: "{{ dummy_list | intersect(dummy_list2) }}"
- debug:
msg: "{{ 100 | random }}"
- debug:
msg: "{{ '/etc/ansible/ansible.cfg' | basename }}"