每个 Minion 的 SaltStack file.recurse

SaltStack file.recurse per minion

目标

如何在 minion 上使用 file.recurse 安装特定于 minions 的文件?

当前策略

这可行:

   files_per_minion:
      file.recurse:
        - source: salt://monitoring/files/per_minion/{{ grains.id }}
        - name: /
        - template: jinja

...但是对于主服务器上没有此目录的小兵,它会失败。我不想为我的 master 上的每个 minion 创建一个目录。

我正在寻找一种可选的包含方法。这是伪代码中的一个条件:

 {% if magic_way_to_check_if_source_exists salt://monitoring/files/per_minion/{{ grains.id }} %}

   files_per_minion:
      file.recurse:
        - source: salt://monitoring/files/per_minion/{{ grains.id }}
        - name: /
        - template: jinja

 {% endif %}

问题

条件怎么写 magic_way_to_check_if_source_exists ?

欢迎其他解决方案

条件magic_way_to_check_if_source_exists只是实现目标的一种策略。欢迎其他解决方案。

用例

假设我想要安装 cron_tab_file_for_xhost,但只安装在名为 xhost 的主机上。我可以通过为这个主机创建一个目录树和一个文件来解决这个问题:

monitoring/files/per_minion/xhost/etc/cron.d/cron_tab_file_for_xhost

根据您的用例和状态树,有不同的方法。

最简单的方法是创建一个单独的状态并使用 top.sls 附加它。

如果您处理通过支柱配置的公式。我会把这些信息写在我的支柱的某个地方。然后各州根据支柱数据做出决定。

pillar.example:

yourformula:
  getspecificfile: true

somestate.sls:

{% if salt['pillar.get']("yourformula:getspecificfile") %}
files_per_minion:
  file.recurse:
    - source: salt://monitoring/files/per_minion/{{ grains.id }}
    - name: /
    - template: jinja
{% endif %}

更新:

我刚刚在 docs of the file.managed state

中看了一眼

The source parameter can be specified as a list. If this is done, then the first file to be matched will be the one that is used. This allows you to have a default file on which to fall back if the desired file does not exist on the salt fileserver. Here's an example:

/etc/foo.conf:
  file.managed:
    - source:
      - salt://foo.conf.{{ grains['fqdn'] }}
      - salt://foo.conf.fallback
    - user: foo
    - group: users
    - mode: 644
    - backup: minion

这似乎是另一种选择 - 如果您不想推出您的 minions 不需要的空文件。