salt匹配规则有多种,对应各种不同的场景,相关参数如下图所示。现总结如下:

wKiom1ky3yiwr4DrAAAUT_jsDgU181.png-wh_50

1、正则匹配

1
2
3
4
5
6
7
8
9
10
[root@salt-master ~] # salt -E 'salt-minion(01|02).contoso.com' test.ping
salt-minion01.contoso.com:
     True
salt-minion02.contoso.com:
     True
[root@salt-master ~] # salt -E 'salt-minion0[1-2].contoso.com' test.ping
salt-minion02.contoso.com:
     True
salt-minion01.contoso.com:
     True


2、列表匹配

1
2
3
4
5
[root@salt-master ~] # salt -L 'salt-minion01.contoso.com,salt-minion02.contoso.com' test.ping
salt-minion01.contoso.com:
     True
salt-minion02.contoso.com:
     True

3、Grains匹配

saltstack Grains项可以通过如下命令获取:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
root@salt-master ~] # salt 'salt-minion01.contoso.com' grains.items
salt-minion01.contoso.com:
     ----------
     SSDs:
     biosreleasedate:
         07 /02/2015
     biosversion:
         6.00
     cpu_flags:
         - fpu
         - vme
         - mtrr
         - sse
         - sse2
         - ssse3
          ....
     cpu_model:
         Intel(R) Core(TM) i7-6820HQ CPU @ 2.70GHz
     cpuarch:
         x86_64
     domain:
         contoso.com
     fqdn:
         salt-minion01.contoso.com
     fqdn_ip4:
         - 192.168.49.101
     fqdn_ip6:
     gpus:
         |_
           ----------
           model:
               SVGA II Adapter
           vendor:
               unknown
     host:
         salt-minion01
     hwaddr_interfaces:
         ----------
         eth0:
             00:0c:29:f3:43:86
         lo:
             00:00:00:00:00:00
     id :
         salt-minion01.contoso.com
     init:
         upstart
     ip4_interfaces:
         ----------
         eth0:
             - 192.168.49.101
         lo:
             - 127.0.0.1
     ip6_interfaces:
         ----------
         eth0:
             - fe80::20c:29ff:fef3:4386
         lo:
             - ::1
     ip_interfaces:
         ----------
         eth0:
             - 192.168.49.101
             - fe80::20c:29ff:fef3:4386
         lo:
             - 127.0.0.1
             - ::1
     ipv4:
         - 127.0.0.1
         - 192.168.49.101
     ipv6:
         - ::1
         - fe80::20c:29ff:fef3:4386
     kernel:
         Linux
     kernelrelease:
         2.6.32-431.el6.x86_64
     locale_info:
         ----------
         defaultencoding:
             UTF8
         defaultlanguage:
             en_US
         detectedencoding:
             UTF-8
     localhost:
         salt-minion01.contoso.com
     lsb_distrib_codename:
         Final
     lsb_distrib_id:
         CentOS
     lsb_distrib_release:
         6.5
     machine_id:
         ff172a920d48e1f109f755820000000a
     manufacturer:
         VMware, Inc.
     master:
         salt-master.contoso.com
     mdadm:
     mem_total:
         980
     nodename:
         salt-minion01.contoso.com
     num_cpus:
         1
     num_gpus:
         1
     os:
         CentOS
     os_family:
         RedHat
     osarch:
         x86_64
     oscodename:
         Final
     osfinger:
         CentOS-6
     osfullname:
         CentOS
     osmajorrelease:
         6
     osrelease:
         6.5
     osrelease_info:
         - 6
         - 5
     path:
         /sbin : /usr/sbin : /bin : /usr/bin
     productname:
         VMware Virtual Platform
     ps :
         ps  -efH
     pythonexecutable:
         /usr/bin/python2 .6
     pythonpath:
         /usr/bin
         /usr/lib64/python26 .zip
         /usr/lib64/python2 .6
         /usr/lib64/python2 .6 /plat-linux2
         /usr/lib64/python2 .6 /lib-tk
         /usr/lib64/python2 .6 /lib-old
         /usr/lib64/python2 .6 /lib-dynload
         /usr/lib64/python2 .6 /site-packages
         /usr/lib64/python2 .6 /site-packages/gtk-2 .0
         /usr/lib/python2 .6 /site-packages
         /usr/lib/python2 .6 /site-packages/setuptools-0 .6c11-py2.6.egg-info
     pythonversion:
         - 2
         - 6
         - 6
         - final
         - 0
     saltpath:
         /usr/lib/python2 .6 /site-packages/salt
     saltversion:
         2015.5.10
     saltversioninfo:
         - 2015
         - 5
         - 10
         - 0
     selinux:
         ----------
         enabled:
             False
         enforced:
             Disabled
     serialnumber:
         VMware-56 4d c8  bc  18 86 5f 20-cb 2f 72 e2 bb f3 43 86
     server_id:
         175207945
     shell:
         /bin/sh
     virtual:
         VMware
     zmqversion:
         3.2.5

当然,也可以就某一个Grains项进行查询,如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@salt-master ~] # salt 'salt-minion01.contoso.com' grains.item os
salt-minion01.contoso.com:
     ----------
     os:
         CentOS
[root@salt-master ~] # salt 'salt-minion01.contoso.com' grains.item kernelrelease
salt-minion01.contoso.com:
     ----------
     kernelrelease:
         2.6.32-431.el6.x86_64
[root@salt-master ~] # salt 'salt-minion01.contoso.com' grains.item host
salt-minion01.contoso.com:
     ----------
     host:
         salt-minion01

获取到上述Grains项之后,就可以有目的的进行匹配了,示例如下:

1
2
3
4
5
6
7
8
9
10
[root@salt-master ~] # salt -G 'os:CentOS' test.ping
salt-minion02.contoso.com:
     True
salt-minion01.contoso.com:
     True
[root@salt-master ~] # salt -G 'kernelrelease:2.6.32-431.el6.x86_64' test.ping
salt-minion02.contoso.com:
     True
salt-minion01.contoso.com:
     True

4、组匹配

首先,编辑master的配置文件,并添加如下内容:

vim /etc/salt/master

nodegroups:

  testgroup1: 'L@salt-minion01.contoso.com,salt-minion02.contoso.com'

  testgroup2: 'G@os:CentOS and G@domain:contoso.com'

  testgroup3: 'G@os:CentOS and E@salt-minion01.*'

然后就可以根据master配置中定义中的组进行匹配,示例如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@salt-master ~] # salt -N testgroup1 test.ping
salt-minion01.contoso.com:
     True
salt-minion02.contoso.com:
     True
[root@salt-master ~] # salt -N testgroup2 test.ping
salt-minion01.contoso.com:
     True
salt-minion02.contoso.com:
     True
[root@salt-master ~] # salt -N testgroup3 test.ping
salt-minion01.contoso.com:
     True

5、复合匹配

1
2
3
4
5
6
7
8
9
10
11
12
[root@salt-master ~] # salt -C 'G@os:CentOS and E@salt-minion02*' test.ping
salt-minion02.contoso.com:
     True
salt-minion01.contoso.com:
     True
[root@salt-master ~] # salt -C 'G@host:salt-minion01 or L@salt-minion02.contoso.com' test.pingsalt-minion01.contoso.com:
     True
salt-minion02.contoso.com:
     True
[root@salt-master ~] # salt -C 'G@host:salt-minion01 and G@domain:contoso.com' test.ping
salt-minion01.contoso.com:
     True

6、CIDR匹配

1
2
3
4
5
6
7
8
[root@salt-master ~] # salt -S '192.168.49.0/24' test.ping
salt-minion01.contoso.com:
     True
salt-minion02.contoso.com:
     True
[root@salt-master ~] # salt -S '192.168.49.101' test.ping
salt-minion01.contoso.com:
     True

7、Pillar匹配(暂时省却,后续添加)