sivann
sivann

Reputation: 2131

SaltStack: edit yaml file on minion host based on salt pillar data

Say the minion host has a default yaml configuration named myconf.yaml. What I want to do is to edit parts of those yaml entries using values from a pillar. I can't even begin to think how to do this on Salt. The only think I can think of is to run a custom python script on the host via cmd.run and feed it with input via arguments, but this seems overcomplicated.

I want to avoid file.managed. I cannot use a template, since the .yaml file is big, and can change by external means. I just want to edit a few parameters in it. I suppose a python script could do it but I thought salt could do it without writing s/w

I have found salt.states.file.serialize with the merge_if_exists option, I will try this and report.

Upvotes: 1

Views: 2593

Answers (2)

Andrew
Andrew

Reputation: 4418

You want file.serialize with the merge_if_exists option.

# states/my_app.sls
something_conf_file:
  file.serialize:
    - name:            /etc/my_app.yaml
    - dataset_pillar:  my_app:mergeconf
    - formatter:       yaml
    - merge_if_exists: true

# pillar/my_app.sls
my_app:
  mergeconf:
    options:
      opt3: 100
      opt4: 200

On the target, /etc/my_app.yaml might start out looking like this (before the state is applied):

# /etc/my_app.yaml
creds:
  user: a
  pass: b
options:
  opt1: 1
  opt2: 2
  opt3: 3
  opt4: 4

And would look like this after the state is applied:

creds:
  user: a
  pass: b
options:
  opt1: 1
  opt2: 2
  opt3: 100
  opt4: 200

As far as I can tell this uses the same algorithm as pillar merges, so e.g. you can merge or partially overwrite dictionaries, but not lists; lists can only be replaced whole.

Upvotes: 3

sivann
sivann

Reputation: 2131

This can be done for both json and yaml with file.serialize. Input can be inline on the state or come from a pillar. A short excerpt follows:

state:

cassandra_yaml:
  file:
    - serialize
#    - dataset:
#        concurrent_reads: 8
    - dataset_pillar: cassandra_yaml
    - name: /etc/cassandra/conf/cassandra.yaml
    - formatter: yaml
    - merge_if_exists: True
    - require:
      - pkg: cassandra-pkgs

pillar:

cassandra_yaml:
  concurrent_reads: "8"

Upvotes: 1

Related Questions