ansbile template weird formatting

I'm trying to create yaml based configuration files. Everything is working except that the lines after a loop is being indented for some reason.

So when I have this...

- job_name: {{ inventory_hostname }}
    pipeline_stages:
        - regex:
            expression: {{ pipeline_regex }}
        - labels:
{% for labels in pipeline_vars %}
{{ labels }}:
{% endfor %}
        - timestamp:
            source: date
            format: 2006-01-01 15:00:00.000000

the timestamp field is improperly indented..

scrape_configs:
    - job_name: test
      pipeline_stages:
        - regex:
            expression: Test
        - labels:
            Test:
            Test2:
            - timestamp:
            source: date
            format: 2006-01-01 15:00:00.000000
        - drop:

If I put a comment in after the for loop that gets indented and the timestamp value is in the right place. I tried to remove the whitespace in the loop and that didn't fix the problem. I assume this is something simple but I am stumped.


Solution 1:

There is no reason the file shouldn't be indented properly. The template

shell> cat scrape_configs.yml.j2
- job_name: {{ inventory_hostname }}
    pipeline_stages:
        - regex:
            expression: {{ pipeline_regex }}
        - labels:
{% for labels in pipeline_vars %}
{{ labels }}:
{% endfor %}
        - timestamp:
            source: date
            format: 2006-01-01 15:00:00.000000

and the playbook

- hosts: test
  gather_facts: false
  vars:
    pipeline_regex: Test
    pipeline_vars:
      - '            Test'
      - '            Test2'
  tasks:
    - template:
        src: scrape_configs.yml.j2
        dest: scrape_configs.yml

gives

shell> cat scrape_configs.yml
- job_name: test
    pipeline_stages:
        - regex:
            expression: Test
        - labels:
            Test:
            Test2:
        - timestamp:
            source: date
            format: 2006-01-01 15:00:00.000000