Saltstack 操作目标,正则匹配,及组管理

   如果我们要维护好一个庞大的配置管理系统那么首选得维护好我们的管理对象,在saltstack系统中我们的管理对象叫做Target,

在master上我们可以采用不同Target去管理不同的Minion。这些Target都是通过去管理和匹配Minion的ID来做的一些集

  • 操作目标
  • 正则匹配
  • 组管理

操作目标

   借用别人博客上面的内容 贴在下面

1.匹配Minions Id

匹配所有 (*)

[root@node1 salt]# salt '*' test.ping
node2.minion:
True

匹配后面是.minion的

[root@node1 salt]# salt '*.minion' test.ping
node2.minion:
True

匹配一个(?)

[root@node1 salt]# salt '*node?.minion' test.ping
node2.minion:
True

匹配多个[1-5]

[root@node1 salt]# salt 'node[1-5].minion' test.ping
node2.minion:
True

匹配某个主机和某个

[root@node1 salt]# salt 'node[2,3].minion' test.ping
node2.minion:
True

匹配a-z

[root@node1 salt]# salt 'node2.minio[n-z]' test.ping
node2.minion:
True

正则匹配

命令格式: salt ‘<操作目标>’ <方法>[参数]

示例: 查看被控主机的内存使用情况

[root@hzbj-salt-020 ~]# salt 'hzbj-tomcat-021' cmd.run 'free -m'
hzbj-tomcat-021:
                 total       used       free     shared    buffers     cached
    Mem:           980        406        574          0         17        257
    -/+ buffers/cache:        131        849
    Swap:         1983          0       1983

    其中针对(操作目标),Saltstack提供了多种方法对被控端主机(id)进行过滤。下面列举常用的具体参数。

1)-E,--pcre,通过正则表达式进行匹配。示例:比如hzbj字符开头的主机id名是否连通,命令:

[root@hzbj-salt-020 ~]# salt -E '^hzbj.*' test.ping
hzbj-tomcat-021:
    True
hzbj-tomcat-022:
    True

2)-L,--list,以主机id名列表的形式进行过滤,格式与Python的列表相似,即不同主机id名称使用逗号分隔,命令:

[root@hzbj-salt-020 ~]# salt -L 'hzbj-tomcat-021,hzbj-tomcat-022' grains.item osfullname
hzbj-tomcat-021:
    ----------
    osfullname:
        CentOS
hzbj-tomcat-022:
    ----------
    osfullname:
        CentOS

3)-S,--ipcidr,根据被控主机的IP地址或IP子网进行匹配,示例如下:

[root@hzbj-salt-020 ~]# salt -S 192.168.0.0/16 test.ping
[root@hzbj-salt-020 ~]# salt -S 192.168.56.0/24 test.ping
[root@hzbj-salt-020 ~]# salt -S 192.168.56.21 test.ping

组管理

根据主控端master配置文件中的分组名称进行过滤。 组(主机信息支持正则表达式,grain,条件运算符等),通常根据业务类型划分,不同业务举办相同的特点,包含部署环境,应用平台,配置文件等。

[root@hzbj-salt-020 ~]# vim /etc/salt/master
nodegroups:
   web1group: 'L@hzbj-tomcat-021'
   web2group: 'L@hzbj-tomcat-022'

其中,L@表示后面的主机id格式为列表,即主机id以逗号分隔;G@表示以grain格式描述;S@表示以IP子网或地址格式描述。

示例: 探测web2group被控主机的连通性,命令:

[root@hzbj-salt-020 ~]# salt -N web2group test.ping
hzbj-tomcat-022:
    True
原文地址:https://www.cnblogs.com/yexiaochong/p/6050519.html