防止 to_nice_yaml 生成别名

prevent to_nice_yaml from generating aliases

是否可以强制函数 to_nice_yaml 避免生成别名?

Ansible 模板中的以下行

scrape_configs:
    {{ scrape_configs | to_nice_yaml(indent=2) | indent(2,False) }}

其中

common_relabeling:
  - stuff1
  - stuff2

scrape_configs:
  - job_name: process_exporter
    relabel_configs: "{{ common_relabeling }}"
  - job_name: node_exporter
    relabel_configs: "{{ common_relabeling }}"

使用别名在 YAML 文件中扩展(见下文),我不确定 Prometheus 的配置解析器是否支持它。显然我想在每个条目

中不硬编码 common_relabeling 来修复它
scrape_configs:
  - job_name: process_exporter
    relabel_configs: &id001
    - stuff1
    - stuff2
  - job_name: node_exporter
    relabel_configs: *id001

您可以保留锚点和别名不变。

Prometheus 使用包 gopkg.in/yaml.v2, and if you read through the documentation of that package, you'll see that it is based on libyaml, which has been parsing anchors and aliases for over a decade now. And the documentation for gopkg.in/yaml.v2 explicitly states that anchors are supported:

The yaml package supports most of YAML 1.1 and 1.2, including support for anchors, tags ...