昨天原本打算是写salt的WebUi-halite的,不过想了想,还是先写一些“看得见、摸得着的”的一些显而易见,最基本的用处吧。(尝到一些甜头后,才会继续去钻研吧。。。哈哈~)
那,什么是Targeting呢?
官方给到的解释是:
Specifying which minions should run a command or execute a state by matching against hostnames, or system information, or defined groups, or even combinations thereof.
这都看的懂的吧...举个形象点的例子吧,我们可以用这句命令去重启minion端,一台名为web1服务器上面的apache。
salt web1 apache.signal restart 同时,我们也可以在top file里面写死,比如只让minion端的web1,去执行webserver.sls
base:
’web1’:
- webserver
当然,正则什么的也可以用,比如:
salt ’web?.example.net’ test.ping #web1.example.net, web2.example.net . . .webN.example.net
salt ’web[1-5]’ test.ping #web1到web5
salt ’web[1,3]’ test.ping #web1和web3
salt ’web-[x-z]’ test.ping #web-x、web-y和web-z
总之,在salt中,是有很多方法去限制特定的minions或者groups的。
下面是一张compound matcher表,可以针对性的进行操作目标的筛选:
LetterMatch TypeExample GGrans globG@os:Ubuntu E PCRE Minion
ID
E@web\d+\.(dev|qa|prod)\.loc PGrains PCREP@os:(RedHat|Fedora|CentOS) LList of minions L@minion1.example.com,minion3.domain.com or
bl
*
.domain.com
IPillar globI@pdata:foobar S Subnet/IP
address
S@192.168.1.0/24 or S@192.168.1.100 RRange clusterR@%foo.bar 官方的概念和介绍就到这里,下面我们进行实际操作一下:
先说明下,我的实验环境:
IPOSidcharacter192.168.139.131CentOS> 操作前,先照例看一下salt-key:[root@ www]# salt-key -L
Accepted Keys:
True
另外,我们也可以在不修改node groups,直接在命令行进行筛选操作。需要注意的是,这里的matchers也可以用and、or和not。
[root@ www]# salt -C 'G@os:Ubuntu or 192*' test.ping
192.168.139.130-ubuntu:
True
192.168.139.128-centos:
True
同样的,也可以再top file里进行限制:
base:
’G@os:Ubuntu or 192* ’:
- match: compound
- webserver
需要注意的是,not 开头是不支持上面那张compund matchers表格的,我们可以这样来规避:
salt -C ’* and not G@kernel:Darwin’ test.ping
salt -C ’* and not web-dc1-srv’ test.ping
接下来,讲一下文件的手动推送。
首先,在master端的/srv/salt/下面创建一个名为test的文件,里面随便打了一串,adada。然后通过salt进行传送到minion端,root目录下,并重命名为1234。
[root@ salt]# pwd
/srv/salt
[root@ salt]# cat test
adada
[root@ salt]# salt '*' cp.get_file salt://test /root/1234
192.168.139.128-centos:
/root/1234
192.168.139.130-ubuntu:
/root/1234
然后,我们去minion端,查看一下结果。
root@ubuntu:~# ll
total 36