Ansible Template Variables Tutorial
Introduction
In Ansible, templates are files that contain variables and are used to dynamically generate configuration files, scripts, or other text-based files. Template variables allow for the customization of these files based on the needs of your infrastructure.
What Are Template Variables?
Template variables in Ansible are placeholders within a template file that are replaced with actual values when the template is processed. These variables are defined in your Ansible playbooks or inventory files.
Defining Template Variables
You can define variables in several ways in Ansible:
- In the inventory file
- In playbooks
- As facts gathered by Ansible
Here's an example of defining variables in a playbook:
--- - name: Example Playbook hosts: all vars: my_var: "Hello, World!" tasks: - name: Debug variable debug: msg: "{{ my_var }}"
Creating a Template File
Template files in Ansible use the Jinja2 templating engine. A template file typically has the .j2
extension. Here's an example of a simple template file:
# template.j2 Welcome to {{ my_var }}!
Using the Template Module
To apply a template file in your playbook, you use the template
module. Here's an example playbook that uses the template module:
--- - name: Apply Template Example hosts: all vars: my_var: "Ansible" tasks: - name: Copy template file template: src: template.j2 dest: /tmp/template_output.txt
This playbook will replace the {{ my_var }}
placeholder in template.j2
with the value "Ansible" and copy the rendered file to /tmp/template_output.txt
.
Rendering the Template
When the playbook is run, the template is rendered with the variable values. The content of /tmp/template_output.txt
would be:
Complex Variables
Template variables can also be more complex data structures like dictionaries and lists. Here's an example:
--- - name: Complex Variables Example hosts: all vars: user_info: name: John Doe email: john.doe@example.com tasks: - name: Copy complex template template: src: complex_template.j2 dest: /tmp/complex_template_output.txt
And the corresponding template file complex_template.j2
:
# complex_template.j2 User Information: Name: {{ user_info.name }} Email: {{ user_info.email }}
Conditional Statements
Jinja2 templates also allow for the use of conditional statements. Here's an example:
# conditional_template.j2 {% if user_info.name %} Hello, {{ user_info.name }}! {% else %} Hello, Guest! {% endif %}
This template will greet the user by name if user_info.name
is defined, otherwise, it will greet "Guest".
Loops in Templates
You can also use loops in your templates to iterate over lists or dictionaries. Here's an example:
# loop_template.j2 Users: {% for user in users %} - {{ user.name }} ({{ user.email }}) {% endfor %}
Conclusion
Template variables in Ansible provide a powerful way to dynamically generate files based on the state of your infrastructure. By using the Jinja2 templating engine, you can create complex and flexible templates that adapt to your needs.
With the knowledge of defining variables, creating template files, and using the template module, you can now start leveraging the full potential of templates in Ansible.