如何使用 Salt cmd.script 执行多个脚本?

How to execute multiple scripts with Salt cmd.script?

我正在尝试应用由多个脚本组成的盐状态。但某些原因只有 script4 是 运行.

这是我的状态文件。

#!jinja|yaml|gpg

Distro_Specific_Scripts:
  cmd.script:
    - name: Run Scripts.sh
  {% if grains['os_family'] == 'RedHat' %}
    - source: salt://scripts/files/scripts/script_for_redhat.sh
  {% elif grains['os_family'] == 'Debian' %}
    - source: salt://scripts/files/scripts/script_for_ubuntu.sh
  {% endif %}
    - source: salt://scripts/files/scripts/script2.sh
    - source: salt://scripts/files/scripts/script3.sh
    - source: salt://scripts/files/scripts/script4.sh

您能分享一下关于 运行 多个脚本的经验吗?或者你能告诉我我错在哪里吗?

其实我找到了实现目标的方法。但是,如果有最聪明的方法来做到这一点,我们将不胜感激:)

#!jinja|yaml|gpg

Distro_Specific_Scripts:
  cmd.script:
    - name: Distro Specific Scripts
  {% if grains['os_family'] == 'RedHat' %}
    - source: salt://scripts/files/scripts/script_for_redhat.sh
  {% elif grains['os_family'] == 'Debian' %}
    - source: salt://scripts/files/scripts/script_for_debian.sh
  {% endif %}
script2:
  cmd.script:
    - name: script2.sh
    - source: salt://scripts/files/scripts/script2.sh
script3:
  cmd.script:
    - name: script3
    - source: salt://scripts/files/scripts/script2.sh
script4:
  cmd.script:
    - name: script4.sh
    - source: salt://scripts/files/scripts/script2.sh

有多种写法。

选项 1:

这将是最简单的方法。如果您有grains['os_family']对应的脚本,即

  • script_for_debian.sh 对于 Debian
  • script_for_ubuntu.sh 对于 Ubuntu
  • script_for_redhat.sh 对于 RedHat
run_my_scripts:
  cmd.script:
    - names:
        - salt://scripts/files/scripts/script_for_{{ grains['os_family'] | lower }}.sh
        - salt://scripts/files/scripts/script2.sh
        - salt://scripts/files/scripts/script3.sh
        - salt://scripts/files/scripts/script4.sh

选项 2:

如果没有,那么我们可以在状态声明之外有条件地设置脚本名称:

{% set os_specific_script = salt['grains.filter_by']({
  'Debian': 'script_for_ubuntu.sh',
  'RedHat': 'script_for_redhat.sh'
}) %}

run_my_scripts:
  cmd.script:
    - names:
        - salt://scripts/files/scripts/{{ os_specific_script }}
        - salt://scripts/files/scripts/script2.sh
        - salt://scripts/files/scripts/script3.sh
        - salt://scripts/files/scripts/script4.sh

如果您可以控制脚本的名称,OPTION 1 将是更好的选择。