Jinja2 Reference
Last updated:
JINJAANSIBLE
Jinja2 is used for templating.
String manipulation
Substitution
The name is {{ name }}
Upper case
The name is {{ name|upper }}
Title Case
{{ book_name|title }}
Replace
{{ dialogue | replace("Bourne","Bond") }}
Array
Highest number in an array
{{ numbers | max }}
Last number in an array
{{ numbers | last }}
Join
{{ words | join(' ') }}
Number of words
{{ words | wordcount }}
Loops
{% for name_server in name_servers -%}
nameserver {{ name_server }}
{% endfor %}
Input:
{
"hosts": [
{
"name": "web1",
"ip_address": "192.168.5.4"
},
{
"name": "web2",
"ip_address": "192.168.5.5"
},
{
"name": "web3",
"ip_address": "192.168.5.8"
},
{
"name": "db1",
"ip_address": "192.168.5.9"
}
]
}
{% for host in hosts -%}
{{ host.name }} {{ host.ip_address }}
{% endfor %}
IF
{% for host in hosts -%}
{% if "web" in host.name %}
{{ host.name }} {{ host.ip_address }}
{% endif %}
{% endfor %}
references: Jinja | The Pallets Projects Jinja — Jinja Documentation (3.1.x) (palletsprojects.com)