SaltStack之远程执行目标选择匹配
Posted warren
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SaltStack之远程执行目标选择匹配相关的知识,希望对你有一定的参考价值。
练习内容
Salt远程执行中目标选择常用的模式
1.通配符匹配
2.正则表达式匹配
3.List支持
4.Grains匹配
5.IP地址匹配
6.混合匹配
7.Node groups
远程执行格式如下:
target就是我们要选择的minion
salt ‘<target>‘ <function> [arguments]
一,通配符匹配,支持minon_id
[[email protected] salt]# salt "*" test.ping linux-node1.example.com: True linux-node2.example.com: True [[email protected]-node1 salt]# salt "*node1*" test.ping linux-node1.example.com: True
二,正则表达式,同样也支持在状态文件里面使用
[[email protected] salt]# salt -E "linux-node[0-1].example.com" test.ping linux-node1.example.com: True [[email protected]-node1 salt]# salt -E "linux-node[0-2].example.com" test.ping linux-node1.example.com: True linux-node2.example.com: True
三,List支持
[[email protected] salt]# salt -L "linux-node1.example.com,linux-node2.example.com" "test.ping" linux-node2.example.com: True linux-node1.example.com: True
四,Grains匹配
[[email protected] salt]# salt -G ‘os:CentOS‘ test.ping linux-node1.example.com: True linux-node2.example.com: True [[email protected]-node1 salt]# salt -G ‘cpuarch:x86_64‘ test.ping linux-node1.example.com: True linux-node2.example.com: True
五,IP地址匹配
[email protected] salt]# salt -S "192.168.56.0/255.255.255.0" test.ping #注意不支持 255.255.255.0子网掩码写法 No minions matched the target. No command was sent, no jid was assigned. ERROR: No return received [[email protected]-node1 salt]# salt -S "192.168.56.0/24" test.ping linux-node1.example.com: True linux-node2.example.com: True [[email protected]-node1 salt]# salt -S "192.168.56.11" test.ping linux-node1.example.com: True
六,混合匹配(不建议使用,看起来有些乱)
[[email protected] salt]# salt -C ‘[email protected] or [email protected][0-2].example.com‘ test.ping linux-node1.example.com: True linux-node2.example.com: True
七,Node groups方式需要写在Master配置文件里面,并重启salt-master服务才能生效
nodegroups: group1: ‘[email protected],bar.domain.com,baz.domain.com and bl*.domain.com‘ group2: ‘[email protected]:Debian and foo.domain.com‘ group3: ‘[email protected]:Debian and [email protected]‘
总结:
1.混合匹配不建议使用,看起来有些乱
2.List模式在平时维护的时候用的少,但是在二次开发建议用Grains采集到数据,然后select拼接成list再调用后端api去执行
3.控制并发可以使用 batch size,简写 -b 如果是1就串行,也支持百分比如 -b 50%
[[email protected] salt]# salt ‘*‘ -b 1 test.ping Executing run on [‘linux-node2.example.com‘] linux-node2.example.com: True retcode: 0 Executing run on [‘linux-node1.example.com‘] linux-node1.example.com: True retcode: 0
百分比使用方法
[[email protected] salt]# salt ‘*‘ -b 50% test.ping Executing run on [‘linux-node2.example.com‘] linux-node2.example.com: True retcode: 0 Executing run on [‘linux-node1.example.com‘] linux-node1.example.com: True retcode: 0
附:英文参考文档
https://www.unixhot.com/docs/saltstack/topics/tutorials/modules.html#target
https://www.unixhot.com/docs/saltstack/topics/targeting/index.html
以上是关于SaltStack之远程执行目标选择匹配的主要内容,如果未能解决你的问题,请参考以下文章