return和job这两种方式都是对执行结果的返回值进行处理,这个处理可以是是返回给别的程序(sys.list_returners 查看)也可以是存储到数据库中
两者又有一定的区别,假设现在需要将处理结果,存储到数据库中,那么
return的执行流程是
主控端也就是master端对minion并行发出指令,minion端执行这个命令后,将执行的结果返回给主控端,同时将数据存储到数据库中(在这个之前需要数据库对minion进行授权,使他能连接到数据库)
job
return是由minion直接连接到数据库服务器的,而job则是由master与服务器进行连接的,执行完master发出的指令后,将结果返回给master,master将这个结果缓存到本地再将结果存储到数据库服务器上
SaltStack组件之return
//查看所有return列表
[root@localhost salt]# salt '*' sys.list_returners
192.168.161.133:
- carbon
- couchdb
- etcd
- hipchat
- local
- local_cache
- multi_returner
- slack
- smtp
- sqlite3
- syslog
//安装模块Mysql-python
[root@localhost salt]# salt '*' pkg.install MySQL-python
192.168.161.133:
----------
MySQL-python:
----------
new:
1.2.5-1.el7
old:
[root@localhost salt]# salt '*' cmd.run 'rpm -qa|grep MySQL-python'
192.168.161.133:
MySQL-python-1.2.5-1.el7.x86_64
//部署mysql
[root@node2 salt]# yum -y install mariadb-server
。。。
[root@node2 salt]# systemctl start mariadb
[root@node2 salt]# systemctl enable mariadb
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
[root@node2 salt]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 50 *:3306 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
//创建数据库和表结构
[root@node2 salt]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.64-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> CREATE DATABASE `salt`
-> DEFAULT CHARACTER SET utf8
-> DEFAULT COLLATE utf8_general_ci;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> USE `salt`;
Database changed
MariaDB [salt]> CREATE TABLE `jids` (
-> `jid` varchar(255) NOT NULL,
-> `load` mediumtext NOT NULL,
-> UNIQUE KEY `jid` (`jid`)
-> ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Query OK, 0 rows affected (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.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.00 sec)
MariaDB [salt]> show tables;
+----------------+
| Tables_in_salt |
+----------------+
| jids |
| salt_events |
| salt_returns |
+----------------+
3 rows in set (0.00 sec)
MariaDB [salt]> grant all on salt.* to salt@'%' identified by 'salt';
Query OK, 0 rows affected (0.00 sec)
MariaDB [salt]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
//配置minion
[root@localhost salt]# vim /etc/salt/minion
。。。//添加这段
mysql.host: '192.168.161.134'
mysql.user: 'salt'
mysql.pass: 'salt'
mysql.db: 'salt'
mysql.port: 3306
[root@localhost salt]# systemctl restart salt-minion
//测试
[root@localhost salt]# vim /etc/salt/minion
[root@localhost salt]# salt '*' test.ping --return mysql
192.168.161.133:
True
[root@localhost salt]# systemctl stop firewalld
[root@localhost salt]# setenforce 0
//数据库
[root@node2 salt]# systemctl stop firewalld
[root@node2 salt]# setenforce 0
[root@node2 salt]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 5.5.64-MariaDB MariaDB Server
Copyright (c) 2000, 2018, 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]> select * from salt_returns\G
Empty set (0.00 sec)
MariaDB [salt]>
MariaDB [salt]> select * from salt_returns\G
*************************** 1. row ***************************
fun: test.ping
jid: 20200303200039999573
return: true
id: 192.168.161.133
success: 1
full_ret: {"fun_args": [], "jid": "20200303200039999573", "return": true, "retcode": 0, "success": true, "fun": "test.ping", "id": "192.168.161.133"}
alter_time: 2020-03-03 20:00:40
1 row in set (0.00 sec)
job cache流程
[root@localhost salt]# vim /etc/salt/master
。。。//添加如下
master_job_cache: mysql
mysql.host: '192.168.161.134'
mysql.user: 'salt'
mysql.pass: 'salt'
mysql.db: 'salt'
mysql.port: 3306
[root@localhost salt]# systemctl restart salt-master
//在数据库服务器中清空表内容
MariaDB [salt]> delete from salt.salt_returns;
Query OK, 1 row affected (0.01 sec)
MariaDB [salt]> select * from salt.salt_returns;
Empty set (0.00 sec)
//测试
[root@localhost salt]# salt '*' cmd.run 'df -h'
192.168.161.133:
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/rhel-root 17G 1.2G 16G 7% /
devtmpfs 901M 0 901M 0% /dev
tmpfs 912M 16K 912M 1% /dev/shm
tmpfs 912M 8.7M 903M 1% /run
tmpfs 912M 0 912M 0% /sys/fs/cgroup
/dev/sda1 1014M 143M 872M 15% /boot
tmpfs 183M 0 183M 0% /run/user/0
/dev/sr0 3.8G 3.8G 0 100% /mnt
MariaDB [salt]> select * from salt.salt_returns\G
*************************** 1. row ***************************
fun: cmd.run
jid: 20200303200556625422
return: "Filesystem Size Used Avail Use% Mounted on\n/dev/mapper/rhel-root 17G 1.2G 16G 7% /\ndevtmpfs 901M 0 901M 0% /dev\ntmpfs 912M 16K 912M 1% /dev/shm\ntmpfs 912M 8.7M 903M 1% /run\ntmpfs 912M 0 912M 0% /sys/fs/cgroup\n/dev/sda1 1014M 143M 872M 15% /boot\ntmpfs 183M 0 183M 0% /run/user/0\n/dev/sr0 3.8G 3.8G 0 100% /mnt"
id: 192.168.161.133
success: 1
full_ret: {"fun_args": ["df -h"], "jid": "20200303200556625422", "return": "Filesystem Size Used Avail Use% Mounted on\n/dev/mapper/rhel-root 17G 1.2G 16G 7% /\ndevtmpfs 901M 0 901M 0% /dev\ntmpfs 912M 16K 912M 1% /dev/shm\ntmpfs 912M 8.7M 903M 1% /run\ntmpfs 912M 0 912M 0% /sys/fs/cgroup\n/dev/sda1 1014M 143M 872M 15% /boot\ntmpfs 183M 0 183M 0% /run/user/0\n/dev/sr0 3.8G 3.8G 0 100% /mnt", "retcode": 0, "success": true, "cmd": "_return", "_stamp": "2020-03-03T12:05:56.686274", "fun": "cmd.run", "id": "192.168.161.133"}
alter_time: 2020-03-03 20:05:56
1 row in set (0.00 sec)
来源:CSDN
作者:it94wei
链接:https://blog.csdn.net/it94wei/article/details/104646754