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

1、正则匹配

[root@salt-master ~]# salt -E 'salt-minion(01|02).contoso.com' test.pingsalt-minion01.contoso.com:    Truesalt-minion02.contoso.com:    True[root@salt-master ~]# salt -E 'salt-minion0[1-2].contoso.com' test.pingsalt-minion02.contoso.com:    Truesalt-minion01.contoso.com:    True

2、列表匹配

[root@salt-master ~]# salt -L 'salt-minion01.contoso.com,salt-minion02.contoso.com' test.pingsalt-minion01.contoso.com:    Truesalt-minion02.contoso.com:    True

3、Grains匹配

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

root@salt-master ~]# salt 'salt-minion01.contoso.com' grains.itemssalt-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项进行查询,如下所示:

[root@salt-master ~]# salt 'salt-minion01.contoso.com' grains.item ossalt-minion01.contoso.com:    ----------    os:        CentOS[root@salt-master ~]# salt 'salt-minion01.contoso.com' grains.item kernelreleasesalt-minion01.contoso.com:    ----------    kernelrelease:        2.6.32-431.el6.x86_64[root@salt-master ~]# salt 'salt-minion01.contoso.com' grains.item hostsalt-minion01.contoso.com:    ----------    host:        salt-minion01

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

[root@salt-master ~]# salt -G 'os:CentOS' test.pingsalt-minion02.contoso.com:    Truesalt-minion01.contoso.com:    True[root@salt-master ~]# salt -G 'kernelrelease:2.6.32-431.el6.x86_64' test.pingsalt-minion02.contoso.com:    Truesalt-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 -N testgroup1 test.pingsalt-minion01.contoso.com:    Truesalt-minion02.contoso.com:    True[root@salt-master ~]# salt -N testgroup2 test.pingsalt-minion01.contoso.com:    Truesalt-minion02.contoso.com:    True[root@salt-master ~]# salt -N testgroup3 test.pingsalt-minion01.contoso.com:    True

5、复合匹配

[root@salt-master ~]# salt -C 'G@os:CentOS and E@salt-minion02*' test.pingsalt-minion02.contoso.com:    Truesalt-minion01.contoso.com:    True[root@salt-master ~]# salt -C 'G@host:salt-minion01 or L@salt-minion02.contoso.com' test.pingsalt-minion01.contoso.com:    Truesalt-minion02.contoso.com:    True[root@salt-master ~]# salt -C 'G@host:salt-minion01 and G@domain:contoso.com' test.pingsalt-minion01.contoso.com:    True

6、CIDR匹配

[root@salt-master ~]# salt -S '192.168.49.0/24' test.pingsalt-minion01.contoso.com:    Truesalt-minion02.contoso.com:    True[root@salt-master ~]# salt -S '192.168.49.101' test.pingsalt-minion01.contoso.com:    True

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