使用saltstack的原因是为了对批量的机器执行相同的操作。大的来说上千台机器,不可能所有的机器都运行相同的业务,有可能这一百台运行的是web、另外一百台运行的是db ,所以分组就显的比较有用。

首先如果不分组,直接用salt命令执行是不是也可以呢?

一、配置分组

1[root@localhost ~]# salt -C 'P@os:CentOS' test.ping
2host172:
3    True
4host174:
5    True

从上面执行的结果看,是OK的。那为什么还要引入分组,当然是为了简化这个过程,以后只需要 -N +组句就ok了,而且也便于区分。

为minion进行预先分组配置非常简单,只需要编辑/etc/salt/master文件即可。示例如下:

1[root@localhost ~]# vim /etc/salt/master
2nodegroups:
3  group1: 'L@host172,host174'
4  group2: 'S@192.168.10.172'
5  group3: 'P@os:CentOS'

进行test.ping测试如下:

 1[root@localhost ~]# salt -N group1 test.ping
 2host172:
 3    True
 4host174:
 5    True
 6[root@localhost ~]# salt -N group2 test.ping
 7host172:
 8    True
 9[root@localhost ~]# salt -N group3 test.ping
10host172:
11    True
12host174:
13    True

二、分组语法

nodegroup分组时可以用到的语法关键字有G、E、P、L、I、S、R、D几个,几者的意义和用法见下表:

saltgroup

此外,匹配中可以使用and、or及not等boolean型操作。例:

想匹配所有minion中主机名(minion id)以webserv开头并且运行在Debian系统上或者minion的主机名(minion id)匹配正则表达式web-dc1-srv.* ,就可以用下表方式表示:

1salt -C 'webserv* and G@os:Debian or E@web-dc1-srv.*' test.ping

当然也可以在预先分组时将这个配置写在分组规则里。在top.sls中可以如下使用:

1base:
2  'webserv* and G@os:Debian or E@web-dc1-srv.*':
3    – match: compound
4    – webserver

参考页面:
http://docs.saltstack.com/topics/targeting/nodegroups.html
http://docs.saltstack.com/ref/states/top.html