🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
官网:https://docs.saltstack.com/en/latest/ref/returners/all/salt.returners.mysql.html minion返回数据到mysql数据库里 1)所有minion要安装python-mysql库 [admin@master ~ ]$ sudo salt '*' pkg.install name=MySQL-python node3.51yuki.cn: ---------- MySQL-python: ---------- new: 1.2.5-1.el7 old: node2.51yuki.cn: ---------- MySQL-python: ---------- new: 1.2.5-1.el7 old: 2)配置mysql MariaDB [(none)]> CREATE DATABASE `salt` -> DEFAULT CHARACTER SET utf8 -> DEFAULT COLLATE utf8_general_ci; MariaDB [(none)]> use salt Database changed MariaDB [salt]> DROP TABLE IF EXISTS `jids`; Query OK, 0 rows affected, 1 warning (0.00 sec) MariaDB [salt]> CREATE TABLE `jids` ( -> `jid` varchar(255) NOT NULL, -> `load` mediumtext NOT NULL, -> UNIQUE KEY `jid` (`jid`) -> ) ENGINE=InnoDB DEFAULT CHARSET=utf8; iumtext 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=InQuery OK, 0 rows affected (0.01 sec) MariaDB [salt]> CREATE INDEX jid ON jids(jid) USING BTREE; ERROR 1061 (42000): Duplicate key name 'jid' MariaDB [salt]> MariaDB [salt]> -- MariaDB [salt]> -- Table structure for table `salt_returns` MariaDB [salt]> -- MariaDB [salt]> MariaDB [salt]> DROP TABLE IF EXISTS `salt_returns`; noDB DEFAULT CHARSET=utf8;Query OK, 0 rows affected, 1 warning (0.00 sec) MariaDB [salt]> 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; Query OK, 0 rows affected (0.02 sec) MariaDB [salt]> MariaDB [salt]> -- MariaDB [salt]> -- Table structure for table `salt_events` MariaDB [salt]> -- MariaDB [salt]> MariaDB [salt]> DROP TABLE IF EXISTS `salt_events`; Query OK, 0 rows affected, 1 warning (0.00 sec) MariaDB [salt]> 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; Query OK, 0 rows affected (0.02 sec) MariaDB [salt]> show tables; +----------------+ | Tables_in_salt | +----------------+ | jids | | salt_events | | salt_returns | +----------------+ 3 rows in set (0.00 sec) 配置授权用户salt MariaDB [(none)]> grant all on salt.* to 'salt'@'10.2.11.%' identified by 'Aa123456'; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> flush privileges; Query OK, 0 rows affected (0.00 sec) 测试能不能连上 [admin@master ~ ]$ sudo mysql -h 10.2.11.227 -u salt -p 3)配置/etc/salt/minion mysql.host: '10.2.11.227' mysql.user: 'salt' mysql.pass: 'Aa123456' mysql.db: 'salt' mysql.port: 3306 4)重启minion [admin@node2 tmp]$ sudo systemctl restart salt-minion 二)查看 [admin@master ~ ]$ sudo salt '*' test.ping --return=mysql [admin@master ~ ]$ sudo salt '*' cmd.run 'df -h' --return=mysql (--return是返回到指定的位置) MariaDB [salt]> select * from salt_returns\G *************************** 1. row *************************** fun: test.ping jid: 20180120192448883501 return: true id: node3.51yuki.cn success: 1 full_ret: {"fun_args": [], "jid": "20180120192448883501", "return": true, "retcode": 0, "success": true, "fun": "test.ping", "id": "node3.51yuki.cn"} alter_time: 2018-01-20 19:24:49 *************************** 2. row *************************** fun: test.ping jid: 20180120192448883501 return: true id: node2.51yuki.cn success: 1 full_ret: {"fun_args": [], "jid": "20180120192448883501", "return": true, "retcode": 0, "success": true, "fun": "test.ping", "id": "node2.51yuki.cn"} alter_time: 2018-01-20 19:24:49 *************************** 3. row *************************** fun: cmd.run jid: 20180120192640811889 return: "Filesystem Size Used Avail Use% Mounted on\n/dev/mapper/cl-root 13G 1.6G 11G 13% /\ndevtmpfs 486M 0 486M 0% /dev\ntmpfs 497M 12K 497M 1% /dev/shm\ntmpfs 497M 31M 466M 7% /run\ntmpfs 497M 0 497M 0% /sys/fs/cgroup\n/dev/vda1 1014M 121M 894M 12% /boot\ntmpfs 100M 0 100M 0% /run/user/1000\ntmpfs 100M 0 100M 0% /run/user/0" id: node2.51yuki.cn success: 1 full_ret: {"fun_args": ["df -h"], "jid": "20180120192640811889", "return": "Filesystem Size Used Avail Use% Mounted on\n/dev/mapper/cl-root 13G 1.6G 11G 13% /\ndevtmpfs 486M 0 486M 0% /dev\ntmpfs 497M 12K 497M 1% /dev/shm\ntmpfs 497M 31M 466M 7% /run\ntmpfs 497M 0 497M 0% /sys/fs/cgroup\n/dev/vda1 1014M 121M 894M 12% /boot\ntmpfs 100M 0 100M 0% /run/user/1000\ntmpfs 100M 0 100M 0% /run/user/0", "retcode": 0, "success": true, "fun": "cmd.run", "id": "node2.51yuki.cn"} alter_time: 2018-01-20 19:26:40 *************************** 4. row *************************** fun: cmd.run jid: 20180120192640811889 return: "Filesystem Size Used Avail Use% Mounted on\n/dev/mapper/cl-root 13G 1.6G 11G 13% /\ndevtmpfs 486M 0 486M 0% /dev\ntmpfs 497M 12K 497M 1% /dev/shm\ntmpfs 497M 14M 483M 3% /run\ntmpfs 497M 0 497M 0% /sys/fs/cgroup\n/dev/vda1 1014M 121M 894M 12% /boot\ntmpfs 100M 0 100M 0% /run/user/0\ntmpfs 100M 0 100M 0% /run/user/1000" id: node3.51yuki.cn success: 1 full_ret: {"fun_args": ["df -h"], "jid": "20180120192640811889", "return": "Filesystem Size Used Avail Use% Mounted on\n/dev/mapper/cl-root 13G 1.6G 11G 13% /\ndevtmpfs 486M 0 486M 0% /dev\ntmpfs 497M 12K 497M 1% /dev/shm\ntmpfs 497M 14M 483M 3% /run\ntmpfs 497M 0 497M 0% /sys/fs/cgroup\n/dev/vda1 1014M 121M 894M 12% /boot\ntmpfs 100M 0 100M 0% /run/user/0\ntmpfs 100M 0 100M 0% /run/user/1000", "retcode": 0, "success": true, "fun": "cmd.run", "id": "node3.51yuki.cn"} alter_time: 2018-01-20 19:26:40 4 rows in set (0.00 sec)