Updated April 5, 2023
Introduction to Ansible Template
Ansible provides a very useful module named template, which give you the ability to use a template where you can make use of available variables, use conditions to generate specific case data, use filters, statements and comments to provide more information to the template user. Then use this template to copy or send a string of data from controller node to remote nodes and this data can be generated based on remote nodes when variables store fact values after fetching from each remote node. For Windows, same can be achieved by win_template module with few differences.
What is Ansible Template?
In Ansible, template module is used to copy data from controller nodes to remote hosts, this data is generated based on jinja2 templates. In a jinja2 template, we can give the plain text which will be transferred to remote hosts as it is. Along with plain text we can give variables, which are either default ones like ansible facts or defined in playbook or included in playbook from a variable file. The templates are processed by jinja2 templating engine. Also, the way these templates are formatted is an important aspect.
How does Ansible Template Works?
While using ansible template module, we need to use at least below two parameters:
- src: The source of template file with relative or absolute path.
- dest: The destination where you want to copy this template on remote hosts. If only directory path is given then a file with same name of template will be created.
Given below are the other parameters that are available for ansible template module along with the src and dest.
- force: By default this parameter is set to ‘yes’. Which means if there is an existing file on destination location and the file template task is going to create has some difference, then target file will be overwritten. If you set this parameter to ‘no’ then it will not replace the existing file.
- mode: If you want to set permission for the destination file explicitly, then you can use this parameter and give permissions under this.
- backup: If the file already exists and you do not want to replace it, then use this parameter and set to ‘yes’. By this every time a new file with same names is pushed from controller node, then older file on remote hosts will be renamed and name will contain date and time of its being renamed.
- owner: To set the owner of the file on remote hosts.
- group: To set the group owner of the file on remote hosts.
- newline_sequence: To specify the newline sequence to use for templating files. Acceptable values are \n, \r, \r\n. In case of win_template, \r\n is default.
- Variables: Mentioned between {{ and}}.
- selevel, serole, setype and seuser: They are related to SELinux file context.
Examples of Ansible Template
Given below are the examples mentioned:
Now by using examples we will see Ansible Template, which we might have to use in day to day operations. Here we have an Ansible control machine named ansible-controller and two remote Linux hosts named host-one and host-two. We will create playbooks and run ansible commands on ansible-controller node and manage the remote hosts.
Example #1
In this example, we will use a jinja2 template to see how it will be transformed into strings and copied to a file on remotehosts.
For this, create a playbook with below content:
Code:
hosts:
all
tasks:
name: Here we use template to copy a plain text file to remotehosts template:
src: plain.j2
dest: /tmp/file.txt
The jinja2 template file have only plain text like below content:
Code:
This is just a sample.
After deploying this playbook we will get the output like below:
Output:
Example #2
In this example, we will use a jinja2 template to see how it will be transformed into strings and copied to a file on remote hosts.
For this, create a playbook with below content:
Code:
hosts:
all
tasks:
name: Here we use template to copy a file with fact data to remotehosts template:
src: fact.j2
dest: /tmp/file_fact.txt
The jinja2 template file have below content where we used ansible facts:
Code:
the hostname of this file is {{ ansible_hostname }} and its ip address is {{ ansible_all_ipv4_addresses }}
After deploying this playbook we will get the output like below:
Output:
Example #3
In this example, we will use a jinja2 template to see how it will be transformed into strings and copied to a file on remote hosts.
For this, create a playbook with below content and define variables:
Code:
hosts:
all vars:
var1:
"hello"
var2:
"my"
"name"
"is"
"ansible"
tasks:
name: Here we used the variables defined in playbook in jinja2 template template:
src: template_var.j2
dest: /tmp/file_var.txt
The jinja2 template file have below content:
Code:
{{ var1 }} world!
ques: what is your name? ans:
{% for vars in var2 %}
{{ vars }}
{%- endfor %}
After deploying this playbook we will get the output like below:
Output:
Here, please note in the jinja2 template, the endfor is like below. Where we put a ‘-‘ after ‘%’
Code:
{%- endfor %}
If we do not put it and make like below only:
Code:
{% endfor %}
The output will be like below:
Output:
So, here ‘-’ is used for removing white spaces at the end of the block.
Conclusion
In Ansible, we can use template module to work efficiently and produce quantifiable results. There are other modules as well like copy module which provide some features of template module if not all when you do not want to work with Jinja2 templating. But using more feature equipped modules makes the tasks execution smooth and leverage the functionality to the full extent enables you to do more tasks with less code.
Recommended Articles
This is a guide to Ansible Template. Here we discuss what is Ansible Template with the working and respective example. You may also have a look at the following articles to learn more –