这个就是把saltstack的运行结果存入到mysql数据库中。
先决条件: master 和 minion上都要安装 yum install MySQL-python
1,saltmaster (192.168.1.212)上安装 mysql,
mysql -uroot -p
use mysql
GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.1.215' IDENTIFIED BY 'mysqlpw' WITH GRANT OPTION;
FLUSH PRIVILEGES;
// 允许root用户从minion (192.168.1.215)登陆,
//创建数据库
2, 在saltmaster(192.168.1.212)上: cat /srv/salt/_returners/testmysql.py
# -*- coding: utf-8 -*-
import sys
import json
import time
import MySQLdb
import salt.returners
try:
import MySQLdb
HAS_MYSQL = True
except ImportError:
HAS_MYSQL = False
__virtualname__ = 'testmysql'
def __virtual__():
if not HAS_MYSQL:
return False
else:
return __virtualname__
def returner(ret):
conn=MySQLdb.connect(host='192.168.1.212',user='root',passwd='mysqlpw',db='salt',
port=3306)
cursor=conn.cursor()
sql = '''INSERT INTO `salt_returns`(`fun`,`jid`,`return`,`id`,`success`,`full_ret`)
VALUES (%s,%s,%s,%s,%s,%s)'''
cursor.execute(sql % (str(json.dumps(ret['fun'])),str(json.dumps(ret['jid'])),
str(json.dumps(ret['return'])),str(json.dumps(ret['id'])),
'"'+str(ret['success'])+'"',"'"+json.dumps(ret)+"'"))
conn.commit()
cursor.close()
conn.close()
//注意 virtualname
# salt '*' saltutil.sync_returners
# salt 'centos5' cmd.run 'df -H' --return testmysql
mysql> select * from salt_returns\G
*************************** 1. row ***************************
fun: cmd.run
jid: 20140923164910093656
return: Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_centos2-lv_root
19G 2.6G 16G 15% /
tmpfs 981M 0 981M 0% /dev/shm
/dev/sda1 508M 79M 403M 17% /boot
id: centos5
success: True
full_ret: {"fun_args": ["df -H"], "jid": "20140923164910093656", "return": "Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_centos2-lv_root
19G 2.6G 16G 15% /
tmpfs 981M 0 981M 0% /dev/shm
/dev/sda1 508M 79M 403M 17% /boot", "retcode": 0, "success": true, "fun": "cmd.run", "id": "centos5"}
alter_time: 2014-09-23 16:49:10