-1

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.

Paul
  • 3,037
  • 6
  • 27
  • 40
flyerhawk
  • 27
  • 3
  • [edit] the question and make it [mre](https://stackoverflow.com/help/minimal-reproducible-example). https://idownvotedbecau.se/nomcve/. – Vladimir Botka Jan 01 '22 at 20:07
  • Not sure what else you are looking for. The code snippet I supplied SHOULD work but it DOESN'T work. – flyerhawk Jan 03 '22 at 19:06

1 Answers1

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
Vladimir Botka
  • 5,138
  • 8
  • 20
  • So I put my entire template on this test site. https://j2live.ttl255.com/ And it works fine on that website. Yet when I run it with ansible, it keeps adding additional whitespaces on that timestamp line. – flyerhawk Jan 03 '22 at 18:34