1. 主机规划

Targeting Minions文档
- https://docs.saltstack.com/en/latest/contents.html
另请参见:自动化运维神器之saltstack (三)节点组及复合匹配器
注意事项
修改了master或者minion的配置文件,那么必须重启对应的服务。
2. 目标指定方式
|
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
|
L@minion1.example.com,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
|
S@192.168.1.0/24 or S@192.168.1.100
|
No
|
|
R
|
Range cluster
|
R@%foo.bar
|
No
|
Matchers can be joined using boolean and, or, and not operators. 【复合匹配的时候】
2.1. 当前有哪些minion
- 1 [root@salt100 ~]# salt '*' test.ping
- 2 salt02:
- 3 True
- 4 salt100:
- 5 True
- 6 salt03:
- 7 True
- 8 salt01:
- 9 True
3. 通过minion id匹配
3.1. 通配符匹配
在 top file 中也仍然适用。【推荐使用】
- 1 # Match all minions:
- 2 salt '*' test.ping
- 3
- 4 # Match all minions in the example.net domain or any of the example domains:
- 5 salt '*.example.net' test.ping
- 6 salt '*.example.*' test.ping
- 7
- 8 # Match all the 「webN」 minions in the example.net domain (web1.example.net, web2.example.net … webN.example.net):
- 9 salt 'web?.example.net' test.ping
- 10
- 11 # Match the 「web1」 through 「web5」 minions:
- 12 salt 'web[1-5]' test.ping
- 13
- 14 # Match the 「web1」 and 「web3」 minions:
- 15 salt 'web[1,3]' test.ping
- 16
- 17 # Match the 「web-x」, 「web-y」, and 「web-z」 minions:
- 18 salt 'web-[x-z]' test.ping
3.2. 正则表达式(-E)
使用较少,因为正则写错的几率会大些。
正则规则参见:
- https://blog.csdn.net/woshizhangliang999/article/details/46859161
- 1 # Match both 「web1-prod」 and 「web1-devel」 minions:
- 2 salt -E 'web1-(prod|devel)' test.ping
3.2.1. 在 top file 中的使用
- 1 base:
- 2 'web1-(prod|devel)':
- 3 - match: pcre # 使用正则匹配
- 4 - webserver
3.3. 列表匹配(-L)
- salt -L 'web1,web2,web3' test.ping
4. 使用grains指定(-G)
- 1 # For example, the following matches all CentOS minions:
- 2 salt -G 'os:CentOS' test.ping
- 3
- 4 # Match all minions with 64-bit CPUs, and return number of CPU cores for each matching minion:
- 5 salt -G 'cpuarch:x86_64' grains.item num_cpus
4.1. 嵌套匹配【细粒度匹配】
- 1 [root@salt100 ~]# salt -G 'ip_interfaces:eth0' test.ping
- 2 salt01:
- 3 True
- 4 salt02:
- 5 True
- 6 salt03:
- 7 True
- 8 salt100:
- 9 True
- 10 [root@salt100 ~]# salt -G 'ip_interfaces:eth0:*11*' test.ping
- 11 salt01:
- 12 True
5. 使用pillar指定(-I)
像grains匹配一样,也支持嵌套匹配。
- 1 # 具体匹配
- 2 salt -I 'somekey:specialvalue' test.ping
5.1. 嵌套匹配【细粒度匹配】
- 1 [root@salt100 ~]# salt -I 'level1:level2:my_user:*zhang*' test.ping
- 2 salt03:
- 3 True
- 4 salt02:
- 5 True
6. 子网/IP 地址匹配(-S)
- 1 # Minions can easily be matched based on IP address, or by subnet
- 2 salt -S 172.16.1.11 test.ping # 具体地址
- 3 salt -S 172.16.1.0/24 test.ping # 网段
- 4 salt -S fe80::20c:29ff:fe95:1b7a test.ping # IPv 6 具体配置
- 5 salt -S 2001:db8::/64 test.ping # IPv 6 网段配置
6.1. 用于复合匹配
- 1 # Ipcidr matching can also be used in compound matches
- 2 salt -C 'S@10.0.0.0/24 and G@os:Debian' test.ping
6.2. 用于pillar和状态的top file匹配
- 1 '172.16.0.0/12':
- 2 - match: ipcidr # 匹配方式
- 3 - internal
7. 复合匹配(-C)

Matchers can be joined using boolean and, or, and not operators. 【复合匹配的时候】
- 1 # the following string matches all 「Debian minions」 with a hostname that begins with 「webserv」, as well as any minions that have a hostname which matches the regular expression 「web-dc1-srv.* 」:
- 2 salt -C 'webserv* and G@os:Debian or E@web-dc1-srv.*' test.ping
- 3
- 4 # Excluding a minion based on its ID is also possible:
- 5 salt -C 'not web-dc1-srv' test.ping
- 6
- 7 # Versions prior to 2015.8.0 a leading 「not」 was not supported in compound matches. Instead, something like the following was required:
- 8 salt -C '* and not G@kernel:Darwin' test.ping
- 9
- 10 # Excluding a minion based on its ID was also possible:
- 11 salt -C '* and not web-dc1-srv' test.ping
7.1. 在 top file 中的使用
- 1 base:
- 2 'webserv* and G@os:Debian or E@web-dc1-srv.*':
- 3 - match: compound # 复合匹配
- 4 - webserver
7.2. 优先匹配
- 1 # 可以使用括号实现优先匹配
- 2 # 一定要注意括号和目标之间需要「空格」。不遵守此规则可能导致错误的目标!
- 3 salt -C '( ms-1 or G@id:ms-3 ) and G@id:ms-3' test.ping
7.3. 替换分隔符
- 1 # 默认为 「:」 改为其他字符分割
- 2 salt -C 'J|@foo|bar|^foo:bar$ or J!@gitrepo!https://github.com:example/project.git' test.ping
案例1
- 1 [root@salt100 ~]# salt -C 'G@os:redhat03' test.ping
- 2 salt01:
- 3 True
- 4 [root@salt100 ~]#
- 5 [root@salt100 ~]# salt -C 'G|@os|redhat03' test.ping # 将分隔符从「:」 改为「|」
- 6 salt01:
- 7 True
- 8 [root@salt100 ~]# salt -C 'G!@os!redhat03' test.ping #将分隔符从「:」 改为「!」
- 9 salt01:
- 10 True
- 11 [root@salt100 ~]# salt -C 'G!@os!redhat03 or salt02' test.ping
- 12 salt02:
- 13 True
- 14 salt01:
- 15 True
案例2
- 1 [root@salt-master-7 ~]# salt '*' pillar.item getos
- 2 10.0.0.112:
- 3 ----------
- 4 getos:
- 5 ----------
- 6 apache:
- 7 httpd
- 8 git:
- 9 git
- 10 172.16.1.111:
- 11 ----------
- 12 getos:
- 13 ----------
- 14 apache:
- 15 apache2:kkk
- 16 git:
- 17 git-core
- 18 salt-master-7:
- 19 ----------
- 20 getos:
- 21 ----------
- 22 apache:
- 23 httpd
- 24 git:
- 25 git
- 26 [root@salt-master-7 ~]# salt -I 'getos:apache:apache2:kkk' test.ping
- 27 172.16.1.111:
- 28 True
- 29 [root@salt-master-7 ~]# salt -C 'I@getos:apache:apache2:kkk' test.ping # 因为有 apache2:kkk ,所以在某些情况下会出现错误
- 30 172.16.1.111:
- 31 True
- 32 [root@salt-master-7 ~]#
- 33 [root@salt-master-7 ~]# salt -C 'I#@getos#apache#apache2:kkk' test.ping # 表示使用 # 作为分隔符,而不是 :
- 34 172.16.1.111:
- 35 True
8. 节点组(-N)
备注:
- 1 1、当向主配置文件添加或修改节点组时,必须重新启动master,以便完全识别这些更改。
- 2 2、在不重启的情况下,可以使用命令行中 -N 作为目标的有限功能。
8.1. /etc/salt/master 配置
- 1 # The nodegroups master config file parameter is used to define nodegroups. Here's an example nodegroup configuration within 「/etc/salt/master」:
- 2 nodegroups:
- 3 group1: 'L@foo.domain.com,bar.domain.com,baz.domain.com or bl*.domain.com'
- 4 group2: 'G@os:Debian and foo.domain.com'
- 5 group3: 'G@os:Debian and N@group1'
- 6 group4:
- 7 - 'G@foo:bar'
- 8 - 'or'
- 9 - 'G@foo:baz'
- 10
- 11 # As of the 2017.7.0 release of Salt, group names can also be prepended with a dash【破折号】. This brings the usage in line with many other areas of Salt. For example:
- 12 # 组节点也可以使用 如下方式。 组名前面到破折号「-」
- 13 nodegroups:
- 14 - group1: 'L@foo.domain.com,bar.domain.com,baz.domain.com or bl*.domain.com'
- 15
- 16 注意:
- 17 Nodegroups可以参考group3中看到的其他Nodegroups,确保没有循环引用。循环引用将被检测到,并导致部分扩展产生日志错误消息。
- 18 注意:
- 19 「N@」 不能在命令行和top file中使用,只能在master config 中使用
8.2. 命令行匹配
8.3. 在 top file 中的使用
- 1 base:
- 2 group1:
- 3 - match: nodegroup # 使用节点组匹配
- 4 - webserver
8.4. 根据列表的minion IDs定义为节点组
- 1 # 常规的定义方式
- 2 nodegroups:
- 3 group1: L@host1,host2,host3
- 4
- 5 # YAML 定义方式
- 6 nodegroups:
- 7 group1:
- 8 - host1
- 9 - host2
- 10 - host3
9. 批量大小(-b)
- 1 # The 「-b」 (or 「--batch-size」) option allows commands to be executed on only a specified number of minions at a time.
- 2 # 同一时间执行多少 minion,支持百分比和数字。
- 3 salt '*' -b 10 test.ping # 同一时间执行 10 台,完毕后执行另外 10 台,依次执行下去
- 4 salt '*' -b 80% test.ping # 同一时间执行 80% 的minion 端,完毕后执行另外 80%【实际是最后的 20%】。
- 5 salt -G 'os:RedHat' --batch-size 25% apache.signal restart #
- 6
- 7 # --batch-wait minion返回后,等待多少秒在发送命令给新的minion
- 8 salt '*' -b 25% --batch-wait 5 test.ping # 第一批minion反馈后,等待 5 秒后,在发送命令给下一批的minion。
