Score:-1

ansbile template weird formatting

dz flag

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.

br flag
[edit] the question and make it [mre](https://stackoverflow.com/help/minimal-reproducible-example). https://idownvotedbecau.se/nomcve/.
flyerhawk avatar
dz flag
Not sure what else you are looking for. The code snippet I supplied SHOULD work but it DOESN'T work.
Score:1
br flag

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
flyerhawk avatar
dz flag
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.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.