Ansible template syntax

Arithmetic operations

bootstrap_expect = {{ (group_names|count  / 2)|int }}

For loop

{% for item in items %}
  Item index {{ loop.index }} is {{ item.name }}.
{% set item_count = 0 %}
{% for subItem in item.items if subItem > 0 %}
{% if not loop.last %}, {% endif %}
{% endfor %}

And/Or in template

{% if (bool_var or other_bool_var) and (true) %}
%{ endif %}

Array of IP addresses in group

[{% for group_hostname in groups["group_name"] %}  "{{ hostvars[group_hostname]["ansible_default_ipv4"]["address"] }}" {% if not loop.last %} , {% endif %}  {% endfor %}]

Template Filters

https://tedboy.github.io/jinja2/templ14.html

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *