mysql中储存salt返回结果

在mysql中储存salt返回结果 参考文档 http://docs.saltstack.cn/topics/jobs/external_cache.html


master端向minion端推送之后,minion端会保存推送信息24小时,为了将信息长久保存,可以选择将数据存储到数据库中,从而长久保存.使数据存储到数据库中有两种实现方式


方法一:

mysql中储存salt返回结果_第1张图片

(1)安装mysql(server1)

yum install -y mysql-server

(2)登陆数据库并对salt用户授权

grant all on salt.* to salt@'172.25.1.%' identified by 'redhat';

(3)在要备份的minion端(sevrer2)安装MySQL-python

yum install MySQL-python -y 

(4)配置server2的minion文件

[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

(5)在server1端的mysql数据库中导入数据

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;

(6)在sever1端向server2推送,数据库的salt数据库salt_returns表中查看返回结果

方法二:

mysql中储存salt返回结果_第2张图片

(1)在server1端安装MySQL-python

yum install MySQL-python

(2)在数据库中对本地localhost用户授权(数据库mysql饥饿python模块均在server1端,所以只用对localhost用户授权就行)

grant all on salt.* to salt@'localhost' identified by 'redhat';

(3)编辑server1端的master文件

[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

(4)在server1上向server2或3或4上推送salt server3 cmd.run ‘df -h’

[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

你可能感兴趣的:(mysql中储存salt返回结果)