Skip to main content

Understanding Ansible Templates

Ansible is a powerful automation tool that provides a simple way to automate apps and IT infrastructure. One of its most exciting features is the use of templates. In this tutorial, we will go through the concept of Ansible templates, how they work, and how to use them in Ansible playbooks.

What are Ansible Templates?

Ansible templates are a way to create files that can be modified dynamically. They use the Jinja2 templating engine for Python, allowing you to use variables to represent values. This feature makes it possible to automate the configuration of multiple systems with the same playbook, even if they require slightly different configurations.

Jinja2 Templating Engine

Jinja2 is a modern and designer-friendly templating language for Python, modelled after Django’s templates. It is fast, widely used and secure, and allows you to utilize variables, loops, and conditionals in your templates, making them more dynamic.

How To Create An Ansible Template

Creating a template in Ansible is straightforward. You simply create a file with the .j2 extension (for Jinja2) and use special syntax to insert variables.

Here is a simple example:

Hello, my name is {{ name }}.

In this template, {{ name }} is a variable. When Ansible uses this template, it will replace {{ name }} with the value of the variable name.

How To Use An Ansible Template In A Playbook

To use a template in a playbook, you use the template module. Here is an example:

---
- hosts: web_servers
tasks:
- name: Create a configuration file from a template
template:
src: /path/to/template.j2
dest: /path/to/destination

In this example, the template module will take the template at /path/to/template.j2, replace any variables in it with their values, and save the result at /path/to/destination.

Variables In Ansible Templates

You can define variables in your playbook and use them in your templates. Here is an example:

---
- hosts: web_servers
vars:
name: John
tasks:
- name: Create a configuration file from a template
template:
src: /path/to/template.j2
dest: /path/to/destination

In this playbook, we define a variable name with the value John. The template module will replace {{ name }} with John in the template.

Loops And Conditionals In Ansible Templates

Jinja2 supports loops and conditionals, which you can use in your Ansible templates. Here is an example template that uses a loop:

{% for item in items %}
- {{ item }}
{% endfor %}

This template will create a list of items, with one item per line.

Conclusion

Ansible templates are a powerful tool that can make your playbooks more flexible and reusable. They allow you to use variables, loops, and conditionals in your files, making them dynamic. With templates, you can automate the configuration of systems that have slightly different requirements with the same playbook.

Remember, practice is key when learning any new technology. Try creating your own templates and using them in your playbooks to get a feel for how they work.