Use it when configuring, checking or troubleshooting Templates in Ansible.
Templates · Ansible
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.
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.
vars + .j2 → rendered file → validate → handler
Context & applicabilityCommon mistakes · platform/version boundaries
Operational reference
Fast context before you go deeper.
Verification & Troubleshooting
Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.
Use the operational verification above to establish the expected state.
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 · 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.
- 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.
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.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Verification history & basis
When and against what this Topic was checked. Later text edits do not count as a new check.
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%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.
Quick lookup tables
Templates Ansible boundary map
Fast input/selection, execution/result and target-proof boundaries for Templates.
| Boundary | Healthy evidence | Suspicious evidence |
|---|---|---|
| Inputs / selection | Correct inventory, hosts, variables/facts and content are explicit | Wrong scope, stale facts, hidden precedence or content-version drift |
| Execution | Connection, privilege, module/task context and ordering match intent | Unreachable, wrong execution host, privilege/plugin failure or misleading skip/change state |
| Result semantics | Per-host/item results are preserved and interpreted correctly | Aggregate recap hides partial/skipped/handled failures |
| Target proof | Resource/workload read-back and rerun confirm convergence | Green Ansible status treated as end-to-end acceptance |