Article From:https://www.cnblogs.com/jimmy-xuli/p/9125182.html
  • SaltstackRemote execution

Syntax example: salt ‘*’ cmd.run’w’

 –  Command: salt

 –  Goal: ‘*’

 –  Module: cmd.run, with 150+ module, or write module by oneself.

 –  Return: return after execution, Returnners component

 

One.SlatStack remote execution — target

1)、Target matching method related to Minion ID

Official website document: https://docs.saltstack.com/en/latest/topics/targeting/index.htmlTwo positioning methods: one is related to minion ID, one is moni.On ID independence1) minion ID related methodsSpecify a specific minion IDSalt'7mini-node2'test.ping

wildcard

salt '*' test.ping 
salt 'linux-node2*' test.ping
salt 'linux-node[1|2].example.com' test.ping
salt 'linux-node[1-2].example.com' test.ping
salt 'linux-node[!2].example.com' test.ping
salt 'linux-node?.example.com' test.ping

regular expression

salt -E 'linux-(node1|node2)*' test.ping
salt -E 'linux-(node1|node2).example.com' test.ping 

Note: all matching targets can be specified in top file.

minion IDSet up the plan: IP address, set according to the business.

Example:Redis-node1-redis04-idc04-h5web.example.comRedis-node1 redis first nodeRedis04 clusterIdc04H5web business line

(2)Minion unrelated matching

1、Grainsmatching[root@linux-node1] salt -G'os:CentOS'test.pingLinux-node2.example.com:TrueLinux-node1.example.com:True2, subnet, IP address matching[root@linux-node1] salt -S'192.168.56.0/24'test.pingLinux-Node1.example.com:TrueLinux-node2.example.com:True3, Pillar matching[root@linux-node1 -] salt-I'apache:httpd'test.pingLinux-node2.example.com:TrueLinux-node1.example.com:True

3) Node GroupsGrouping, you need to configure the master file

vim /etc/salt/master

 

Restart systemctl restart salt-master

[root@7mini-node1 ~]# salt -N web test.ping
7mini-node1:
    True
7mini-node2:
    True

4)A variety of mixed matches (less used)

Official document https://docs.saltstack.com/en/latest/topics/targeting/compound.html

Batch processing can be used to restart all hosts or process scenarios, restart the host or process by percentage or a fixed number of points.

Official document https://docs.saltstack.com/en/latest/topics/targeting/batch.html

 

Two. Implementation

Official website document https://docs.saltstack.com/en/latest/ref/modules/all/index.html#all-salt-modules

List all the salt modules and how to use the help documents.

By default, salt is installed by yum. All modules store path /usr/lib/python2.7/site-packages/salt/modules (CentOS 7).

Example:

salt '*' network.active_tcp  # List the TCP connections that all hosts runSalt '*' "network.arp" lists all the host ARPSalt '*' service.available sshd sshd lists all hosts sshdSalT '*' service.get_all can list all the running services of all hosts.Salt '*' service.status sshd sshd lists all host sshd running status.Salt-cp '*'/etc/hosts /tmp/test master copies the /etc/hosts files on the /tmp/test to the /tmp/test of all hosts.View top by salt '*' state.show_topSalT '*' state.single pkg.installed name=lsof name=lsof, all hosts install lsof

  

3、Return program

 

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

 1)To return to the MySQL library is written directly from minion to MySQL library.

All minion installs the python MySQL module

Two ways to install MYSQL-pythonSalt '*' state.single pkg.installed name=MySQL-pythonorSalt '*' cmd.run'yum insTall MySQL-python -y'

   Create a salt Library

CREATE DATABASE  `salt`
  DEFAULT CHARACTER SET utf8
  DEFAULT COLLATE utf8_general_ci;
USE `salt`;

 Create a table

--
-- 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; 

To grant authorization

grant all on salt.* to salt@'%' identified by 'salt@pw';
FLUSH PRIVILEGES;

 Configuring the minion file

vim /etc/salt/minion
mysql.host: '10.0.0.11'
mysql.user: 'salt'
mysql.pass: 'salt@pw'
mysql.db: 'salt'
mysql.port: 3306

Restart systemctl restart salt-minion

    test

salt '*' test.ping --return mysql

salt '*' cmd.run 'df -h' --return mysql 

  

2)Use salt’s job_cache mechanism to write commands to MySQL.
All commands executed will be written to MySQL without using return to write cache to MySQL.

[root@linux-node1 ~]# vim /etc/salt/master
master_job_cache: mysql
mysql.host: '192.168.56.11'
mysql.user: 'salt'
mysql.pass: 'salt'
mysql.db: 'salt'
mysql.port: 3306
[root@linux-node1 ~]# systemctl restart salt-master
[root@linux-node1 ~]# salt '*' cmd.run 'w'
[root@linux-node1 ~]# mysql -uroot -p123456 -e "select * from salt.salt_returns;"

#You can see Jid with the -v parameter, and you can see the result of running through Jid.[root@linux-node1 -] salt '*' cmd.run'uptime'-vExecuting job withJid 20180118095000725560- -- - --- - ----Linux-node2.example.com:09:50:00 up 14 days, 4:24, 2 users, load average: 0, 0.01, 0.05Linux-node1.example.com:09:50:00 up 23 days, 3:56, 2 users, load average: 0, 0.06, 0.18[root@linux-node1 -] salt-ruN jobs.lookup_jid 20180118095000725560Linux-node1.example.com:09:50:00 up 23 days, 3:56, 2Users, load average: 0, 0.06, 0.18Linux-node2.example.com:09:50:00 up 14 days, 4:24, 2Users, load average: 0, 0.01, 0.05

  

 

  

 

 

 

 

 

Similar Posts:

Link of this Article: Saltstack remote execution (four)

Leave a Reply

Your email address will not be published. Required fields are marked *