saltstack模式匹配

前端之家收集整理的这篇文章主要介绍了saltstack模式匹配前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

wKiom1ky3yiwr4DrAAAUT_jsDgU181.png-wh_50

1、正则匹配

[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、列表匹配

[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项可以通过如下命令获取

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-6820HQcpu@2.70GHz
cpuarch:
x86_64
domain:
contoso.com
fqdn:
salt-minion01.contoso.com
fqdn_ip4:
-192.168.49.101
fqdn_ip6:
gpus:
|_
----------
model:
SVGAIIAdapter
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:
VMwareVirtualPlatform
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-564dc8bc18865f20-cb2f72e2bbf34386
server_id:
175207945
shell:
/bin/sh
virtual:
VMware
zmqversion:
3.2.5

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

[root@salt-master~]#salt'salt-minion01.contoso.com'grains.itemos
salt-minion01.contoso.com:
----------
os:
CentOS
[root@salt-master~]#salt'salt-minion01.contoso.com'grains.itemkernelrelease
salt-minion01.contoso.com:
----------
kernelrelease:
2.6.32-431.el6.x86_64
[root@salt-master~]#salt'salt-minion01.contoso.com'grains.itemhost
salt-minion01.contoso.com:
----------
host:
salt-minion01

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

[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配置中定义中的组进行匹配,示例如下:

[root@salt-master~]#salt-Ntestgroup1test.ping
salt-minion01.contoso.com:
True
salt-minion02.contoso.com:
True
[root@salt-master~]#salt-Ntestgroup2test.ping
salt-minion01.contoso.com:
True
salt-minion02.contoso.com:
True
[root@salt-master~]#salt-Ntestgroup3test.ping
salt-minion01.contoso.com:
True

5、复合匹配

[root@salt-master~]#salt-C'G@os:CentOSandE@salt-minion02*'test.ping
salt-minion02.contoso.com:
True
salt-minion01.contoso.com:
True
[root@salt-master~]#salt-C'G@host:salt-minion01orL@salt-minion02.contoso.com'test.pingsalt-minion01.contoso.com:
True
salt-minion02.contoso.com:
True
[root@salt-master~]#salt-C'G@host:salt-minion01andG@domain:contoso.com'test.ping
salt-minion01.contoso.com:
True

6、CIDR匹配

[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匹配(暂时省却,后续添加

原文链接:https://www.f2er.com/regex/358291.html

猜你在找的正则表达式相关文章