Pcornel
Browse topics
Options
Recent
Favorites
Automation · Technical reference

Templates · Ansible

Direct answer

Ansible templates use Jinja2 to build configuration files from variables before copying them to the target with the template module. Check the rendered file on the target, not just the template source, when a service misbehaves.

When to use this

Use it when configuring, checking or troubleshooting Templates in Ansible.

First 30 seconds

1. template module renders Jinja2 files to targets.

2. validate with native tools (sshd -t -f %s).

3. Mode/owner set explicitly.

4. Handlers restart on change.

5. Test rendering.

Operational verification

vars + .j2 → rendered file → validate → handler

Context & applicabilityCommon mistakes · platform/version boundaries
Platform / version applicability
Ansible automation
applies
Structured applicability is explicit Topic metadata; it does not infer compatibility beyond the stored rows.
At a glance

Operational reference

Fast context before you go deeper.

PlatformAnsible automation · Templates owns Ansible template delivery semantics. Jinja2 expression behavior and target application configuration have adjacent ownership.
Scope / prerequisiteUse it when configuring, checking or troubleshooting Templates in Ansible.
What next?3 suggested next steps
Evidence1
On this pageJump to a specific section
Operational workflow

Verification & Troubleshooting

Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.

3 steps Observe → verify → isolate → change minimally → repeat the original test.
ObservedRecord what the original test actually shows

Capture the concrete output, symptom, packet, log or counter before changing anything.

Step 1 Operational evidence path Verify boundary

template source → variables/Jinja2 render → rendered content → destination file/payload → handler/service validation → workload state

Healthy evidence: source and inputs are traceable, rendered output is validated, destination metadata is correct and consuming service acceptance is verified.

Step 2 Failure modes and next checks Narrow the fault

Suspicious evidence: wrong variable value, malformed render, secret leakage, missing handler/reload or assuming file change equals service health.

Step 3 Common mistakes Narrow the fault
  • Templates rendering empty values silently.
  • Wrong file permissions on deployed templates.
Evidence references

Evidence · 1 on this Topic

Expected observations and documentation recipes are reference material. Record actual output, device identity, scope and time before claiming a verified result.

observation · 1
Key points
  • Template source looks correct ≠ rendered output correct; variable precedence and conditionals alter content.
  • Rendered file delivered ≠ service accepted it; syntax validation/reload may still fail.
  • No task change ≠ template source unchanged; current rendered content may coincidentally match.
  • Template output ≠ secret-safe automatically; sensitive values can be materialized on disk/logs.
Key checks
  • ansible-playbook site.yml --check --diff — template diff
  • template module with validate: — syntax check
  • backup: yes for important files — recovery
ReferenceWhat it is1 section · depth on demand

Core model

Validate template source/version, effective variables/types, render result, destination ownership/mode and consuming service validation/reload separately.

ReferenceImportant terms & boundaries2 sections · depth on demand

What this shows and what it does not

Successful template deployment shows rendered content reached its destination, but it does not show the consuming service parsed, reloaded or used it successfully.

In depth

The template module renders a Jinja2 template with the host's variables and writes the result to the target, reporting changed only when the content differs. For network devices, templates usually render configuration that is then applied with a config module.

- name: Render access switch config
  ansible.builtin.template:
    src: access_switch.j2
    dest: "build/{{ inventory_hostname }}.cfg"
  delegate_to: localhost

- name: Apply rendered config
  cisco.ios.ios_config:
    src: "build/{{ inventory_hostname }}.cfg"
  register: result

For servers, validate: checks the file before replacing the original (validate: 'visudo -cf %s', nginx -t -c %s), and backup: true keeps the previous version.

Source basisSource attribution states what this section is based on; it is not independent proof that the current environment matches it.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Technical verification history

Verification history & basis

When and against what this Topic was checked. Later text edits do not count as a new check.

2026-09-14 · vendor docAnsible automation current

Authoritative/upstream documentation reviewed for bounded claims in Ansible > Templates. Documentation verification only; this is not observed lab or production Evidence.

Basis: https://docs.ansible.com/ansible/latest/ · confidence 94%
Terminology layer

Terms & aliases

Hover a linked term for a quick definition; click it to open its glossary entry. Aliases are other names that find this Topic in search.

Aliases that resolve here
ansible templatetemplate modulejinja template filerendered config
Embedded reference data

Quick lookup tables

Templates Ansible boundary map

Fast input/selection, execution/result and target-proof boundaries for Templates.

BoundaryHealthy evidenceSuspicious evidence
Inputs / selectionCorrect inventory, hosts, variables/facts and content are explicitWrong scope, stale facts, hidden precedence or content-version drift
ExecutionConnection, privilege, module/task context and ordering match intentUnreachable, wrong execution host, privilege/plugin failure or misleading skip/change state
Result semanticsPer-host/item results are preserved and interpreted correctlyAggregate recap hides partial/skipped/handled failures
Target proofResource/workload read-back and rerun confirm convergenceGreen Ansible status treated as end-to-end acceptance
Confirmation

Confirm action

Continue?