AnsiblePilot — Master Ansible Automation

AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,100 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.

Popular Topics

About Luca Berton

Luca Berton is an Ansible automation expert, author of "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example" published by Apress, and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.

Ansible Templates: Jinja2 Template Module Complete Guide

By Luca Berton · Published 2024-01-01 · Category: troubleshooting

Complete guide to Ansible templates. Use Jinja2 templating with variables, loops, conditionals, and filters to generate dynamic configuration files.

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:

Example Playbook Using a Template

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.

2. Conditionals: Add logic to generate conditional content.

3. Loops: Use loops to iterate over lists or dictionaries.

4. Filters: Transform data with Jinja2 filters.

5. Comments: Use comments for clarity.

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

Basic Template Usage

Template with Variables

Loops in Templates

Conditionals

Filters

Template Module Parameters

| Parameter | Description | |-----------|-------------| | src | Template file (relative to templates/) | | dest | Destination path on remote | | owner | File owner | | group | File group | | mode | File permissions | | backup | Create backup before overwriting | | validate | Validation command before deploy | | force | Replace even if dest exists |

Validate Before Deploy

Template Directory Structure

Advanced: Macros

FAQ

template vs copy?

template processes Jinja2 variables and logic. copy transfers files as-is. Use template when you need dynamic content.

Where does Ansible look for templates? templates/ directory relative to the playbook templates/ directory inside the role The absolute or relative path you specify

How do I include another template?

Can I template non-text files?

No — templates are for text files. Use copy for binary files.

Basic Template

Variables in Templates

Conditionals

Loops

Filters

Practical: Application Config

Template with Validation

Template Inheritance (Macros)

FAQ

template vs copy?

template processes Jinja2 (variables, loops, conditionals). copy transfers files as-is. Use template for dynamic content, copy for static files.

Where do templates go?

Convention: templates/ directory in your role or project root. Ansible looks there automatically.

Can I template a binary file?

No — template is for text files only. Use copy for binaries.

Related ArticlesAnsible Template GuideAnsible Vault GuideAnsible when Conditional GuideAnsible Check Mode GuideAnsible Loops Guide

Category: troubleshooting

Browse all Ansible tutorials · AnsiblePilot Home