What are Ansible templates and how does Jinja2 work in playbooks?
Understand Ansible templates and Jinja2 — how the template module renders variables, loops, conditionals and filters into host-specific config files.
Expected Interview Answer
Ansible templates are configuration files containing Jinja2 placeholders that Ansible renders with real variable values on the control node, then copies to the target host using the template module.
A template file (conventionally ending in .j2) mixes static text with Jinja2 expressions like {{ variable }}, control structures like {% for %} and {% if %}, and filters like | default or | upper. When the template module runs, Jinja2 evaluates these against the host's variables, facts, and inventory data to produce a fully rendered file. This lets one template generate host-specific configs — different ports, IPs, or hostnames — from a single source of truth.
- Generate host-specific configs from one file
- Inject variables, facts and inventory data dynamically
- Use loops and conditionals to build complex files
- Apply filters for defaults, formatting and transformation
- Keep configuration DRY and version-controlled
AI Mentor Explanation
A template is like a match-programme master with blanks for the two team names, the venue, and the toss result. The printer keeps one master and, for each fixture, fills the blanks from that day's details to produce a bespoke programme. Jinja2 is the fill-in engine turning one master into every match-specific booklet automatically.
Step-by-Step Explanation
Step 1
Author the .j2 file
Write a config file with static text plus Jinja2 tokens like {{ server_name }} and blocks like {% for %} / {% if %}, saved under templates/.
Step 2
Define variables
Provide values via group_vars, host_vars, play vars, or gathered facts such as ansible_default_ipv4.address.
Step 3
Call the template module
In a task, use ansible.builtin.template with src pointing to the .j2 file and dest to the target path on the host.
Step 4
Render on the control node
Ansible evaluates the Jinja2 expressions against each host's variables, producing a host-specific rendered file.
Step 5
Deliver and act
The rendered file is copied to the target; combine with notify to restart the service only when the file changed.
What Interviewer Expects
- Templates use Jinja2 and are applied by the template module
- Difference between template (renders variables) and copy (verbatim)
- Knowledge of {{ }}, {% %}, and common filters like default and upper
- Awareness that rendering happens on the control node
- Use of loops, conditionals and facts inside templates
- Convention of the .j2 extension and templates/ directory
Common Mistakes
- Confusing the template module with the copy module
- Thinking rendering happens on the managed node, not the control node
- Forgetting to quote or default variables that may be undefined
- Mixing up expression {{ }} and statement {% %} delimiters
- Not using notify to restart services after the template changes
Best Answer (HR Friendly)
“Ansible templates are configuration files with blanks that Ansible fills in with the right values for each server before delivering them. The engine behind it, Jinja2, swaps in variables and can even loop or make decisions, so one file can produce correct settings for many different machines.”
Code Example
- hosts: web
become: true
vars:
server_name: example.com
worker_ports: [8080, 8081, 8082]
tasks:
- name: Render nginx config from template
ansible.builtin.template:
src: nginx.conf.j2
dest: /etc/nginx/nginx.conf
owner: root
mode: '0644'
notify: restart nginxserver {
server_name {{ server_name }};
listen {{ http_port | default(80) }};
{% for port in worker_ports %}
upstream_backend {{ port }};
{% endfor %}
{% if enable_ssl | default(false) %}
ssl on;
{% endif %}
}Follow-up Questions
- What is the difference between the template and copy modules?
- On which node does Jinja2 rendering happen — control or managed?
- How do you provide a default value for a possibly-undefined variable?
- How do you loop over a list inside a Jinja2 template?
- What are some commonly used Jinja2 filters in Ansible?
- How can you access gathered facts inside a template?
MCQ Practice
1. Which module renders a Jinja2 file and copies the result to the host?
The template module processes the .j2 file through Jinja2 and delivers the rendered output, unlike copy which transfers files verbatim.
2. Where does Jinja2 template rendering take place?
Ansible evaluates Jinja2 templates on the control node, then transfers the fully rendered file to the managed host.
3. What does {{ http_port | default(80) }} do?
The default filter supplies a fallback value (80) when the variable http_port is not defined, otherwise it uses the variable's value.
Flash Cards
What module applies a Jinja2 template? — ansible.builtin.template — it renders the .j2 file and copies the output to the target host.
template vs copy? — template renders Jinja2 variables/logic; copy transfers a file verbatim with no substitution.
Where is a template rendered? — On the control node, before the resulting file is sent to the managed host.
What does the default filter do? — Supplies a fallback value when a variable is undefined, e.g. {{ port | default(80) }}.
Expression vs statement delimiters? — {{ }} outputs a value; {% %} runs control logic like for-loops and if-conditions.