Pcornel
Browse topics
Options
Recent
Favorites
Automation · Technical reference

Jinja2 · Templates

Direct answer

Jinja2 is the Python template engine used by Ansible, Salt, NetBox and Nornir, with {{ variables }}, {% if %} and {% for %} blocks, filters and macros.

When to use this

Use this when writing Jinja2 templates for Ansible, Salt, NetBox or Nornir.

First 30 seconds

1. Jinja2: {{ variables }}, {% control %}, {# comments #}, filters.

2. Used by Ansible, Salt, many tools.

3. StrictUndefined.

4. Whitespace control.

5. Autoescape for HTML.

Operational verification

data + Jinja2 → rendered text

Context & applicabilityCommon mistakes · platform/version boundaries
Platform / version applicability
template rendering 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.

Platformtemplate rendering automation · Automation implementations are tool and platform specific. Use the adjacent Domain for the technology being automated and this Domain for automation behavior.
Scope / prerequisiteUse this when writing Jinja2 templates for Ansible, Salt, NetBox or Nornir.
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.

2 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 Verification path Verify boundary

The negative fixture should deliberately exercise risks such as undefined values, unsafe escaping, implicit type conversion, environment-specific filters, secret exposure, whitespace-sensitive syntax and valid render producing invalid target configuration, choosing the cases most relevant to template environment, undefined policy, filters/tests, escaping, whitespace and context schema needed for deterministic rendering.

Step 2 Common mistakes Narrow the fault
  • Undefined variables rendering as empty text (use StrictUndefined).
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
  • Jinja2 uses {{ }} for values and {% %} for logic.
  • StrictUndefined fails on missing variables.
  • Whitespace control avoids blank lines.
Key checks
  • python3 -c "from jinja2 import Environment,StrictUndefined;print(Environment(undefined=StrictUndefined).from_string('{{ x }}').render(x=1))" — render with strict undefined
  • ansible <host> -m debug -a "msg={{ inventory_hostname }}" — Ansible Jinja2
  • trim_blocks/lstrip_blocks — whitespace
ReferenceWhy it matters1 section · depth on demand

What it is

Jinja2 is the templating language used by Ansible, Salt and many tools: {{ var }} outputs values, {% %} controls logic.

ReferenceImportant terms & boundaries2 sections · depth on demand

In depth

Jinja2 is the templating language used by Ansible, Salt, NetBox export templates and many Python tools. A template is text with placeholders and logic — {{ variable }} for values, {% %} for loops and conditions — rendered with data into configuration files.

hostname {{ hostname }}
{% for vlan in vlans %}
vlan {{ vlan.id }}
 name {{ vlan.name }}
{% endfor %}
{% if snmp_enabled | default(false) %}
snmp-server location {{ site | upper }}
{% endif %}
from jinja2 import Environment, FileSystemLoader, StrictUndefined
env = Environment(loader=FileSystemLoader("templates"), undefined=StrictUndefined, trim_blocks=True, lstrip_blocks=True)
print(env.get_template("access.j2").render(hostname="sw01", vlans=[{"id": 20, "name": "USERS"}]))

StrictUndefined makes missing variables an error instead of silently rendering empty text.

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 doctemplate rendering automation current

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

Basis: https://jinja.palletsprojects.com/en/stable/templates/ · confidence 94%
Depth when you need it

Deep reference

Healthy vs suspicious evidence

Output interpretation

For template environment, undefined policy, filters/tests, escaping, whitespace and context schema needed for deterministic rendering, prefer typed fields and stable IDs over human presentation text.

Confirmation

Confirm action

Continue?