繁体   English   中英

如何将嵌套字典保存为 jinja2 ansible 中的变量?

[英]How can I save a nested dictionary as variable in jinja2 ansible?

我正在尝试为我们的 ansible 角色创建一个 prometheus.yml.j2 模板。 这是变量:

SCRAPE_CONFIGS:
  - job_name: 'prometheus'
    static_configs:
      - targets: ['localhost:9090']

  - job_name: 'postgresql'
    static_configs:
      - targets: ['postgresql-exporter:9187']

我试过了:

scrape_config:
  {% for scrape in SCRAPE_CONFIGS -%}
    {{ scrape }}
  {% endfor %}

这是 output:

scrape_config:
  {'job_name': 'prometheus', 'static_configs': [{'targets': ['localhost:9090']}]}
  {'job_name': 'postgresql', 'static_configs': [{'targets': ['postgresql-exporter:9187']}]}

但它应该看起来像变量本身:

  scrape_config:
    - job_name: 'prometheus'
      static_configs:
        - targets: ['localhost:9090']

    - job_name: 'postgresql'
      static_configs:
        - targets: ['postgresql-exporter:9187']

否则 prometheus 容器会抛出语法错误,因为它无法正确读取 prometheus.yml 配置文件。 有没有人有更好的建议如何遍历这个嵌套字典? 结构应该保持不变。 还应该可以使用更多条目添加不同的 scrape_configs,例如:

- job_name: 'elasticsearch'
  scrape_intervall: 10s
  scrape_timeout: 5s
  static_configs:
    - targets: ['elasticsearch-exporter:9114']

您为什么不直接使用set_fact来重新创建您请求的字典结构,然后使用过滤器 to_yaml 将整个内容转储为to_yaml

鉴于剧本:

- hosts: all
  gather_facts: no

  tasks:
    - set_fact:
        config: 
          scrape_config: "{{ SCRAPE_CONFIGS }}"
      vars:
        SCRAPE_CONFIGS:
          - job_name: 'prometheus'
            static_configs:
              - targets: ['localhost:9090']

          - job_name: 'postgresql'
            static_configs:
              - targets: ['postgresql-exporter:9187']

    - copy:
        content: "{{ config | to_yaml }}"
        dest: prometheus.yml.j2

这将创建一个包含以下内容的文件prometheus.yml.j2

scrape_config:
- job_name: prometheus
  static_configs:
  - targets: ['localhost:9090']
- job_name: postgresql
  static_configs:
  - targets: ['postgresql-exporter:9187']

并添加一个额外的元素,剧本

- hosts: all
  gather_facts: no

  tasks:
    - set_fact:
        config: 
          scrape_config: "{{ SCRAPE_CONFIGS + elements_to_add }}"
      vars:
        SCRAPE_CONFIGS:
          - job_name: 'prometheus'
            static_configs:
              - targets: ['localhost:9090']

          - job_name: 'postgresql'
            static_configs:
              - targets: ['postgresql-exporter:9187']

        elements_to_add:
          - job_name: 'elasticsearch'
            scrape_intervall: 10s
            scrape_timeout: 5s
            static_configs:
              - targets: ['elasticsearch-exporter:9114']

    - copy:
        content: "{{ config | to_yaml }}"
        dest: prometheus.yml.j2

将创建一个包含以下内容的文件prometheus.yml.j2

scrape_config:
- job_name: prometheus
  static_configs:
  - targets: ['localhost:9090']
- job_name: postgresql
  static_configs:
  - targets: ['postgresql-exporter:9187']
- job_name: elasticsearch
  scrape_intervall: 10s
  scrape_timeout: 5s
  static_configs:
  - targets: ['elasticsearch-exporter:9114']

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM