saltstack远程执行命令.md
saltstack 支持在master上向client 远程执行命令,并显示命令执行的结果
命令格式:
salt '<操作目标>' <方法> [参数]
示例
- 示例1 ping检查agent
*:代表所有minion主机;test:模块; ping:test模块的一个方法,这里的单引号也可以使用双引号
[root@stack-master ~]# salt '*' test.ping
stack-master:
True
stack-node01:
True
stack-node02:
True
- 示例2 执行命令
使用cmd.run远程执行命令,cmd是模块,run是cmd模块的一个方法
[root@stack-master ~]# salt '*' cmd.run "free -m"
stack-master:
total used free shared buffers cached
Mem: 1877 670 1207 0 28 225
-/+ buffers/cache: 415 1461
Swap: 4095 0 4095
stack-node01:
total used free shared buffers cached
Mem: 1877 377 1499 0 27 223
-/+ buffers/cache: 126 1751
Swap: 4095 0 4095
stack-node02:
total used free shared buffers cached
Mem: 1877 377 1499 0 28 223
-/+ buffers/cache: 125 1751
Swap: 4095 0 4095
针对<操作目标> saltstack 提供了多种方法对client(id)进行过滤
- 普通匹配
匹配所有 '*'
匹配单个 'minion_id'
- 正则匹配
使用-E ,--pcre 进行正则匹配
[root@stack-master ~]# salt -E '^stack-node\w+' cmd.run "free -m"
stack-node01:
total used free shared buffers cached
Mem: 1877 377 1499 0 27 223
-/+ buffers/cache: 126 1751
Swap: 4095 0 4095
stack-node02:
total used free shared buffers cached
Mem: 1877 377 1499 0 28 223
-/+ buffers/cache: 125 1751
Swap: 4095 0 4095
- 列表匹配
-L,--list,以主机id名列表的形式进行过滤,格式与Python的列表相似,即不同主机id 名称使用逗号分隔
[root@stack-master ~]# salt -L 'stack-node01,stack-node02' cmd.run "free -m"
stack-node02:
total used free shared buffers cached
Mem: 1877 376 1501 0 28 223
-/+ buffers/cache: 123 1753
Swap: 4095 0 4095
stack-node01:
total used free shared buffers cached
Mem: 1877 378 1499 0 28 223
-/+ buffers/cache: 126 1751
Swap: 4095 0 4095
- IP匹配
-S,--ipcidr,根据被控主机的IP地址或IP子网进行匹配
[root@stack-master ~]# salt -S 172.16.202.0/24 cmd.run "free -m"
stack-master:
total used free shared buffers cached
Mem: 1877 676 1201 0 30 225
-/+ buffers/cache: 420 1457
Swap: 4095 0 4095
stack-node01:
total used free shared buffers cached
Mem: 1877 378 1498 0 28 223
-/+ buffers/cache: 126 1751
Swap: 4095 0 4095
stack-node02:
total used free shared buffers cached
Mem: 1877 376 1500 0 28 223
-/+ buffers/cache: 123 1753
Swap: 4095 0 4095
学习提高自己,能力证明自己,技能创造价值