runners
saltstack return 除了在配置文件中可以定义外(太繁琐),还可以自定义retunner,当然,这需要通过代码实现了,实现方式和自定义的pillar和grains类似,步骤如下:
#创建_returners目录,并创建脚本 [root@linux-node1 _returners]# pwd /srv/salt/base/_returners [root@linux-node1 _returners]# cat mysql_return.py #这里脚本的名称需要和__virual__函数返回的一致 #!/usr/bin/env python # _*_ coding:utf-8 _*_ __author__ = 'Charles Chang' import json import MySQLdb #模块需要在master和minion端都安装 def __virtual__(): return 'mysql_return' def returner(ret): conn = MySQLdb.connect(host='192.168.74.20',user='salt',passwd='salt',db='salt',port=3306) cursor = conn.cursor() cursor.execute('INSERT INTO salt_returns VALUES (%s,%s,%s,%s,%s,%s)'%(json.dumps(ret['fun']),json.dumps(ret['jid']), json.dumps(ret['return']),json.dumps(ret['id']),'"'+str(ret['success'])+'"',"'"+json.dumps(ret)+"'")) conn.commit() cursor.close() conn.close() #创建mysql库和表 CREATE DATABASE `salt` DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci; GRANT ALL PRIVILEGES ON salt.* TO 'salt'@localhost IDENTIFIED BY 'salt'; GRANT ALL PRIVILEGES ON salt.* TO 'salt'@'%' IDENTIFIED BY 'salt'; DROP TABLE IF EXISTS `salt_returns`; CREATE TABLE `salt_returns` ( `fun` varchar(50) NOT NULL, `jid` varchar(255) NOT NULL, `return` mediumtext NOT NULL, `id` varchar(255) NOT NULL, `success` varchar(10) NOT NULL, `full_ret` mediumtext NOT NULL, KEY `id` (`id`), KEY `jid` (`jid`), KEY `fun` (`fun`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; #同步returnner到客户端 [root@linux-node1 _returners]# salt '*' saltutil.sync_returners linux-node1.oldboyedu.com: - returners.mysql_return linux-node2-computer: - returners.mysql_return #执行 [root@linux-node1 ~]# salt \* cmd.run 'whoami' --return mysql_return linux-node2-computer: root linux-node1.oldboyedu.com: root #数据:每一个minion占据一行数据 select * from salt_returns\G *************************** 6. row *************************** fun: cmd.run jid: 20170608165722516408 return: root id: linux-node2-computer success: True full_ret: {"fun_args": ["whoami"], "jid": "20170608165722516408", "return": "root", "retcode": 0, "success": true, "fun": "cmd.run", "id": "linux-node2-computer"} *************************** 7. row *************************** fun: cmd.run jid: 20170608165722516408 return: root id: linux-node1.oldboyedu.com success: True full_ret: {"fun_args": ["whoami"], "jid": "20170608165722516408", "return": "root", "retcode": 0, "success": true, "fun": "cmd.run", "id": "linux-node1.oldboyedu.com"}
查看有哪些returner
[root@linux-node1 ~]# salt '*' sys.list_returners linux-node2-computer: - carbon - couchdb - etcd - hipchat - local - local_cache - memcache - multi_returner - mysql - mysql_return - slack - smtp - sqlite3 - syslog linux-node1.oldboyedu.com: - carbon - couchdb - django - etcd - hipchat - local - local_cache - mongo - mongo_return - multi_returner - mysql - mysql_return - pushover - rawfile_json - redis - slack - smtp - splunk - sqlite3 - syslog - zabbix
returner的源码在 salt项目下的 salt/returners.py