1. 程式人生 > >saltstack的命令返回結果寫入mysql資料庫

saltstack的命令返回結果寫入mysql資料庫

第一種:

[root@server1 ~]# yum install -y mysql-server
[root@server1 ~]# /etc/init.d/mysqld start
[root@server1 ~]# salt server2 state.sls httpd.install
[root@server2 ~]# yum install -y MySQL-python.x86_64
[root@server2 ~]# vim /etc/salt/minion
814 mysql.host: '172.25.40.1'
815 mysql.user: 'salt'
816
mysql.pass: 'westos' 817 mysql.db: 'salt' 818 mysql.port: 3306 [root@server2 ~]# /etc/init.d/salt-minion restart

這裡寫圖片描述

[[email protected] ~]# mysql
mysql> grant all on salt.* to [email protected]'172.25.40.%' identified by 'westos';


[[email protected] ~]# 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; [[email protected] ~]# mysql < test.sql [[email protected] ~]# salt 'server2' test.ping --return mysql

這裡寫圖片描述

[root@server1 ~]# mysql
mysql> use salt
mysql> show tables;
mysql> select * from salt_returns;

這裡寫圖片描述

第二種:

[root@server1 ~]# yum install -y MySQL-python.x86_64
[root@server1 ~]# vim /etc/salt/master
[root@server1 ~]# /etc/init.d/salt-master restart

這裡寫圖片描述

[root@server1 ~]# mysql
mysql> grant all on salt.* to salt@localhost identified by 'westos';
[root@server1 ~]# salt server3 test.ping 
[root@server1 ~]# reoot
[root@foundation40 ~]# ssh [email protected]
[root@server1 ~]# /etc/init.d/mysqld start 
[root@server1 ~]# salt server3 test.ping  --return mysql
[root@server1 ~]# mysql
mysql> use salt
mysql> select * from salt_returns;

可以看到server3的返回值
這裡寫圖片描述


[root@server1 ~]# mkdir /srv/salt/_modules
[root@server1 ~]# cd /srv/salt/
[root@server1 salt]# cd _modules/
[root@server1 _modules]# vim my_disk.py
#!/usr/bin/env python

def df():
    return __salt__['cmd.run']('df -h')
[root@server1 _modules]# salt '*' saltutil.sync_modules

這裡寫圖片描述

[root@server1 _modules]# salt '*' my_disk.df 

這裡寫圖片描述

測試:檢視server4中有my_disk.py

[root@server4 ~]# cd /var/cache/
[root@server4 cache]# cd salt/
[root@server4 salt]# cd minion/
[root@server4 minion]# cd extmods/
[root@server4 extmods]# cd modules/
[root@server4 modules]# ls
[root@server4 modules]# ll

這裡寫圖片描述