2013-12-19 71 views

回答

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 

我專門做了(我需要執行一個命令,但是隻有當發佈命令成功執行。發佈哪個命令取決於僕從的角色):

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 /鹽/主等通信朋輩發佈設置':

peer: 
    .*: 
    - .* 

此配置並不安全,因爲它使所有的爪牙執行同胞爪牙的所有命令,但我還沒有想出根據他們的角色選擇正確的語法來選擇小兵。

另一個需要注意的是,創建一個包含cmd.run的自定義命令,然後只啓用它,因爲啓用所有節點相互之間執行任意腳本是不安全的。

這個答案的本質和Dan Garthwaite的一樣,但是我需要的是一個.sls文件的解決方案。

+0

謝謝。你曾問過如何在一個模塊中做到這一點,我也不知道該怎麼做。你如何定義你的角色? –

+0

目前在minion配置文件中 – Marmoy

4

你想要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"') 

您必須以鹽用戶身份(通常是root)運行上述操作。

然後轉到web001並確認消息在/ var/log/syslog中。爲我工作。

相關問題