Saltstack利用Returners程序保存执行结果到mysql
在我们执行saltstack的时候,minion端会返回一大堆的执行结果显示在master端,那如何将
每一次slat执行的结果这些结果保存起来便于日后查询,这里就用到了saltstack的返回程序
Returners,可以保存在Redis,Mongodb,MySQL等这些程序当中;
注意这的返回并不是将执行结果返回给master,master再写入到MySQL或者Redis中,而是salt-minion端直接向MySQL或者Redis中写,
下面是操作步骤:
1.安装软件包
master端:yum install -y MySQL-python mysql mysql-server
minion端:yum install -y MySQL-python
2.建立数据库,创建salt所需要的数据库及表结构
CREATE DATABASE `salt`
DEFAULT CHARACTER SET utf8
DEFAULT COLLATE utf8_general_ci;
USE `salt`;
--
-- Table structure for table `jids`
--
DROP TABLE IF EXISTS `jids`;
CREATE TABLE `jids` (
`jid` varchar(255) NOT NULL,
`load` mediumtext NOT NULL,
UNIQUE KEY `jid` (`jid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Table structure for table `salt_returns`
--
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,
`alter_time` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
KEY `id` (`id`),
KEY `jid` (`jid`),
KEY `fun` (`fun`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Table structure for table `salt_events`
--
DROP TABLE IF EXISTS `salt_events`;
CREATE TABLE `salt_events` (
`id` BIGINT NOT NULL AUTO_INCREMENT,
`tag` varchar(255) NOT NULL,
`data` varchar(1024) NOT NULL,
`alter_time` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `tag` (`tag`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
#检查一下是否创建成功:
mysql> use salt
Database changed
mysql> show tables;
+----------------+
| Tables_in_salt |
+----------------+
| jids |
| salt_events |
| salt_returns |
+----------------+
rows in set (0.00 sec)
3.授权数据库,修改master配置
mysql > grant all on salt.* to salt@'192.168.2.0/255.255.255.0' identified by 'salt';
mysql > flush privileges;
在master端或者每个minion端都写如以下配置内容。当然如果写在master端是比较简单的做法,因为只需要写一次就行啦。我这里写在了master端。
[root@zabbix-test ~]# vim /etc/salt/master
master_job_cache: mysql #每次执行不加--return mysql由master端将返回的数据写入数据库,不需要minion。
mysql.host: '192.168.2.21' #mysql 数据库服务器地址
mysql.user: 'salt'
mysql.pass: 'salt'
mysql.db: 'salt'
mysql.port: 3306
4.执行语句测试:
[root@zabbix-test ~]# salt \* test.ping
zabbix-test:
True
MariaDB [salt]> select * from salt_returns\G;
*************************** 1. row ***************************
fun: test.ping
jid: 20170322111813690543
return: true
id: zabbix-test
success: 1
full_ret: {"fun_args": [], "jid": "20170322111813690543", "return": true, "retcode": 0, "success": true, "fun": "test.ping", "id": "zabbix-test"}
alter_time: 2017-03-22 11:18:13
1 row in set (0.00 sec)
参考文章:http://www.mamicode.com/info-detail-1634255.html
https://blog.csdn.net/reblue520/article/details/52151361