Use it when configuring, checking or troubleshooting Jinja2 in Ansible.
Jinja2 · Ansible
Jinja2 is the expression and templating layer Ansible uses to resolve variables, filters, tests and control structures in many fields before modules receive final arguments.
1. Jinja2 in Ansible for templating and expressions.
2. Filters and tests.
3. Undefined variables error by default (good).
4. Keep logic simple.
5. Use ansible.utils for network filters.
expression → Jinja2 → value
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
raw vars/facts → Jinja2 expression/filter/test → rendered value/type → task/template consumer → action/result
Healthy evidence: inputs/types are known, expression evaluates predictably and the consumer receives the intended final value/type.
Step 2 Failure modes and next checks Narrow the fault
Suspicious evidence: undefined masked by defaults, string/boolean confusion, filter-version differences, quoting issues or debugging only the source expression without final value.
A successfully rendered expression shows Jinja2 produced a value, but it does not show the value is correct for the receiving module or target application.
Step 3 Common mistakes Narrow the fault
- Missing
| default()on optional values. - Complex logic inside 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.
- Expression parses ≠ value semantically correct; type coercion and defaults can hide bad inputs.
- Undefined handled by default ≠ missing data harmless; fallback can mask source errors.
- Rendered string ≠ original data type always; consumer expectations matter.
- Jinja2 logic ≠ task conditional scope automatically; templating and task controls have distinct contexts.
ansible localhost -m debug -a "msg={{ 'a,b' | split(',') }}"— test expression- Filters from collections (ansible.utils.ipaddr) — requirements
- StrictUndefined behaviour (error on undefined) — safety
ReferenceWhat it is1 section · depth on demand
Core model
Debug Jinja2 by input types/undefined values, expression/filter/test behavior, escaping/render context and final rendered value at the consumer boundary.
ReferenceImportant terms & boundaries2 sections · depth on demand
What this shows and what it does not
Generic programming logic and target config syntax remain separate.
In depth
Ansible uses Jinja2 everywhere: in templates, in task arguments ("{{ var }}"), in conditions (when:) and in loops. Ansible adds its own filters and tests on top of standard Jinja2 — for IP addresses, data manipulation and network parsing.
- debug:
msg: >-
{{ interfaces | selectattr('enabled') | map(attribute='name') | list }}
- set_fact:
mgmt_netmask: "{{ mgmt_cidr | ansible.utils.ipaddr('netmask') }}"
- name: Only on IOS XE 17+
when: ansible_net_version is version('17.0', '>=')
debug: msg=ok
In when: conditions, write expressions without braces. Quote values that start with {{ in YAML. Prefer moving complex expressions into set_fact or a custom filter for readability.
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 > Jinja2. 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
Jinja2 Ansible boundary map
Fast input/selection, execution/result and target-proof boundaries for Jinja2.
| 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 |