saltstack将数据返回到mysql服务器:

1、在master上安装mysql-server

[root@master ~]# yum -y install mysql-server
[root@master ~]# service mysqld start && chkconfig mysqld on

2、创建相关的表结构

[root@master ~]# mysql
mysql>
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;
说明:Required python modules: MySQLdb

3、mysql授权

mysql>  grant all on salt.* to salt@'%' identified by 'salt';

4、客户端安装MySQL-python

[root@master ~]# salt '*' cmd.run 'yum -y install MySQL-python'
[root@node01 ~]# mysql -u satl -p -h 10.10.10.140
Enter password: 
ERROR 1045 (28000): Access denied for user 'satl'@'node01' (using password: YES)

5、客户端进行连接测试

[root@node01 ~]# mysql -usalt -psalt -h 10.10.10.140
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.1.73 Source distribution
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| salt               |
| test               |
+--------------------+
3 rows in set (0.01 sec)
mysql> use salt;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+----------------+
| Tables_in_salt |
+----------------+
| jids           |
| salt_events    |
| salt_returns   |
+----------------+
3 rows in set (0.00 sec)

6、配置master及minion端相关配置

[root@master ~]# vim /etc/salt/master
mysql.host: '10.10.10.140'
mysql.user: 'salt'
mysql.pass: 'salt'
mysql.db: 'salt'
mysql.port: 3306
[root@master ~]# /etc/init.d/salt-master restart
Stopping salt-master daemon:                               [确定]
Starting salt-master daemon:                               [确定]
[root@node01 ~]# vim /etc/salt/minion
mysql.host: '10.10.10.140'
mysql.user: 'salt'
mysql.pass: 'salt'
mysql.db: 'salt'
mysql.port: 3306
[root@node01 ~]# /etc/init.d/salt-minion restart
Stopping salt-minion daemon:                               [确定]
Starting salt-minion daemon:                               [确定]

7、测试返回值到mysql的情况

[root@master ~]# salt '*' cmd.run 'df' --return mysql
node02.saltstack.com:
    Filesystem     1K-blocks    Used Available Use% Mounted on
    /dev/sda5       14114736 8728708   4662368  66% /
    tmpfs             953964      12    953952   1% /dev/shm
    /dev/sda1         194241   42160    141841  23% /boot
    /dev/sda3        1998672   18204   1875612   1% /tmp
node01.saltstack.com:
    Filesystem     1K-blocks    Used Available Use% Mounted on
    /dev/sda5       14114736 8729432   4661644  66% /
    tmpfs             953964      84    953880   1% /dev/shm
    /dev/sda1         194241   42160    141841  23% /boot
    /dev/sda3        1998672   18224   1875592   1% /tmp

测试结果:

mysql> select * from salt_returns\G;
*************************** 1. row ***************************
       fun: cmd.run
       jid: 20170220120522696829
    return: "Filesystem     1K-blocks    Used Available Use% Mounted on\n/dev/sda5       14114736 8729432   4661644  66% /\ntmpfs             953964      84    953880   1% /dev/shm\n/dev/sda1         194241   42160    141841  23% /boot\n/dev/sda3        1998672   18224   1875592   1% /tmp"
        id: node01.saltstack.com
   success: 1
  full_ret: {"fun_args": ["df"], "jid": "20170220120522696829", "return": "Filesystem     1K-blocks    Used Available Use% Mounted on\n/dev/sda5       14114736 8729432   4661644  66% /\ntmpfs             953964      84    953880   1% /dev/shm\n/dev/sda1         194241   42160    141841  23% /boot\n/dev/sda3        1998672   18224   1875592   1% /tmp", "retcode": 0, "success": true, "fun": "cmd.run", "id": "node01.saltstack.com"}
alter_time: 2017-02-20 12:05:24
*************************** 2. row ***************************
       fun: cmd.run
       jid: 20170220120528485243
    return: "Filesystem     1K-blocks    Used Available Use% Mounted on\n/dev/sda5       14114736 8729432   4661644  66% /\ntmpfs             953964      84    953880   1% /dev/shm\n/dev/sda1         194241   42160    141841  23% /boot\n/dev/sda3        1998672   18224   1875592   1% /tmp"
        id: node01.saltstack.com
   success: 1
  full_ret: {"fun_args": ["df"], "jid": "20170220120528485243", "return": "Filesystem     1K-blocks    Used Available Use% Mounted on\n/dev/sda5       14114736 8729432   4661644  66% /\ntmpfs             953964      84    953880   1% /dev/shm\n/dev/sda1         194241   42160    141841  23% /boot\n/dev/sda3        1998672   18224   1875592   1% /tmp", "retcode": 0, "success": true, "fun": "cmd.run", "id": "node01.saltstack.com"}
alter_time: 2017-02-20 12:05:28
2 rows in set (0.00 sec)
ERROR: 
No query specified

8、如果每次执行不加--return=mysql,由master端将返回的数据写入数据库

master_job_cache: mysql
注意:如果配置了/etc/salt/roster的话,
要注释掉master_job_cache: mysql,否则会有冲突
#master_job_cache: mysql

如果之前已经配置过salt-ssh的话,在设置master_job_cache: mysql的话,就会出现报错信息:

[root@master salt]# salt 'node01.saltstack.com' cmd.run 'ls -ld /' 
Failed to allocate a jid. The requested returner 'mysql' could not be loaded.
我暂时没找到解决办法

其它报错信息:

[root@master salt]# salt 'node01.saltstack.com' cmd.run 'ls -ld /' 
Salt request timed out. The master is not responding. If this error persists after verifying the master is up, worker_threads may need to be increased.
可以尝试在/etc/salt/master中修改worker_threads的值
#worker_threads: 5
worker_threads: 20
如果仍旧报错,可能是网络的问题,重新执行几次,可能就恢复了

参考资料:http://docs.saltstack.cn/ref/returners/all/salt.returners.mysql.html#module-salt.returners.mysql