SaltStack:根据 salt pillar 数据在 minion 主机上编辑 yaml 文件

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

假设 minion 主机有一个名为 myconf.yaml 的默认 yaml 配置。我想要做的是使用支柱中的值编辑这些 yaml 条目的一部分。我什至无法开始思考如何在 Salt 上执行此操作。我唯一能想到的是 运行 通过 cmd.run 在主机上自定义 python 脚本并通过参数输入它,但这似乎过于复杂。

我想避开file.managed。我不能使用模板,因为 .yaml 文件很大,可以通过外部方式更改。我只想在其中编辑一些参数。我想 python 脚本可以做到,但我认为盐可以不写 s/w

我发现 salt.states.file.serialize 带有 merge_if_exists 选项,我会试试这个并报告。

json 和使用 file.serialize 的 yaml 都可以这样做。输入可以在状态上内联或来自支柱。简短摘录如下:

状态:

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

支柱:

cassandra_yaml:
  concurrent_reads: "8"

您希望 file.serialize 带有 merge_if_exists 选项。

# 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

在目标上,/etc/my_app.yaml 可能开始看起来像这样(在应用状态之前):

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

应用状态后看起来像这样:

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

据我所知,它使用与支柱合并相同的算法,例如您可以合并或部分覆盖字典,但不能合并列表;列表只能全部替换。