Saltstack远程执行(四)

  • Saltstack远程执行

语法例:salt '*' cmd.run 'w'

 -  命令:salt

 -  目标:'*'

 -  模块:cmd.run,自带150+模块,也可以自己写模块

 -  返回:执行后结果返回,Returnners组件

 

一.SlatStack远程执行--目标

1)、和Minion ID相关的目标匹配方式

官网文档:https://docs.saltstack.com/en/latest/topics/targeting/index.html

两种定位方法:一种和minion ID有关,一种和monion ID无关

    1)minion ID有关的方法

  指定具体的minion ID

salt '7mini-node2' test.ping

通配符

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

正则表达式

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

备注: 所有匹配目标的方式,都可以用到top file里面来指定目标。

minion ID设置方案:IP地址、根据业务来进行设置  

例:
redis-node1-redis04-idc04-h5web.example.com
redis-node1  # redis第一个节点
redis04  # 集群
idc04  # 机房
h5web  # 业务线

(2)和Minion无关匹配

1、Grains匹配
[root@linux-node1 ~]# salt -G 'os:CentOS' test.ping
linux-node2.example.com:
    True
linux-node1.example.com:
    True

2、子网、IP地址匹配
[root@linux-node1 ~]# salt -S '192.168.56.0/24' test.ping
linux-node1.example.com:
    True
linux-node2.example.com:
    True

3、Pillar匹配
[root@linux-node1 ~]# salt -I 'apache:httpd' test.ping
linux-node2.example.com:
    True
linux-node1.example.com:
    True

3) Node Groups分组,需要配置master文件 

vim /etc/salt/master

 

重启systemctl restart salt-master

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

4)各种混合匹配(用的比较少)

官方文档 https://docs.saltstack.com/en/latest/topics/targeting/compound.html

批处理 ,可用于重启所有主机或进程场景,百分比或固定数量的一点一点重启主机或进程

官方文档 https://docs.saltstack.com/en/latest/topics/targeting/batch.html

 

二、执行

官网文档 https://docs.saltstack.com/en/latest/ref/modules/all/index.html#all-salt-modules

列出salt所有模块,以及如何使用的帮助文档

通过yum默认安装salt所有模块存放路径 /usr/lib/python2.7/site-packages/salt/modules(centos 7)

例:

salt '*' network.active_tcp  # 列出所有主机运行的tcp连接
salt '*' network.arp  # 列出所有主机arp
 
salt '*' service.available sshd  # 列出所有主机sshd
salt '*' service.get_all  # 列出所有主机的所有运行服务
salt '*' service.status sshd  # 列出所有主机sshd运行状态
 
salt-cp '*' /etc/hosts /tmp/test  # 将master上/etc/hosts文件拷贝到所有主机的/tmp/test
 
salt '*' state.show_top  # 查看top
salt '*' state.single pkg.installed name=lsof  # 所有主机安装lsof

  

3、返回程序

 

官方文档 https://docs.saltstack.com/en/latest/ref/returners/index.html

 1)将返回写入mysql库,是由minion直接写入mysql库         #不常用

所有minion安装python mysql模块

两种方法安装MYSQL-python
salt '*' state.single pkg.installed name=MySQL-python
或
salt '*' cmd.run 'yum install MySQL-python -y'

   创建salt库

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

授权

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

 配置minion文件

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

重启 systemctl restart salt-minion

    测试

salt '*' test.ping --return mysql

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

  

2)使用salt的job_cache机制将命令写入mysql(常用方法)
执行的所有命令都会写入mysql,不用使用return,把cache写在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;"

#加上-v参数可以看到jid,并且通过jid可以查看运行的结果
[root@linux-node1 ~]# salt '*' cmd.run 'uptime' -v
Executing job with jid 20180118095000725560
-------------------------------------------

linux-node2.example.com:
     09:50:00 up 14 days,  4:24,  2 users,  load average: 0.00, 0.01, 0.05
linux-node1.example.com:
     09:50:00 up 23 days,  3:56,  2 users,  load average: 0.00, 0.06, 0.18
[root@linux-node1 ~]# salt-run jobs.lookup_jid 20180118095000725560
linux-node1.example.com:
     09:50:00 up 23 days,  3:56,  2 users,  load average: 0.00, 0.06, 0.18
linux-node2.example.com:
     09:50:00 up 14 days,  4:24,  2 users,  load average: 0.00, 0.01, 0.05

  

 

  

 

 

 

 

 

posted @ 2018-06-02 13:28  jimmy_xuli  阅读(250)  评论(0编辑  收藏  举报