Saltstack python client

获取master配置

import salt.config
master_opts = salt.config.client_config('/etc/salt/master')
print master_opts

实例化

import salt.client
local = salt.client.LocalClient()

通过grain过滤主机并且返回jid

ret = local.cmd(tgt='roles:salt-master', fun='test.version', tgt_type='grain', full_return=True)
print ret

批量执行

ret1 = local.cmd_batch(tgt="roles:salt-slave", fun="cmd.run", arg=(["ping -c 10 baidu.com"]), tgt_type='grain')
for i in ret1:
    print i

导出所有模块的doc

modules = local.cmd("n1", fun="sys.list_modules")
for module in modules["n1"]:
    local.cmd('n1', fun="file.touch", arg=(["/root/salt_"+module+"_doc.md"]))
    module_doc = local.cmd('n1', fun="sys.doc", arg=([module]))
    doc = module_doc["n1"]
    for i in doc:
        key = i.encode("raw_unicode_escape")
        local.cmd("n1", fun="file.append", arg=(["/root/salt_"+module+"_doc.md", key]))
        print key
        value = doc[key].encode("raw_unicode_escape")
        local.cmd("n1", fun="file.append", arg=(["/root/salt_"+module+"_doc.md", value]))
        print value
posted @ 2020-03-28 17:20  random_lee  阅读(287)  评论(0编辑  收藏  举报