Ansible templates are a powerful feature that enables dynamic generation of configuration files and scripts during automation workflows. Using Jinja2 templating, Ansible templates allow for flexibility and adaptability in creating infrastructure and application configurations. This article explains what templates are, how they work, and their benefits.
What Are Ansible Templates?
Ansible templates are Jinja2-based files used to create dynamic configuration files or scripts. These templates can include variables, conditionals, loops, and filters, allowing for highly customized output tailored to the specific needs of managed systems.
Key Features:
- Dynamic Content: Generate files with runtime-specific data.
- Customization: Use variables and logic to create tailored configurations.
- Reusability: Write once and reuse templates across multiple tasks.
How Do Ansible Templates Work?
Templates are stored as .j2
files and processed using Ansible’s template
module, which replaces variables and applies logic before deploying the file to the target system.
Example Template File
nginx.conf.j2:
server {
listen {{ nginx_port }};
server_name {{ nginx_server_name }};
location / {
root {{ nginx_root }};
index index.html;
}
}
Example Playbook Using a Template
- name: Configure Nginx
hosts: webservers
vars:
nginx_port: 80
nginx_server_name: example.com
nginx_root: /var/www/html
tasks:
- name: Deploy Nginx configuration
template:
src: nginx.conf.j2
dest: /etc/nginx/nginx.conf
mode: 0644
This playbook:
- Substitutes variables (e.g.,
{{ nginx_port }}
) with their defined values. - Saves the rendered configuration file to
/etc/nginx/nginx.conf
.
Common Features of Templates
1. Variables:
Insert variables into templates for dynamic values.
server_name {{ nginx_server_name }};
2. Conditionals:
Add logic to generate conditional content.
{% if ssl_enabled %}
listen 443 ssl;
{% else %}
listen 80;
{% endif %}
3. Loops:
Use loops to iterate over lists or dictionaries.
upstream backend {
{% for server in backend_servers %}
server {{ server }};
{% endfor %}
}
4. Filters:
Transform data with Jinja2 filters.
upstream backend {
{{ backend_servers | join(' ') }}
}
5. Comments:
Use comments for clarity.
# This is a comment
Benefits of Ansible Templates
Dynamic Configuration: Templates enable you to create files tailored to runtime conditions and variables.
Efficiency: Avoid manual editing by automating the generation of configuration files.
Reusability: Write a single template and reuse it for multiple hosts or environments.
Consistency: Ensure all configurations follow a standardized format.
Best Practices for Using Templates
Use Meaningful Variable Names: Define clear and descriptive variable names to make templates easier to understand.
Test Templates: Use the
ansible-playbook
command in check mode (--check
) to validate templates before applying changes.Organize Template Files: Store templates in the
templates/
directory of your project or role.Secure Sensitive Data: Use Ansible Vault to encrypt variables containing sensitive information.
Leverage Jinja2 Features: Take advantage of Jinja2’s rich features like filters, conditionals, and loops for maximum flexibility.
Conclusion
Ansible templates are a versatile tool for generating dynamic and reusable configuration files. By leveraging Jinja2 templating, you can adapt configurations to the needs of your automation workflows, ensuring consistency, efficiency, and customization.
Learn More About Ansible Templates
Subscribe to the YouTube channel, Medium, and Website, X (formerly Twitter) to not miss the next episode of the Ansible Pilot.Academy
Explore real-world examples of templates in Ansible by Examples.
Donate
Support this project by contributing today.