## Creating and Using Templates in Ansible
Ansible templates are a powerful feature for creating customized configuration files dynamically on managed hosts. They allow you to embed variables and control structures directly into static file content, which is then rendered on the target system. Ansible uses the Jinja2 templating language for this purpose.
Why Use Templates?
Templates are essential for:
- Dynamic Configuration: Generating configuration files (e.g., Apache virtual hosts, Nginx server blocks, user profiles) that vary based on host-specific data, group variables, or facts.
- Reduced Duplication: Maintaining a single template file and populating it with different data, rather than having multiple slightly different static configuration files.
- Idempotency: The `template` module, like other Ansible modules, ensures that the target file is only updated if its content changes, maintaining idempotency.
The `template` Module
The core of using templates in Ansible is the `template` module.
It takes a source template file (on the control node) and renders it on the managed node, substituting Jinja2 expressions with actual values.
Key parameters for the `template` module:
- `- src`: Specifies the path to the template file on the Ansible control node. This path is typically relative to the playbook or defined in `ansible.cfg`. A common convention is to place templates in a `templates/` subdirectory relative to the playbook.
- `- dest`: Specifies the absolute path where the rendered file will be created or updated on the managed node.
- `- owner`, `- group`, `- mode`: Optional parameters to set the ownership, group, and file permissions of the destination file on the managed node, similar to the `copy` module.
Jinja2 Syntax Basics
Understanding basic Jinja2 syntax is crucial for writing effective templates:
- Expressions and Variables: Use `{{ variable_name }}` to insert the value of an Ansible variable. For example, `ServerName {{ ansible_hostname }}`.
- Statements and Control Structures: Use `{% statement %}` for logic like loops (`for`) and conditionals (`if/else`).
- Example `for` loop:
```jinja2
{% for user in users %}
User {{ user.name }} with UID {{ user.uid }}
{% endfor %}
```
```jinja2
{% if enable_feature %}
Feature is enabled.
{% else %}
Feature is disabled.
{% endif %}
```
- Comments: Use `{# comment #}` for comments within the template that will not appear in the rendered output.
Variables used within templates can originate from various Ansible sources, including inventory variables, `group_vars`, `host_vars`, `vars` sections in playbooks, `set_fact` tasks, and Ansible facts.
What templating language does Ansible use for its templates?
Jinja2
tap to reveal
Which Ansible module is used to deploy a template file to a managed node?
`template` module
tap to reveal
In the `template` module, what does the `src` parameter specify?
The path to the template file on the Ansible control node.
tap to reveal
In the `template` module, what does the `dest` parameter specify?
The absolute path where the rendered file will be created on the managed node.
tap to reveal
What is the Jinja2 syntax for embedding an Ansible variable within a template?
`{{ variable_name }}`
tap to reveal
What is the Jinja2 syntax for control structures like loops or conditionals within a template?
`{% statement %}`
tap to reveal
Where are template files typically stored relative to an Ansible playbook?
In a `templates/` subdirectory.
tap to reveal
Name two optional parameters for the `template` module to manage the destination file's permissions.
`owner`, `group`, `mode` (any two)
tap to reveal