第1章 SaltStack远程执行
备注:官方文档:https://docs.saltstack.com/en/latest/topics/tutorials/modules.html
1.1 三大基本要素
- 选择目标
- 执行模块方法和参数
- 命令执行返回的结果
salt '<target>' <function> [arguments]
- 示例
[root@linux-node01 ~]# salt '*' cmd.run 'w' linux-node02:</span> 16:21:24 up 7:21, 1 user, load average: 0.00, 0.01, 0.05 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT root pts/0 10.10.10.1 09:08 30:44 0.54s 0.54s -bash linux-node01:</span> 16:21:24 up 7:23, 2 users, load average: 0.00, 0.01, 0.05 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT root pts/0 10.10.10.1 09:22 4.00s 1.77s 0.47s /usr/bin/python /usr/bin/salt * cmd.run w root pts/1 10.10.10.1 14:23 30:28 0.12s 0.12s -bash</span>
1.2 选择目标
备注:官方文档:https://docs.saltstack.com/en/latest/topics/targeting/index.html
1.2.1 通配符选择匹配
1.2.1.1 匹配所有*
- 示例:
salt '*' test.version
- 示例:
salt '*.example.net' test.version salt '*.example.*' test.version
1.2.1.2 匹配一个字符?
- 示例:匹配example.net, web2.example.net … webN.example.net
salt 'web?.example.net' test.version
1.2.1.3 匹配范围字符[]
- 示例:匹配web1,web2…web5
salt 'web[1-5]' test.version
- 示例:匹配web1和web3
salt 'web[1,3]' test.version
- 示例:匹配web-x,web-y,web-z
salt 'web-[x-z]' test.version
1.2.2 正则选择匹配
使用正则表达式需要添加-E参数使用。
- 示例:匹配web1-prod或web1-devel
salt -E 'web1-(prod|devel)' test.version
- 示例:在sls文件中也可以使用所有选择目标的方式
base: 'web1-(prod|devel)': - match: pcre # 关键字代表使用正则表达式匹配 - webserver # 代表webserver.sls文件
1.2.3 列表选择匹配
使用列表选择时需要添加-L参数使用。
- 示例:匹配web1,web2和web3
salt -L 'web1,web2,web3' test.version
1.2.4 Grains选择匹配
使用Grains选择时需要添加-G item:item-value参数使用。
- 示例:匹配所有CentOS系统的minion
salt -G 'os:CentOS' test.version
- 示例:匹配所有64位CPU的minion并且返回CPU核心数
salt -G 'cpuarch:x86_64' grains.item num_cpus
- 示例:匹配名为ec2_tags标签中包含environment键的字典中包含production单词的grain
salt -G 'ec2_tags:environment:*production*'
1.2.5 Pillar选择匹配
- 示例:
salt -I 'somekey:specialvalue' test.version
- 示例:
salt -I 'foo:bar:baz*' test.version
1.2.6 子网/IP选择匹配
使用子网/IP选择时需要添加-S参数使用。
- 示例:
salt -S 192.168.40.20 test.version salt -S 2001:db8::/64 test.version
1.2.7 混合选择匹配(生产中不建议使用)
混合选择匹配即为以上所有方式使用与(and)或(or)非(not)互相混合使用的方式。
Letter | Match Type | Example | Alt Delimiter |
G | Grains glob | G@os:Ubuntu | Yes |
E | PCRE Minion ID | E@web\d+\.(dev|qa|prod)\.loc | No |
P | Grains PCRE | P@os:(RedHat|Fedora|CentOS) | Yes |
L | List of minions | [email protected],minion3.domain.com or bl*.domain.com | No |
I | Pillar glob | I@pdata:foobar | Yes |
J | Pillar PCRE | J@pdata:^(foo|bar)$ | Yes |
S | Subnet/IP address | [email protected]/24 or [email protected] | No |
R | Range cluster | R@%foo.bar | No |
N | Nodegroups | N@group1 | No |
- 示例:
salt -C '[email protected]/24 and G@os:Debian' test.version
- 示例:
salt -C 'webserv* and G@os:Debian or E@web-dc1-srv.*' test.version
- 示例:
base: 'webserv* and G@os:Debian or E@web-dc1-srv.*': - match: compound - webserver
1.2.8 节点组选择匹配
使用节点组选择时需要在/etc/salt/master添加各节点分组信息后使用,此时需要重新启动master服务,添加-N参数使用。
1.2.8.1 添加以下信息到/etc/salt/master
nodegroups: group1: '[email protected],bar.domain.com,baz.domain.com or bl*.domain.com' group2: 'G@os:Debian and foo.domain.com' group3: 'G@os:Debian and N@group1' group4: - 'G@foo:bar' - 'or' - 'G@foo:baz'
1.2.8.2 命令执行中使用节点组方式
salt -N group1 test.version
1.2.8.3 top.sls中使用节点组方式
base: group1: - match: nodegroup - webserver
1.2.9 批处理选择匹配
使用批处理选择时需要添加-b或–batch-size参数使用。
- 示例:按照并发为10的方式执行命令
salt '*' -b 10 test.version
- 示例:按照并发25%方式执行命令
salt -G 'os:RedHat' --batch-size 25% apache.signal restart
1.3 远程执行模块方法和参数
备注:官方文档:https://docs.saltstack.com/en/latest/ref/modules/all/index.html
1.3.1 执行模块cmdmod
1.3.1.1 功能
直接执行命令模块
注意:模块名称为cmdmod,但是在命令执行中为cmd.run
1.3.1.2 常用方法
- run:执行传递的命令并以字符串形式返回输出
- script:从远程位置下载脚本并在本地执行该脚本
1.3.2 执行模块linux_service
1.3.2.1 功能
收集和管理系统服务(自动识别不同种类版本的系统,如centos6,centos7,ubuntu等)信息
1.3.2.2 常用方法
- get_all:返回所有可用服务的列表
- start(name):批量启动指定的服务
- stop(name):批量关闭指定的服务
- reload_(name):批量重载指定的服务
- restart(name):批量重启指定的服务
1.3.3 执行模块state
1.3.3.1 功能
控制minion的状态系统
1.3.3.2 常用方法
- sls:执行一个或者多个sls文件
- show_top:返回当前minion在top.sls文件中的定义
- single:执行单个状态函数
1.3.4 执行模块network
1.3.4.1 功能
收集和管理网络信息的模块
1.3.4.2 常用方法
- active_tcp:返回一个包含所有正在运行的TCP连接信息的字典(目前仅针对linux和solaris)
- arp:返回ARP信息表
- connect:使用minion的特定端口测试到主机的连接
- dig:返回域名解析结果
- get_hostname:返回主机名
- get_route:返回指定目标ip的路由信息
- netstat:返回有关打开的端口和状态的信息
1.4 Job Cache
1.4.1 Returner组件
SaltStack的Returner组件用来管理salt命令执行后返回的结果,是由Minion直接返回到指定的位置,并非由Master进行返回,默认返回的位置是Master的Job cache中,由Master对结果进行保存。保存的位置和时间如下:
[root@linux-node01 ~]# cd /var/cache/salt/master/jobs/ [root@linux-node01 jobs]# ls 00 0c 1a 24 2c 39 44 4a 4d 5b 66 70 76 7c 80 88 97 9b a7 b7 c4 c9 cd d5 d9 e1 e4 07 11 1b 29 37 40 46 4b 57 61 67 75 79 7d 82 92 99 a0 aa c3 c7 ca d1 d7 e0 e3 f0 [root@linux-node01 jobs]# grep -B 1 keep_jobs /etc/salt/master # Set the number of hours to keep old job information in the job cache: #keep_jobs: 24
备注:官方文档:https://docs.saltstack.com/en/latest/ref/returners/index.html
RETURNER模块文档:https://docs.saltstack.com/en/latest/ref/returners/all/index.html
1.4.2 配置Job Cache到MySQL
1.4.2.1 安装MySQL-python包
[root@linux-node01 ~]# yum install -y MySQL-python
1.4.2.2 编辑Master配置文件
[root@linux-node01 ~]# vim /etc/salt/master # 在文件结尾添加以下内容 master_job_cache: mysql mysql.host: '10.10.10.102' mysql.user: 'salt' mysql.pass: 'salt' mysql.db: 'salt' mysql.port: 3306
1.4.2.3 在mysql中创建数据库和表
[root@linux-node02 ~]# mysql -uroot -p MariaDB [(none)]> 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; -- -- 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; MariaDB [salt]> grant all on salt.* to salt@'10.10.10.%' identified by 'salt';
1.4.2.4 重启Master服务
[root@linux-node01 ~]# systemctl restart salt-master.service
1.4.2.5 验证结果
- 未执行命令之前:
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]> show tables; +----------------+ | Tables_in_salt | +----------------+ | jids | | salt_events | | salt_returns | +----------------+ 3 rows in set (0.00 sec) MariaDB [salt]> select * from salt_returns; Empty set (0.00 sec)
- 运行命令:
[root@linux-node01 ~]# salt '*' test.ping -v Executing job with jid 20200715170407250592 ------------------------------------------- linux-node02: True linux-node01: True
- 执行命令之后:
MariaDB [salt]> select * from salt_returns\G *************************** 1. row *************************** fun: test.ping jid: 20200715170407250592 return: true id: linux-node02 success: 1 full_ret: {"fun_args": [], "jid": "20200715170407250592", "return": true, "retcode": 0, "success": true, "cmd": "_return", "_stamp": "2020-07-15T09:04:07.679952", "fun": "test.ping", "id": "linux-node02"} alter_time: 2020-07-15 17:04:07 *************************** 2. row *************************** fun: test.ping jid: 20200715170407250592 return: true id: linux-node01 success: 1 full_ret: {"fun_args": [], "jid": "20200715170407250592", "return": true, "retcode": 0, "success": true, "cmd": "_return", "_stamp": "2020-07-15T09:04:07.829721", "fun": "test.ping", "id": "linux-node01"} alter_time: 2020-07-15 17:04:07 2 rows in set (0.00 sec)
1.4.3 使用MySQL存储Job Cache好处
- SaltStack做二次开发时:
- 使Master Job Cache将所有的job输出保存到MySQL
- 如果做了管理平台可以将Uid和Jid做关联,可以直接查看到哪个人做了哪个job
- 使用list做目标选择
2022-12-20 20:57:39
,已超过345
天没有更新。某些文章具有时效性,若文章内容或图片资源有错误或已失效,请联系站长。谢谢!转载请注明本文链接:https://blog.leonshadow.cn/763482/2130.html