1. 主机规划
Targeting Minions文档
https://docs.saltstack.com/en/latest/contents.html
另请参见:自动化运维神器之saltstack (三)节点组及复合匹配器
注意事项
修改了master或者minion的配置文件,那么必须重启对应的服务。
2. 目标指定方式
Letter |
Match Type |
Example |
|
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&n