1: minion端返回值存入mysql
(1)minion端的配置。只能对已经配置过的minion端
1:master安装数据库
[root@srever4 ~]# yum insatll -y mariadb-server
2:开启数据库
[root@srever4 ~]# systemctl start mariadb
[root@srever4 ~]# systemctl enable mariadb
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service
3:设置数据库相关信息
[root@server7 ~]# vim /etc/salt/minion
return: mysql
mysql.host: '172.25.60.4'
mysql.user: 'salt'
mysql.pass: 'salt'
mysql.db: 'salt'
mysql.port: 3306
4:修改了配置文件,重新开启服务
[root@server7 ~]# systemctl restart salt-minion
5:创建test.sql文件,然后导入数据库中,自动创建所需要的salt库
[root@server4 ~]# 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;
--
-- 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;
[root@server4 ~]# mysql < tset.sql ###导入到数据库中
6:给权限
[root@server4 ~]# mysql
MariaDB [(none)]> grant all on salt.* to salt@'%' identified by 'salt';
Query OK, 0 rows affected (0.01 sec)
7:进入数据库查看库 表
[root@server4 ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 5.5.52-MariaDB MariaDB Server
Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> 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
MariaDB [salt]> show tables;
+----------------+
| Tables_in_salt |
+----------------+
| jids |
| salt_events |
| salt_returns |
+----------------+
3 rows in set (0.00 sec)
8:minon端安装MySQL-python
[root@server7 ~]# yum install MySQL-python -y
9:master端执行指令
[root@server4 ~]# salt server7 cmd.run hostname --return mysql
server7:
server7
10:查看值是否返回到数据库中
[root@server4 ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 5.5.52-MariaDB MariaDB Server
Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> 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
MariaDB [salt]> show tables;
+----------------+
| Tables_in_salt |
+----------------+
| jids |
| salt_events |
| salt_returns |
+----------------+
3 rows in set (0.00 sec)
MariaDB [salt]> select * from salt_returns;
+---------+----------------------+-----------+---------+---------+--------------------------------------------------------------------------------------------------------------------------------------------------+---------------------+
| fun | jid | return | id | success | full_ret | alter_time |
+---------+----------------------+-----------+---------+---------+--------------------------------------------------------------------------------------------------------------------------------------------------+---------------------+
| cmd.run | 20190403092404685744 | "server7" | server7 | 1 | {"fun_args": ["hostname"], "jid": "20190403092404685744", "return": "server7", "retcode": 0, "success": true, "fun": "cmd.run", "id": "server7"} | 2019-04-03 09:24:04 |
+---------+----------------------+-----------+---------+---------+--------------------------------------------------------------------------------------------------------------------------------------------------+---------------------+
1 row in set (0.00 sec)
(2):master端进行操作,对所以的master都可以使用
1:取出minion端之前的设置
[root@server7 ~]# vim /etc/salt/minion
删除:
return: mysql
mysql.host: '172.25.60.4'
mysql.user: 'salt'
mysql.pass: 'salt'
mysql.db: 'salt'
mysql.port: 3306
2:重新启动服务
[root@server7 ~]# systemctl restart salt-minion
3:设置到master端
[root@server4 ~]# vim /etc/salt/master
mysql.host: 'localhost'
mysql.user: 'salt'
mysql.pass: 'salt'
mysql.db: 'salt'
mysql.port: 3306
master_job_cache: mysql ##这个是minion端没有的
4:重新启动master端
[root@server4 ~]# systemctl restart salt-master
5:登陆数据库修改权限
[root@server4 ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 12
Server version: 5.5.52-MariaDB MariaDB Server
Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> grant all on salt.* to salt@'localhost' identified by 'salt'; ##设置成localhost
Query OK, 0 rows affected (0.00 sec)
6:安装MySQL-python
[root@server4 ~]# yum install MySQL-python -y
7:测试
[root@server4 ~]# salt server6 test.ping
server6:
True
[root@server4 ~]# salt server5 test.ping
server5:
True
8:登陆数据库,查看返回值
[root@server4 ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 35
Server version: 5.5.52-MariaDB MariaDB Server
Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> 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
MariaDB [salt]> show tables;
+----------------+
| Tables_in_salt |
+----------------+
| jids |
| salt_events |
| salt_returns |
+----------------+
3 rows in set (0.00 sec)
MariaDB [salt]> select * from salt_returns;
+-----------+----------------------+-----------+---------+---------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------------------+
| fun | jid | return | id | success | full_ret | alter_time |
+-----------+----------------------+-----------+---------+---------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------------------+
| cmd.run | 20190403092404685744 | "server7" | server7 | 1 | {"fun_args": ["hostname"], "jid": "20190403092404685744", "return": "server7", "retcode": 0, "success": true, "fun": "cmd.run", "id": "server7"} | 2019-04-03 09:24:04 |
| test.ping | 20190403093943616949 | true | server5 | 1 | {"fun_args": [], "jid": "20190403093943616949", "return": true, "retcode": 0, "success": true, "cmd": "_return", "_stamp": "2019-04-03T01:39:43.816971", "fun": "test.ping", "id": "server5"} | 2019-04-03 09:39:43 |
| test.ping | 20190403093943731170 | true | server5 | 1 | {"fun_args": [], "jid": "20190403093943731170", "return": true, "retcode": 0, "success": true, "cmd": "_return", "_stamp": "2019-04-03T01:39:43.890905", "fun": "test.ping", "id": "server5"} | 2019-04-03 09:39:43 |
| test.ping | 20190403093943627113 | true | server5 | 1 | {"fun_args": [], "jid": "20190403093943627113", "return": true, "retcode": 0, "success": true, "cmd": "_return", "_stamp": "2019-04-03T01:39:43.897164", "fun": "test.ping", "id": "server5"} | 2019-04-03 09:39:43 |
| test.ping | 20190403094311004990 | true | server6 | 1 | {"fun_args": [], "jid": "20190403094311004990", "return": true, "retcode": 0, "success": true, "cmd": "_return", "_stamp": "2019-04-03T01:43:11.209314", "fun": "test.ping", "id": "server6"} | 2019-04-03 09:43:11 |
| test.ping | 20190403094310790152 | true | server5 | 1 | {"fun_args": [], "jid": "20190403094310790152", "return": true, "retcode": 0, "success": true, "cmd": "_return", "_stamp": "2019-04-03T01:43:11.328342", "fun": "test.ping", "id": "server5"} | 2019-04-03 09:43:11 |
| test.ping | 20190403094321537943 | true | server5 | 1 | {"fun_args": [], "jid": "20190403094321537943", "return": true, "retcode": 0, "success": true, "cmd": "_return", "_stamp": "2019-04-03T01:43:21.787427", "fun": "test.ping", "id": "server5"} | 2019-04-03 09:43:21 |
+-----------+----------------------+-----------+---------+---------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------------------+
7 rows in set (0.00 sec)
二:远程执行
注:server1为master,各节点使用的是rhel6.5
模块:
[root@server1 salt]# mkdir _modules
[root@server1 salt]# cd _modules/
[root@server1 _modules]# vim my_disk.py
#!/usr/bin/env python
def df(): #定义df函数
cmd = 'df -h'
return __salt__['cmd.run'](cmd)
[root@server1 _modules]# salt server4 saltutil.sync_modules
server4:
- modules.my_disk
[root@server4 minion]# yum install tree
[root@server4 minion]# cd /var/cache/salt/minion
[root@server4 minion]# tree .
│ ├── _modules
│ │ └── my_disk.py
[root@server1 _modules]# salt server4 my_disk.df #调用df函数
server4:
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root 19G 1.1G 17G 7% /
tmpfs 246M 64K 246M 1% /dev/shm
/dev/vda1 485M 33M 427M 8% /boot
脚本:
(1).
[root@server1 ~]# vim test.sh
echo "hello world"
[root@server1 ~]# chmod +x test.sh
[root@server1 ~]# ./test.sh
hello world
[root@server1 ~]# salt-cp '*' '/root/test.sh' '/tmp/test.sh'
server1:
----------
/tmp/test.sh:
True
server2:
----------
/tmp/test.sh:
True
server3:
----------
/tmp/test.sh:
True
server4:
----------
/tmp/test.sh:
True
[root@server1 ~]# salt '*' cmd.script '/tmp/test.sh'
server2:
----------
pid:
2399
retcode:
0
stderr:
stdout:
hello world
server3:
----------
pid:
2531
retcode:
0
stderr:
stdout:
hello world
server1:
----------
pid:
25774
retcode:
0
stderr:
stdout:
hello world
server4:
----------
pid:
7946
retcode:
0
stderr:
stdout:
hello world
(2).minion端主动去master端get
[root@server1 ~]# mv test.sh /srv/salt/
[root@server1 ~]# salt '*' cp.get_file salt://test.sh /mnt/test.sh
server4:
/mnt/test.sh
server2:
/mnt/test.sh
server1:
/mnt/test.sh
server3:
/mnt/test.sh
[root@server1 ~]# salt '*' cmd.script '/mnt/test.sh'
server4:
----------
pid:
7985
retcode:
0
stderr:
stdout:
hello world
server2:
----------
pid:
2432
retcode:
0
stderr:
stdout:
hello world
server3:
----------
pid:
2564
retcode:
0
stderr:
stdout:
hello world
server1:
----------
pid:
26102
retcode:
0
stderr:
stdout:
hello world
目录:
[root@server1 ~]# salt '*' cp.get_dir salt://haproxy /mnt/
server2:
- /mnt//haproxy/files/haproxy.cfg
- /mnt//haproxy/install.sls
server4:
- /mnt//haproxy/files/haproxy.cfg
- /mnt//haproxy/install.sls
server3:
- /mnt//haproxy/files/haproxy.cfg
- /mnt//haproxy/install.sls
server1:
- /mnt//haproxy/files/haproxy.cfg
- /mnt//haproxy/install.sls
三:syndic-顶级master
topmaster -> (syndic -> master) -> minion
[root@server1 ~]# salt-key -d server4
The following keys are going to be deleted:
Accepted Keys:
server4
Proceed? [N/y] y
Key for minion server4 deleteed.
[root@server4 ~]# /etc/init.d/keepalived stop
[root@server4 ~]# /etc/init.d/salt-minion stop
[root@server4 ~]# chkconfig salt-minion off
[root@server1 ~]# yum install salt-syndic -y
[root@server1 ~]# cd /etc/salt/
[root@server1 salt]# vim master
syndic_master: server4
#master_job_cache: mysql
#mysql.host: '172.25.60.1'
#mysql.user: 'salt'
#mysql.pass: 'westos'
#mysql.db: 'salt'
#mysql.port: 3306
[root@server1 salt]# /etc/init.d/salt-master restart
[root@server1 salt]# /etc/init.d/salt-syndic start
[root@server4 ~]# yum install -y salt-master
[root@server4 ~]# vim /etc/salt/master
order_masters: True
[root@server4 ~]# /etc/init.d/salt-master start
[root@server4 ~]# salt-key -L
Accepted Keys:
Denied Keys:
Unaccepted Keys:
server1
Rejected Keys:
[root@server4 ~]# salt-key -A
The following keys are going to be accepted:
Unaccepted Keys:
server1
Proceed? [n/Y] Y
Key for minion server1 accepted.
[root@server4 ~]# salt '*' test.ping
server2:
True
server3:
True
server1:
True
[root@server1 salt]# cd /srv/salt/
[root@server1 salt]# vim top.sls
base:
'server1':
- haproxy.install
'roles:httpd':
- match: grain
- httpd.service
'roles:nginx':
- match: grain
- nginx.service
[root@server1 salt]# /etc/init.d/keepalived stop
Stopping keepalived: [ OK ]
[root@server4 ~]# salt '*' state.highstate
#########################################
salt-ssh
#########################################
[root@server2 ~]# /etc/init.d/salt-minion stop
[root@server3 ~]# /etc/init.d/salt-minion stop
[root@server1 salt]# yum install -y salt-ssh
[root@server1 salt]# cd /etc/salt/
[root@server1 salt]# vim roster
server2:
host: 172.25.60.2
user: root
passwd: westos
server3:
host: 172.25.60.3
user: root
passwd: westos
[root@server1 salt]# vim master
#master_job_cache: mysql
#mysql.host: '172.25.60.1'
#mysql.user: 'salt'
#mysql.pass: 'westos'
#mysql.db: 'salt'
#mysql.port: 3306
[root@server1 salt]# /etc/init.d/salt-master start
[root@server1 salt]# salt-ssh '*' test.ping -i
server3:
True
server2:
True
[root@server1 salt]# salt-ssh '*' state.highstate
server3:
Summary for server3
-----------
Succeeded: 0
Failed: 0
-----------
Total states run: 0
Total run time: 0.000 ms
server2:
Summary for server2
-----------
Succeeded: 0
Failed: 0
-----------
Total states run: 0
Total run time: 0.000 ms
四:salt-api
[root@server2 ~]# /etc/init.d/salt-minion start
[root@server3 ~]# /etc/init.d/salt-minion start
[root@server1 salt]# yum install -y salt-api
[root@server1 salt]# cd /etc/pki/tls/certs/
[root@server1 certs]# cd ..
[root@server1 tls]# cd private/
[root@server1 private]# openssl genrsa 2048 > localhost.key
Generating RSA private key, 2048 bit long modulus
.................................................................+++
..............+++
e is 65537 (0x10001)
[root@server1 private]# cd ../certs/
[root@server1 certs]# make testcert
umask 77 ; \
/usr/bin/openssl req -utf8 -new -key /etc/pki/tls/private/localhost.key -x509 -days 365 -out /etc/pki/tls/certs/localhost.crt -set_serial 0
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:cn
State or Province Name (full name) []:shaanxi
Locality Name (eg, city) [Default City]:xi'an
Organization Name (eg, company) [Default Company Ltd]:westos
Organizational Unit Name (eg, section) []:linux
Common Name (eg, your name or your server's hostname) []:server1
Email Address []:root@localhost
[root@server1 salt]# cd /etc/salt/master.d/
[root@server1 master.d]# vim api.conf
rest_cherrypy:
port: 8000
ssl_crt: /etc/pki/tls/certs/localhost.crt
ssl_key: /etc/pki/tls/private/localhost.key
[root@server1 master.d]# vim eauth.conf
external_auth:
pam:
salt-api:
- '.*'
- '@wheel'
- '@runner'
- '@jobs'
[root@server1 master.d]# /etc/init.d/salt-api start
[root@server1 master.d]# /etc/init.d/salt-master restart
[root@server1 certs]# cd -
/etc/salt/master.d
[root@server1 master.d]# useradd salt-api
[root@server1 master.d]# passwd salt-api
Changing password for user salt-api.
New password:
BAD PASSWORD: it is based on a dictionary word
BAD PASSWORD: is too simple
Retype new password:
passwd: all authentication tokens updated successfully.
[root@server1 master.d]# curl -sSk https://localhost:8000/login -H 'Accept: application/x-yaml' -d username=salt-api -d password=westos -d eauth=pam
return:
- eauth: pam
expire: 1527970689.0481751
perms:
- .*
- '@wheel'
- '@runner'
- '@jobs'
start: 1527927489.048173
token: 68aaa2ba29de9614b8ab91f828c9ec0db7427312
user: salt-api
[root@server1 master.d]# curl -sSk https://localhost:8000 \
> -H 'Accept: application/x-yaml' \
> -H 'X-Auth-Token: 68aaa2ba29de9614b8ab91f828c9ec0db7427312' \
> -d client=local \
> -d tgt='*' \
> -d fun=test.ping
return:
- server1: true
server2: true
server3: true
[root@server1 ~]# vim saltapi.py
sapi = SaltAPI(url='https://172.25.60.1:8000',username='salt-api',password='westos')
sapi.token_id()
print sapi.list_all_key()
#sapi.delete_key('test-01')
#sapi.accept_key('test-01')
#sapi.deploy('server2','httpd.service')
#print sapi.remote_noarg_execution('test-01','grains.items')
[root@server1 ~]# python saltapi.py
([u'server1', u'server2', u'server3'], [])
[root@server1 ~]# vim saltapi.py
def main():
sapi = SaltAPI(url='https://172.25.60.1:8000',username='salt-api',password='westos')
sapi.token_id()
#print sapi.list_all_key()
#sapi.delete_key('test-01')
#sapi.accept_key('test-01')
sapi.deploy('server2','httpd.service')
#print sapi.remote_noarg_execution('test-01','grains.items')
[root@server2 ~]# /etc/init.d/httpd stop
[root@server1 ~]# python saltapi.py
[root@server2 ~]# ps ax
3619 ? Ss 0:00 /usr/sbin/httpd
3622 ? S 0:00 /usr/sbin/httpd
3623 ? S 0:00 /usr/sbin/httpd
3624 ? S 0:00 /usr/sbin/httpd
3625 ? S 0:00 /usr/sbin/httpd
3626 ? S 0:00 /usr/sbin/httpd
3627 ? S 0:00 /usr/sbin/httpd
3628 ? S 0:00 /usr/sbin/httpd
3629 ? S 0:00 /usr/sbin/httpd