Ansible Jinja indentation bug ?

Hi,

I have the bellow vars to populate a template , it works almost fine but the final file result you can see there is some indentation problem :

vars :

datadog_http_checks:

window : 5
timeout : 1

template:

init_config:

instances:
{% for http in datadog_http_checks %}

  • name: {{ http.name }}
    {% if http.url is defined -%}
    url: {{ http.url }}
    {% endif -%}
    {% if http.threshold is defined -%}
    threshold: {{ http.threshold }}
    {% endif %}
    {% if http.window is defined -%}
    window: {{ http.window }}
    {% endif %}
    {% if http.timeout is defined -%}
    timeout: {{ http.timeout }}
    {% endif %}
    {% if http.include_content is defined -%}
    include_content: {{ http.include_content }}
    {% endif %}
    notify:
  • pagerduty

{% endfor %}

result :

instances:

Is this a bug or am I doing something wrong ?

The problem from what I can remember is that ansible does not specify lstrip_blocks, and as such, it can cause indentation issues.

The approach I take, it to left justify all jinja2 control blocks. So all {% %} lines would have no indentation, only the lines inside would be indented.

such as:

{% for http in datadog_http_checks %}

  • name: {{ http.name }}
    {% if http.url is defined -%}
    url: {{ http.url }}
    {% endif -%}


{% endfor %}

Joined this group just to give you a shout out Matt, all became clear as soon as I read your reply.
Thanks man, you rock,
Ioan