# ansible / configuration / templates

Dynamic Configs with Templates

In Ansible, a template is a text file that allows dynamic content generation. Stop maintaining 50 separate configuration files for 50 servers; build one template and let Ansible inject the unique variables at runtime.

$ cat /etc/apache2/apache.conf
TemplateServerName {{ ansible_hostname }}
Result 1ServerName web-01.example.com
Result 2ServerName web-02.example.com
StatusDynamically Rendered
Jinja2
Engine
.j2
Extension
# introduction

Why Use Templates?

Deploying identical files to every server is easy with the copy module. But when a file needs to differ slightly per host (like injecting specific IP addresses or unique hostnames), you need a template.

efficiency

Reduces Duplication

One single template file can serve your entire infrastructure, automatically adjusting itself to fit the specific server it is deployed to.

reliability

Ensures Consistency

By using a single source of truth, you ensure standard configurations across the fleet while still allowing for dynamic variations.

# jinja2

Template Files & Syntax

apache.conf.j2JINJA2 TEMPLATE
1 2 3 4 5
# Dynamically injected Hostname
ServerName {{ ansible_hostname }}

# Dynamically injected IP Address
Listen {{ ansible_default_ipv4.address }}:80

What is a Template?

A template is simply a normal text file that contains placeholders. These placeholders are written using Jinja2 syntax, which is recognized by double curly braces {{ }}.

The .j2 Extension

Templates typically use the .j2 file extension (e.g., apache.conf.j2). While the extension is not strictly mandatory, it is the universal Ansible standard and helps text editors apply the correct syntax highlighting.

# module

The Template Module

To deploy a template to a managed node, you use the template module inside your playbook. It works very similarly to the copy module, but it processes the Jinja2 variables before transferring the file.

deploy.ymlYAML
1 2 3 4 5 6 7
---
- name: Deploy Web Configuration
  hosts: webservers
  tasks:
    - name: Deploy Apache configuration via template
      template:
        src: apache.conf.j2
        dest: /etc/apache2/apache.conf
# advanced

Advanced Features (Logic)

Programming Logic

Jinja2 isn't just for variable substitution. It is a fully-featured templating engine that allows you to use programming logic like Conditionals and Loops directly inside your configuration files.

  • {% if %}

    Write configuration blocks that only appear if certain conditions (like the OS family) are met.

  • {% for %}

    Iterate over lists of variables (like an array of IPs or packages) to generate repeated config lines automatically.

advanced.conf.j2JINJA2
# Condition Example
{% if ansible_os_family == "Debian" %}
PackageManager apt
{% endif %}
# Loop Example
{% for pkg in packages %}
Install {{ pkg }}
{% endfor %}
# summary

Final Summary

TL;DR

In Ansible, templates are used to generate dynamic configuration files using Jinja2 syntax. The template module processes these files by replacing variables with actual values at runtime and deploying them to the target systems.

RESULT

This approach allows a single, clean configuration file to be successfully reused across hundreds of unique servers.