Use this when writing Jinja2 templates for Ansible, Salt, NetBox or Nornir.
Jinja2 · Templates
Jinja2 is the Python template engine used by Ansible, Salt, NetBox and Nornir, with {{ variables }}, {% if %} and {% for %} blocks, filters and macros.
1. Jinja2: {{ variables }}, {% control %}, {# comments #}, filters.
2. Used by Ansible, Salt, many tools.
3. StrictUndefined.
4. Whitespace control.
5. Autoescape for HTML.
data + Jinja2 → rendered text
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 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 · 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.
- Jinja2 uses {{ }} for values and {% %} for logic.
- StrictUndefined fails on missing variables.
- Whitespace control avoids blank lines.
python3 -c "from jinja2 import Environment,StrictUndefined;print(Environment(undefined=StrictUndefined).from_string('{{ x }}').render(x=1))"— render with strict undefinedansible <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.
Example
{% for i in interfaces %}
interface {{ i.name }}
description {{ i.desc | default('unused') }}
{% endfor %}
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 > Jinja2. Documentation verification only; this is not observed lab or production Evidence.
Basis: https://jinja.palletsprojects.com/en/stable/templates/ · confidence 94%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.