Jinja2 Syntax
Reference guide for Jinja2 templating syntax.
Basic Syntax
| Syntax | Purpose | Example |
|---|---|---|
{{ }} | Output expression | {{ hostname }} |
{% %} | Statement (logic) | {% if enabled %} |
{# #} | Comment | {# This is a comment #} |
Control Structures
Conditionals
{% if enable_snmp %}
snmp-server community {{ community }} RO
{% endif %}
{% if environment == 'production' %}
logging host 10.0.0.1
{% else %}
logging host 10.0.0.2
{% endif %}Loops
{% for server in ntp_servers %}
ntp server {{ server }}
{% endfor %}
{% for vlan in vlans %}
vlan {{ vlan.id }}
name {{ vlan.name }}
{% endfor %}Filters
Filters modify variable output:
{{ hostname | upper }} {# ROUTER-01 #}
{{ hostname | lower }} {# router-01 #}
{{ description | default('') }} {# empty if undefined #}
{{ ip | ipaddr }} {# validate IP address #}
{{ items | join(', ') }} {# comma-separated list #}