'Saltstack or JInja2 - Merge yaml with dictionary

I am trying to merge a dictionary with a yaml file content and pass them to some salt state.

metricbeat.yml content:

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 content:

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

Statefile:

{% 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

But I am getting the below error:

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

What am I doing wrong ?



Solution 1:[1]

Fixed the issue as below

{% 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)" is for multiline yaml and proper indentation with "indent".

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sujeet Padhi