Saltstack? Use guide 08? Remote execution - return procedure

Keywords: Linux MySQL MariaDB Database saltstack

1. Host planning

 

salt version
1 [root@salt100 ~]# salt --version
2 salt 2018.3.3 (Oxygen)
3 [root@salt100 ~]# salt-minion --version
4 salt-minion 2018.3.3 (Oxygen)

 

Returners document
https://docs.saltstack.com/en/latest/ref/returners/index.html

 

Returner Modules documentation

https://docs.saltstack.com/en/latest/ref/returners/all/index.html#all-salt-returners

 

Matters needing attention

If the configuration file of master or minion is modified, the corresponding service must be restarted.

 

2. Database configuration

2.1. Database installation

Deploy mariadb database in salt100 according to the plan

1 # Database installation
2 yum install -y mariadb mariadb-server    
3 # Start database
4 systemctl start mariadb.service   # If necessary, it can be set to power on and start automatically
5 # Installed on "other" machine to test whether remote connection is possible
6 yum install -y mariadb  

 

2.2. Character set setting

Configuration files also need to be modified

 1 [root@salt100 ~]# mysql
 2 Welcome to the MariaDB monitor.  Commands end with ; or \g.
 3 Your MariaDB connection id is 2
 4 Server version: 5.5.60-MariaDB MariaDB Server
 5 
 6 Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
 7 
 8 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
 9 
10 MariaDB [(none)]> show variables like '%char%';  
11 +--------------------------+----------------------------+
12 | Variable_name            | Value                      |
13 +--------------------------+----------------------------+
14 | character_set_client     | utf8                       |
15 | character_set_connection | utf8                       |
16 | character_set_database   | latin1                     |
17 | character_set_filesystem | binary                     |
18 | character_set_results    | utf8                       |
19 | character_set_server     | latin1                     |
20 | character_set_system     | utf8                       |
21 | character_sets_dir       | /usr/share/mysql/charsets/ |
22 +--------------------------+----------------------------+
23 8 rows in set (0.00 sec)
24 
25 MariaDB [(none)]> set character_set_database=utf8;  
26 Query OK, 0 rows affected (0.00 sec)
27 
28 MariaDB [(none)]> set character_set_server=utf8;  
29 Query OK, 0 rows affected (0.00 sec)
30 
31 MariaDB [(none)]> show variables like '%char%';  
32 +--------------------------+----------------------------+
33 | Variable_name            | Value                      |
34 +--------------------------+----------------------------+
35 | character_set_client     | utf8                       |
36 | character_set_connection | utf8                       |
37 | character_set_database   | utf8                       |
38 | character_set_filesystem | binary                     |
39 | character_set_results    | utf8                       |
40 | character_set_server     | utf8                       |
41 | character_set_system     | utf8                       |
42 | character_sets_dir       | /usr/share/mysql/charsets/ |
43 +--------------------------+----------------------------+
44 8 rows in set (0.01 sec)

 

2.3. Create database, user and authorization

 1 # Create database
 2 MariaDB [(none)]> CREATE DATABASE  `salt`
 3          DEFAULT CHARACTER SET utf8
 4          DEFAULT COLLATE utf8_general_ci;
 5 Query OK, 1 row affected (0.00 sec)
 6 
 7 MariaDB [(none)]> show create database salt;
 8 +----------+---------------------------------------------------------------+
 9 | Database | Create Database                                               |
10 +----------+---------------------------------------------------------------+
11 | salt     | CREATE DATABASE `salt` /*!40100 DEFAULT CHARACTER SET utf8 */ |
12 +----------+---------------------------------------------------------------+
13 1 row in set (0.00 sec)
14 
15 # Create users and authorize
16 MariaDB [(none)]> grant all on salt.* to salt@'%' identified by 'salt';  # For remote access
17 Query OK, 0 rows affected (0.00 sec)
18 
19 MariaDB [(none)]> grant all on salt.* to salt@'salt100' identified by 'salt';   # For local access
20 Query OK, 0 rows affected (0.00 sec)
21 
22 MariaDB [(none)]> flush privileges; 
23 Query OK, 0 rows affected (0.00 sec)
24 
25 MariaDB [(none)]> select user,host,password from mysql.user where user = 'salt';
26 +------+---------+-------------------------------------------+
27 | user | host    | password                                  |
28 +------+---------+-------------------------------------------+
29 | salt | %       | *36F75ABC6D500DFA6E905046FD8BE5E115812DD0 |
30 | salt | salt100 | *36F75ABC6D500DFA6E905046FD8BE5E115812DD0 |
31 +------+---------+-------------------------------------------+
32 2 rows in set (0.00 sec)

 

Local or remote login test

1 mysql -hsalt100 -usalt -psalt
2 # perhaps
3 mysql -h172.16.1.100 -usalt -psalt

 

2.4. Create table

Related documents

https://docs.saltstack.com/en/latest/ref/returners/all/salt.returners.mysql.html#module-salt.returners.mysql

 

 1 use salt;
 2 
 3 --
 4 -- Table structure for table `jids`
 5 --
 6 DROP TABLE IF EXISTS `jids`;
 7 CREATE TABLE `jids` (
 8   `jid` varchar(255) NOT NULL,
 9   `load` mediumtext NOT NULL,
10   UNIQUE KEY `jid` (`jid`)
11 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
12 
13 --
14 -- Table structure for table `salt_returns`
15 --
16 DROP TABLE IF EXISTS `salt_returns`;
17 CREATE TABLE `salt_returns` (
18   `fun` varchar(50) NOT NULL,
19   `jid` varchar(255) NOT NULL,
20   `return` mediumtext NOT NULL,
21   `id` varchar(255) NOT NULL,
22   `success` varchar(10) NOT NULL,
23   `full_ret` mediumtext NOT NULL,
24   `alter_time` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
25   KEY `id` (`id`),
26   KEY `jid` (`jid`),
27   KEY `fun` (`fun`)
28 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
29 
30 --
31 -- Table structure for table `salt_events`
32 --
33 DROP TABLE IF EXISTS `salt_events`;
34 CREATE TABLE `salt_events` (
35 `id` BIGINT NOT NULL AUTO_INCREMENT,
36 `tag` varchar(255) NOT NULL,
37 `data` mediumtext NOT NULL,
38 `alter_time` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
39 `master_id` varchar(255) NOT NULL,
40 PRIMARY KEY (`id`),
41 KEY `tag` (`tag`)
42 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

 

3. Install dependency package

1 salt '*' state.single pkg.installed name=MySQL-python    
2 perhaps
3 salt '*' cmd.run 'yum install -y MySQL-python'    

 

4. minion returns the result to MySQL database

Return to MySQL database directly from minion side, without passing through master side.

 

Related documents

https://docs.saltstack.com/en/latest/topics/jobs/external_cache.html

 

4.1. minion storage architecture

 

4.2. Mode 1: add settings to all Minion terminals

1 [root@salt03 ~]# vim /etc/salt/minion 
2 ............
3 mysql.host: 'salt100'
4 mysql.user: 'salt'
5 mysql.pass: 'salt'
6 mysql.db: 'salt'
7 mysql.port: 3306
8 
9 [root@salt03 ~]# systemctl restart salt-minion.service  # The configuration has been modified, and minion needs to be restarted

 

4.3. Mode 2: add settings on the master side

 1 [root@salt100 ~]# vim /etc/salt/master 
 2 ............
 3 ext_job_cache: mysql
 4 mysql.host: 'salt100'
 5 mysql.user: 'salt'
 6 mysql.pass: 'salt'
 7 mysql.db: 'salt'
 8 mysql.port: 3306
 9 
10 [root@salt100 ~]# systemctl restart salt-master.service   # The configuration has been modified. You need to restart the master

 

4.4. Test execution

 1 # Description: with --return mysql ,Suitable for the above way 1, in minion End configuration
 2 # If not  --return mysql ,Suitable for the above way 2, in master End configuration
 3 [root@salt100 ~]# salt '*' test.ping --return mysql  
 4 salt03:
 5     True
 6 salt01:
 7     True
 8 salt02:
 9     True
10 salt100:
11     True
12 [root@salt100 ~]# salt 'salt0*' cmd.run 'df -h' --return mysql  
13 salt03:
14     Filesystem      Size  Used Avail Use% Mounted on
15     /dev/sda3        18G  2.0G   16G  12% /
16     devtmpfs        901M     0  901M   0% /dev
17     tmpfs           911M   12K  911M   1% /dev/shm
18     tmpfs           911M  9.6M  902M   2% /run
19     tmpfs           911M     0  911M   0% /sys/fs/cgroup
20     /dev/sda1       197M  113M   85M  58% /boot
21     tmpfs           183M     0  183M   0% /run/user/1001
22 salt01:
23     Filesystem      Size  Used Avail Use% Mounted on
24     /dev/sda3        18G  2.1G   16G  12% /
25     devtmpfs        901M     0  901M   0% /dev
26     tmpfs           911M   12K  911M   1% /dev/shm
27     tmpfs           911M  9.6M  902M   2% /run
28     tmpfs           911M     0  911M   0% /sys/fs/cgroup
29     /dev/sda1       197M  113M   85M  58% /boot
30     tmpfs           183M     0  183M   0% /run/user/1001
31 salt02:
32     Filesystem      Size  Used Avail Use% Mounted on
33     /dev/sda3        18G  2.0G   16G  12% /
34     devtmpfs        901M     0  901M   0% /dev
35     tmpfs           911M   12K  911M   1% /dev/shm
36     tmpfs           911M  9.6M  902M   2% /run
37     tmpfs           911M     0  911M   0% /sys/fs/cgroup
38     /dev/sda1       197M  113M   85M  58% /boot
39     tmpfs           183M     0  183M   0% /run/user/1001

 

4.5. Database table information

 

5. The master returns the result to the MySQL database

After the data is returned to the master from the minion end, the master writes it to MySQL

 

Related documents

https://docs.saltstack.com/en/latest/topics/jobs/external_cache.html

 

5.1. master storage architecture

 

5.2. Add the following configuration on the master side

 1 [root@salt100 ~]# vim /etc/salt/master 
 2 ............
 3 master_job_cache: mysql
 4 mysql.host: 'salt100'
 5 mysql.user: 'salt'
 6 mysql.pass: 'salt'
 7 mysql.db: 'salt'
 8 mysql.port: 3306
 9 
10 [root@salt100 ~]# systemctl restart salt-master.service   # The configuration has been modified. You need to restart the master

 

5.3. Test execution

1 [root@salt100 ~]# salt '*' grains.items
2 [root@salt100 ~]# salt '*' cmd.run 'w' 

 

5.4. Database table information

 

 

Posted by KrisCons on Fri, 15 Nov 2019 10:49:19 -0800