1. return of saltstack component
The return component can be understood as the SaltStack system stores or returns the data returned by Minion to other programs. It supports a variety of storage methods, such as MySQL, MongoDB, Redis, Memcache, etc. through return, we can record each operation of SaltStack and provide a data source for future log audit. At present, 30 return data storage and interfaces are officially supported. We can easily configure and use it. Of course, it also supports self-defined returns. Custom returns need to be written in python. After selecting and configuring the return to use, just specify return after the salt command.
//View the list of all return s [root@master ~]# salt '*' sys.list_returners minion01: - carbon - couchdb - etcd - highstate - local - local_cache - mattermost - multi_returner - pushover - rawfile_json - slack - slack_webhook - smtp - splunk - sqlite3 - syslog - telegram minion02: - carbon - couchdb - etcd - highstate - local - local_cache - mattermost - multi_returner - pushover - rawfile_json - slack - slack_webhook - smtp - splunk - sqlite3 - syslog - telegram
2. return process
Return is to trigger the task on the Master side, and then Minion accepts the processing task, directly establishes a connection with the return storage server, and then saves the data return to the storage server. It must be noted that the Minion side operates the storage server in this process, so it is necessary to ensure that the configuration and dependency package of the Minion side are correct, which means that we must install the specified return mode dependency package on each Minion. If Mysql is used as the return storage mode, we will install Python Mysql module on each Minion.
3. Use mysql as the return storage method
Install MySQL Python module on all minion s
[root@master ~]# salt '*' pkg.install python3-PyMySQL minion01: ---------- python3-PyMySQL: ---------- new: 0.10.1-2.module_el8.4.0+666+456f5f48 old:
3.1 deploy a mysql server as a storage server
[root@localhost ~]# yum -y install mariadb mariadb-server [root@localhost ~]# systemctl enable --now mariadb Create database and table structures MariaDB [(none)]> CREATE DATABASE `salt` -> DEFAULT CHARACTER SET utf8 -> DEFAULT COLLATE utf8_general_ci; Query OK, 1 row affected (0.000 sec) MariaDB [(none)]> use 'salt'; //Enter database Database changed MariaDB [salt]> DROP TABLE IF EXISTS `jids`; Query OK, 0 rows affected, 1 warning (0.001 sec) 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.006 sec) MariaDB [salt]> DROP TABLE IF EXISTS `salt_returns`; Query OK, 0 rows affected, 1 warning (0.000 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.041 sec) MariaDB [salt]> DROP TABLE IF EXISTS `salt_events`; Query OK, 0 rows affected, 1 warning (0.000 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.006 sec) MariaDB [salt]> show tables; +----------------+ | Tables_in_salt | +----------------+ | jids | | salt_events | | salt_returns | +----------------+ 3 rows in set (0.001 sec) //Authorized access MariaDB [salt]> grant all on salt.* to salt@'%' identified by 'salt'; Query OK, 0 rows affected (0.001 sec) MariaDB [salt]> flush privileges; Query OK, 0 rows affected (0.001 sec)
Install mariadb on minion01 for testing
[root@minion01 ~]# yum -y install mariadb [root@minion01 ~]# mysql -usalt -psalt -h 192.168.182.143
Configure minion01
[root@minion01 ~]# vim /etc/salt/minion mysql.host: '192.168.182.143' //IP address of the database host used for storage mysql.user: 'salt' mysql.pass: 'salt' mysql.db: 'salt' mysql.port: 3306 [root@minion01 ~]# systemctl restart salt-minion.service
Store the test in mysql on the master
[root@master ~]# salt '*' test.ping minion01: True [root@master ~]# salt '*' test.ping --return mysql minion01: True
Store the test in mysql on the Master
MariaDB [salt]> select * from salt_returns\G *************************** 1. row *************************** fun: test.ping jid: 20211106090212085114 return: true id: minion01 success: 1 full_ret: {"success": true, "return": true, "retcode": 0, "jid": "20211106090212085114", "fun": "test.ping", "fun_args": [], "id": "minion01"} alter_time: 2021-11-06 17:02:12 1 row in set (0.001 sec)
4. job cache
job cache process
When returning, the Minion directly interacts with the storage server. Therefore, it is necessary to install modules with specified storage methods on each Minion, such as Python mysql. Can we directly store the returned results on the Master to the storage server?
The answer is yes. This method is called job cache. It means that after Minion returns the results to the Master, the Master caches the results locally, and then stores the cached results to the specified storage server, such as mysql.
Open the master on the master side_ job_ cache
close minion01 Upper return [root@minion01 ~]# vim /etc/salt/minion #mysql.host: '192.168.182.143' #mysql.user: 'salt' #mysql.pass: 'salt' ##mysql.db: 'salt' #mysql.port: 3306 [root@minion01 ~]# systemctl restart salt-minion.service [root@master ~]# vim /etc/salt/master master_job_cache: mysql mysql.host: '192.168.182.143' mysql.user: 'salt' mysql.pass: 'salt' mysql.db: 'salt' mysql.port: 3306 [root@master ~]# systemctl restart salt-master.service When you want to use job cache Must be installed python3-PyMySQL [root@master ~]# yum -y install python3-PyMySQL [root@master ~]# salt '*' test.ping minion01: True
Empty table contents in database server
MariaDB [salt]> delete from salt_returns; Query OK, 2 rows affected (0.040 sec) MariaDB [salt]> select * from salt_returns\G Empty set (0.000 sec)
Test again on the master to see if it can be stored in the database
[root@master ~]# salt "*" test.ping minion01: True MariaDB [salt]> select * from salt_returns\G *************************** 1. row *************************** fun: test.ping jid: 20211106105510286783 return: true id: minion01 success: 1 full_ret: {"cmd": "_return", "id": "minion01", "success": true, "return": true, "retcode": 0, "jid": "20211106105510286783", "fun": "test.ping", "fun_args": [], "_stamp": "2021-11-06T10:55:10.524486"} alter_time: 2021-11-06 18:55:10 1 row in set (0.001 sec) [root@master ~]# salt '*' cmd.run 'ls /opt' minion01: file file01 file01.bak test MariaDB [salt]> select * from salt_returns\G *************************** 1. row *************************** fun: test.ping jid: 20211106105510286783 return: true id: minion01 success: 1 full_ret: {"cmd": "_return", "id": "minion01", "success": true, "return": true, "retcode": 0, "jid": "20211106105510286783", "fun": "test.ping", "fun_args": [], "_stamp": "2021-11-06T10:55:10.524486"} alter_time: 2021-11-06 18:55:10 *************************** 2. row *************************** fun: cmd.run jid: 20211106105552672396 return: "file\nfile01\nfile01.bak\ntest" id: minion01 success: 1 full_ret: {"cmd": "_return", "id": "minion01", "success": true, "return": "file\nfile01\nfile01.bak\ntest", "retcode": 0, "jid": "20211106105552672396", "fun": "cmd.run", "fun_args": ["ls /opt"], "_stamp": "2021-11-06T10:55:53.000269"} alter_time: 2021-11-06 18:55:53 2 rows in set (0.001 sec)
5. job management
Gets the jid of the task
[root@master ~]# salt '*' cmd.run 'uptime' -v Executing job with jid 20211106105759452279 ------------------------------------------- minion01: 18:58:00 up 4:02, 1 user, load average: 0.00, 0.00, 0.00 //This is the jid for this command
Get the return result of this task through jid
[root@master ~]# salt-run jobs.lookup_jid 20211106105759452279 minion01: 18:58:00 up 4:02, 1 user, load average: 0.00, 0.00, 0.00
list_jobs will parse the cached execution data and display all job data of jobs that have been or partially returned
[root@master ~]# salt-run jobs.list_jobs 20211106104547659513: ---------- Arguments: Function: test.ping StartTime: 2021, Nov 06 10:45:47.659513 Target: * Target-type: glob User: root 20211106105510286783: ---------- Arguments: Function: test.ping StartTime: 2021, Nov 06 10:55:10.286783 Target: * Target-type: glob User: root 20211106105552672396: ---------- Arguments: - ls /opt Function: cmd.run StartTime: 2021, Nov 06 10:55:52.672396 Target: * Target-type: glob User: root 20211106105759452279: ---------- Arguments: - uptime Function: cmd.run StartTime: 2021, Nov 06 10:57:59.452279 Target: * Target-type: glob User: root 20211106110003339024: ---------- Arguments: Function: runner.jobs.lookup_jid StartTime: 2021, Nov 06 11:00:03.339024 Target: master_master Target-type: list User: root 20211106110701863719: ---------- Arguments: Function: runner.jobs.active StartTime: 2021, Nov 06 11:07:01.863719 Target: master_master Target-type: list User: root 20211106110732721807: ---------- Arguments: Function: saltutil.running StartTime: 2021, Nov 06 11:07:32.721807 Target: * Target-type: glob User: root 20211106110846994548: ---------- Arguments: Function: runner.jobs.active StartTime: 2021, Nov 06 11:08:46.994548 Target: master_master Target-type: list User: root 20211106110848797724: ---------- Arguments: Function: saltutil.running StartTime: 2021, Nov 06 11:08:48.797724 Target: * Target-type: glob User: root