【问题标题】:How do you use the 'publish' module in your own module您如何在自己的模块中使用“发布”模块
【发布时间】:2013-12-19 09:46:36
【问题描述】:

我需要在另一个 minion 上执行一个脚本。最好的解决方案似乎是 Peer Publishing,但 the only documentation 我只能找到显示如何通过 CLI 进行操作。

如何在模块中定义以下内容?

salt-call system.example.com publish.publish '*' cmd.run './script_to_run'

【问题讨论】:

    标签: salt-stack


    【解决方案1】:

    您需要 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 中。为我工作。

    【讨论】:

      【解决方案2】:

      .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
      

      我具体做了什么(我需要执行一个命令,但前提是发布的命令执行成功。发布哪个命令取决于minion的角色):

      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 %}
      

      记得在“Peer Publish Settings”部分下的 /etc/salt/master 中启用对等通信:

      peer:
        .*:
          - .*
      

      这种配置并不安全,因为它允许所有的 minions 对其他 minions 执行所有命令,但我还没有弄清楚根据角色选择 minions 的正确语法。

      另一个注意事项是,最好创建一个包含 cmd.run 的自定义命令,然后只启用它,因为让所有节点相互执行任意脚本并不安全。

      这个答案的本质与 Dan Garthwaite 的相同,但我需要的是 .sls 文件的解决方案。

      【讨论】:

      • 谢谢。您曾问过如何在模块中执行此操作,我也不知道该怎么做。你是如何定义你的角色的?
      • 目前在minion配置文件中
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-11
      • 2017-07-21
      • 1970-01-01
      • 2018-04-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多