Pcornel
Browse topics
Options
Recent
Favorites
Automation · Technical reference

Jinja2 · Ansible

Direct answer

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.

When to use this

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

First 30 seconds

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.

Operational verification

expression → Jinja2 → value

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 · Jinja2 owns Ansible expression/render semantics. Generic programming logic and target config syntax remain separate.
Scope / prerequisiteUse it when configuring, checking or troubleshooting Jinja2 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

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 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
  • 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.
Key checks
  • 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.

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 > Jinja2. 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
jinja2 ansibleansible filtersansible teststemplate expression
Embedded reference data

Quick lookup tables

Jinja2 Ansible boundary map

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

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?