我需要在另一个奴才上执行脚本。最好的解决方案似乎是 Peer Publishing,但我能找到的唯一文档只显示了如何通过 CLI 进行操作。
如何在模块中定义以下内容?
salt-call system.example.com publish.publish '*' cmd.run './script_to_run'
我需要在另一个奴才上执行脚本。最好的解决方案似乎是 Peer Publishing,但我能找到的唯一文档只显示了如何通过 CLI 进行操作。
如何在模块中定义以下内容?
salt-call system.example.com publish.publish '*' cmd.run './script_to_run'
你想要 salt.client.Caller() API。
#!/usr/bin/env python
import salt.client
salt_call = salt.client.Caller()
salt_call.function('publish.publish', 'web001',
'cmd.run', 'logger "publish.publish success"')
您必须以 salt 用户(通常是 root)的身份运行上述程序。
然后转到 web001 并确认消息在 /var/log/syslog 中。为我工作。
.sls 文件的语法:
salt-call publish.publish \* cmd.run 'cd /*directory* && ./script_to_run.sh:
cmd.run
替代语法:
execute script on other minion:
cmd.run
- name: salt-call publish.publish \* cmd.run 'cd /*directory* && ./script_to_run.sh
我具体做了什么(我需要执行一个命令,但前提是发布的命令执行成功。发布哪个命令取决于奴才的角色):
execute script:
cmd.run:
- name: *some shell command here*
- cwd: /*directory*
- require:
- file: *some file here*
{% if 'role_1' in grains['roles'] -%}
- onlyif: salt-call publish.publish \* cmd.run 'cd /*other_directory* && ./script_to_run_A.sh'
{% elif 'role_2' in grains['roles'] -%}
- onlyif: salt-call publish.publish \* cmd.run 'cd /*other_directory* && ./script_to_run_B.sh'
{% endif %}
请记住在“对等发布设置”部分下的 /etc/salt/master 中启用对等通信:
peer:
.*:
- .*
这种配置并不安全,因为它使所有的 minions 都可以在其他 minions 上执行所有命令,但我还没有弄清楚根据角色选择 minions 的正确语法。
另一个注意事项是,最好创建一个包含 cmd.run 的自定义命令,然后只启用它,因为让所有节点相互执行任意脚本并不安全。
这个答案的本质与 Dan Garthwaite 的相同,但我需要的是 .sls 文件的解决方案。