Saltstack | runners(七)

一、runners介绍

runners有点类似于modules,区别在于modules是给minions远程执行的,runners是在master上执行的,而且runners调用的是salt-run命令
使用方法也跟modules无太大区别,创建runners定义函数,调用salt-run执行,官方自带了几个runners,当然你也可以自己写

二、runners使用

例如使用官方自带的runners给大家演示下

# salt-run manage.status
down:
up:
- BJ-SQUIDAPP-50
- BJ-SQUIDAPP-52
...
# salt-run jobs.active
'20130815101458820238':
Arguments:
- ping www.baidu.com
Function: cmd.run
Returned: []
Running: []
Target: '*'
Target-type: glob
User: root

三、自定义runners

1、默认自定义runners是没有开启的,这个需要去master配置文件开启并重启服务

# vim /etc/salt/master
#runner_dirs: []
runner_dirs: ['/srv/salt/_runners']
# /etc/init.d/salt-master restart
Stopping salt-master daemon: [确定]
Starting salt-master daemon: [确定]

2、创建相关目录与文件

# mkdir /srv/salt/_runners && cd /srv/salt/_runners
# touch proxy.py

3、编写python脚本,例如写一个显示所有在线的minions

#!/usr/bin/python
# _*_ coding=utf-8 _*_
# Import salt modules
import salt.client
def up():
'''
Print a list of all of the minions that are up
'''
client = salt.client.LocalClient(__opts__['conf_file'])
minions = client.cmd('*', 'test.ping', timeout=1)
for minion in sorted(minions):
print minion

4、测试

# salt-run proxy.up
BJ-SQUIDAPP-50
BJ-SQUIDAPP-52
...

备注
你可以去salt的github上找一些比较好的例子 runners