【问题标题】:Saltstack or JInja2 - Merge yaml with dictionarySaltstack 或 JInja2 - 将 yaml 与字典合并
【发布时间】:2022-01-25 09:18:44
【问题描述】:

我正在尝试将字典与 yaml 文件内容合并并将它们传递给某种盐状态。

metricbeat.yml 内容:

metricbeat:
  config:
    modules:
      path: /etc/metricbeat/modules.d/*.yml
      reload.enabled: true
      reload.period: 10s

output.logstash:
  hosts:
  worker: 1
  compression_level: 3
  loadbalance: true
  ssl:
    certificate: /usr/share/metricbeat/metricbeat.crt
    key: /usr/share/metricbeat/metricbeat.key
    verification_mode: none

logging:
  level: debug
  to_files: true
  files:
    path: /var/tellme/log/metricbeat
    name: metricbeat.log
    rotateeverybytes: 10485760
    keepfiles: 7

config.yml 内容:

metricbeat:
  config:
    modules:
      reload.period: 100s

状态文件:

{% import_yaml "config.yml" as config %}

manage_file:
  file.managed:
    - name: /etc/metricbeat/metricbeat.yml
    - source: salt://metricbeat.yml
    - template: jinja

conf_file:
  file.serialize:
    - name: /etc/metricbeat/metricbeat.yml
    - dataset:
        output.logstash:
          hosts: ['exacmple.domain.com:5158']
        {{ config | yaml }}
    - serializer: yaml
    - merge_if_exists: true

但我收到以下错误:

example-1.domain.com:
    Data failed to compile:
----------
    Rendering SLS 'base:test' failed: could not find expected ':'

我做错了什么?

【问题讨论】:

    标签: yaml jinja2 salt-stack


    【解决方案1】:

    修复如下问题

    {% import_yaml "config.yml" as config %}
    
    manage_file:
      file.managed:
        - name: /etc/metricbeat/metricbeat.yml
        - source: salt://metricbeat.yml
        - template: jinja
    
    conf_file:
      file.serialize:
        - name: /etc/metricbeat/metricbeat.yml
        - dataset:
            output.logstash:
              hosts: ['exacmple.domain.com:5158:5158']
            {{ config | yaml(false) | indent(8) }}
        - serializer: yaml
        - merge_if_exists: true
    

    "yaml(false)" 用于多行 yaml 并使用 "indent" 进行适当的缩进。

    【讨论】:

      猜你喜欢
      • 2011-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-21
      • 2021-06-30
      • 2021-08-03
      相关资源
      最近更新 更多