如何在 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 %}
这是输出:
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
in order to recreate you requested dictionnary structure, then dump the whole thing as YAML with the filter 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']
我正在尝试为我们的 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 %}
这是输出:
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
in order to recreate you requested dictionnary structure, then dump the whole thing as YAML with the filter 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']