ansbile 模板格式奇怪

ansbile 模板格式奇怪

我正在尝试创建基于 yaml 的配置文件。除了循环后的行由于某种原因缩进外,一切正常。

所以当我有这个时......

- 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

时间戳字段缩进不正确..

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:

如果我在 for 循环后添加注释,该注释会缩进,并且时间戳值位于正确的位置。我尝试删除循环中的空格,但问题并未解决。我认为这很简单,但我被难住了。

答案1

没有理由不正确地缩进文件。模板

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

和剧本

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

给出

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

相关内容