Ansible Jinja indentation bug ?

655 views
Skip to first unread message

Nicolas G

unread,
Jun 29, 2015, 2:46:14 PM6/29/15
to ansible...@googlegroups.com
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:
  - name : mycheck
    threshold : 5
    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:
- name: mycheck
  threshold: 5
    window: 5
    timeout: 1
      notify:
    - pagerduty


Is this a bug or am I doing something wrong ?

Matt Martz

unread,
Jun 29, 2015, 2:55:48 PM6/29/15
to ansible...@googlegroups.com
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 %}

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/5e7c16ad-ce87-4db9-add6-a1d600f5b2bb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Matt Martz
@sivel
sivel.net

Ioan surname

unread,
Jul 15, 2016, 7:59:19 PM7/15/16
to Ansible Project
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
Reply all
Reply to author
Forward
0 new messages