在mysql中储存salt返回结果 参考文档 http://docs.saltstack.cn/topics/jobs/external_cache.html
master端向minion端推送之后,minion端会保存推送信息24小时,为了将信息长久保存,可以选择将数据存储到数据库中,从而长久保存.使数据存储到数据库中有两种实现方式
yum install -y mysql-server
grant all on salt.* to salt@'172.25.1.%' identified by 'redhat';
yum install MySQL-python -y
[root@server2 ~]# vim /etc/salt/minion
810 #return:
811 # - mysql
812 # - hipchat
813 # - slack
814 mysql.host: '172.25.1.1' # mysql数据库所在主机
815 mysql.user: 'salt' # 登陆数据库的用户
816 mysql.pass: 'redhat' # salt用户密码
817 mysql.db: 'salt' # 存放推送信息的数据库
818 mysql.port: 3306
mysql < test.sql
[root@server2 ~]# vim test.sql
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;
-- CREATE INDEX jid ON jids(jid) USING BTREE;
--
-- 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` mediumtext NOT NULL,
`alter_time` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
`master_id` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
KEY `tag` (`tag`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
yum install MySQL-python
grant all on salt.* to salt@'localhost' identified by 'redhat';
[root@server1 ~]# vim /etc/salt/master
1058 master_job_cache: mysql
1059 mysql.host: 'localhost'
1060 mysql.user: 'salt'
1061 mysql.pass: 'redhat'
1062 mysql.db: 'salt'
1063 mysql.port: 3306
[root@server1 ~]# salt server3 cmd.run 'df -h'
server3:
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root 14G 1.1G 12G 8% /
tmpfs 246M 16K 246M 1% /dev/shm
/dev/vda1 485M 33M 427M 8% /boot
mysql> use salt;
mysql> select * from salt_returns\G;
*************************** 5. row ***************************
fun: cmd.run
jid: 20181113200153946345
return: "Filesystem Size Used Avail Use% Mounted on\n/dev/mapper/VolGroup-lv_root 14G 1.1G 12G 8% /\ntmpfs 246M 16K 246M 1% /dev/shm\n/dev/vda1 485M 33M 427M 8% /boot"
id: server3
success: 1
full_ret: {"fun_args": ["df -h"], "jid": "20181113200153946345", "return": "Filesystem Size Used Avail Use% Mounted on\n/dev/mapper/VolGroup-lv_root 14G 1.1G 12G 8% /\ntmpfs 246M 16K 246M 1% /dev/shm\n/dev/vda1 485M 33M 427M 8% /boot", "retcode": 0, "success": true, "cmd": "_return", "_stamp": "2018-11-13T12:01:54.013976", "fun": "cmd.run", "id": "server3"}
alter_time: 2018-11-13 20:01:54