Use this when templates render wrong output or fail on undefined variables.
Template Troubleshooting
Troubleshoot templates by rendering with test data and diffing the output, enabling StrictUndefined to catch missing variables, checking whitespace control and filter usage.
1. Problems: undefined variables, wrong types, whitespace, escaping, filter not found.
2. Render locally with the same data.
3. StrictUndefined reveals missing data.
4. Check collection/filter availability.
5. Add tests.
data + template → error → fix
Context & applicabilityCommon mistakes · platform/version boundaries
Operational reference
Fast context before you go deeper.
python -c "from jinja2 import Environment; Environment().parse(open('<template.j2>').read())"Primary Commands
Configuration and operational commands for this Topic.
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 Command read-back and what it shows Verify boundary
Known-good is the template parses, renders deterministically with the intended context and the rendered artifact passes target validation and produces the expected final state; broken state is parse error, undefined/filter/render error, nondeterministic/wrong rendered values or target rejection despite successful Jinja parsing. The Jinja parse command shows template syntax only, but it does not show variable completeness, rendered semantics or downstream configuration acceptance.
Step 2 Common mistakes Narrow the fault
- Not rendering locally to inspect output.
Step 3 Observation-driven branch and next-check logic Narrow the fault
If Jinja parse fails, fix template syntax first; if parse passes but rendering fails, inspect missing variables/filter context; if rendering succeeds but target rejects output, validate rendered artifact against target syntax/schema and compare with known-good output.
- Undefined variables are the most common problem.
- Whitespace can break formats.
- Filters may behave unexpectedly.
python3 -c "from jinja2 import Environment,StrictUndefined" + render with sample— reproduceansible <host> -m debug -a 'var=<variable>'— inspect variable- Whitespace/indentation in output — formatting
ReferenceHow it works1 section · depth on demand
How it works
Template Troubleshooting is governed by template source syntax, variable/context resolution, rendering filters/macros and the consumer that parses/applies rendered output. Jinja parses template syntax into an AST, resolves variables/filters/macros against supplied context and renders text that another tool then interprets; parse validity, render correctness and consumer validity are separate stages. Template loader paths, custom filters, variable sources, escaping rules and downstream configuration/schema validation are distinct boundaries. The important state separation is that a template can parse successfully while rendering fails on undefined data, and rendered text can be syntactically valid for Jinja but invalid for its target system.
ReferenceImportant terms & boundaries1 section · depth on demand
In depth
Template troubleshooting deals with errors during rendering and with output that renders but is wrong.
| Symptom | Likely cause | Fix |
|---|---|---|
UndefinedError | Missing variable or typo | Check data; use StrictUndefined to catch early |
| Empty values in output | Undefined without strict mode | Enable strict mode; add default() only where valid |
TemplateSyntaxError | Unclosed block, wrong delimiter | Line number in error; lint templates |
| Extra blank lines/indentation | Whitespace control | trim_blocks, lstrip_blocks, {%- |
| Wrong type behaviour | String vs number ('10' > '9' is false) | Convert with int/string filters |
| Output order changes each run | Unsorted dicts/sets | Sort before looping |
Render one host with debug output (ansible -m debug -a "msg={{ lookup('template','x.j2') }}") to isolate problems quickly.
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 Templates > Template Troubleshooting. Documentation verification only; this is not observed lab or production Evidence.
Basis: https://jinja.palletsprojects.com/en/stable/templates/ · confidence 94%