Ansible Template

Ansible Template
Techiio-author
Written by Nilima PaulJanuary 25, 2022
10 min read
Ansible
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Techiio-author
Nilima Paul

Technology Security Analyst

We will know in this article, what's the Ansible Template.

Introduction to Ansible Template

Ansible gives an extremely helpful module named format, which empowers you to utilize a layout where you can utilize accessible factors, use conditions to create explicit case information, use channels, explanations, and remarks to give more data to the format client. Then, at that point, utilize this layout to duplicate or send a series of information from regulator hubs too far off hubs and this information can be created given far off hubs when factors store truth esteems in the wake of bringing from every distant hub. For Windows, the equivalent can be accomplished by the win_template module with few contrasts.

What is Ansible Template?

In Ansible, a format module is utilized to duplicate information from regulator hubs to remote has, this information is created given jinja2 layouts. In a jinja2 format, we can give the plain text which will be moved to remote hosts all things considered. Alongside the plain text, we can give factors, which are either default ones like ansible realities or characterized in the playbook or remembered for playbook from a variable document. The layouts are handled by the jinja2 templating motor. Additionally, how these layouts are organized is a significant angle.

How does Ansible Template Work?

While using the 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 a directory path is given then a file with the same name of the template will be created. Given below are the other parameters that are available for the Ansible template module along with the src and dest.
  • force: By default, this parameter is set to ‘yes’. This means if there is an existing file on destination location and the file template task is going to create has some difference, then the 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 it to ‘yes’. By this every time a new file with the same name is pushed from the controller node, then the older file on remote hosts will be renamed and the name will contain the 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}}.
  • level, role, type, and user: They are related to the SELinux file context.

Examples of Ansible Template

Given beneath are the models referenced:

Presently by utilizing models we will see Ansible Template, which we may need to use in everyday tasks. Here we have an Ansible control machine named ansible-regulator and two distant Linux has named have one and host-two. We will make playbooks and run ansible orders on the ansible-regulator hub and deal with 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 remote hosts.

For this, create a playbook with the below content:

Code:

hosts:
all
tasks:
name: Here we use the template to copy a plain text file to remote hosts template:
src: plain.j2
dest: /tmp/file.txt

The jinja2 template file has only plain text like the below content:

Code:

This is just a sample.

After deploying this playbook we will get the output like below:

Output:

blogpost

blogpost

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 the below content:

Code:

hosts:
all
tasks:
name: Here we use the template to copy a file with fact data to remote hosts 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:

blogpost

blogpost

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 the 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 the playbook in the jinja2 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:

blogpost

blogpost

Here, please note in the jinja2 template, the endfor isendbelow. 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:

blogpost

So, here ‘-’ is used for removing white spaces at the end of the block.

Conclusion

In Ansible, we can utilize the layout module to work effectively and produce quantifiable outcomes. There are different modules too like a duplicate module which gives a few highlights of the format module to work with Jinja2 templating. Be that as it may, utilizing more element-prepared modules makes the errands execution smooth and influence the user to the full degree empowering you to accomplish more undertakings with less code.

Ansible
Ansible Template
Linux
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-author
Nilima Paul
Technology Security Analyst
Techiio-followerTechiio-followerTechiio-follower
201 Blog Posts
0 Discussion Threads
Trending Technologies
15
Software40
DevOps46
Frontend Development24
Backend Development20
Server Administration17
Linux Administration26
Data Center24
Sentry24
Terraform23
Ansible83
Docker70
Penetration Testing16
Kubernetes21
NGINX20
JenkinsX17
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance