Saltstack系列之二——Targeting

    昨天本来打算是写salt的WebUi-halite的,不过想了想,仍是先写一些“看得见、摸得着的”的一些显而易见,最基本的用处吧。(尝到一些甜头后,才会继续去钻研吧。。。哈哈~)node

    那,什么是Targeting呢?web

    官方给到的解释是:apache

    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.ubuntu

    这都看的懂的吧...举个形象点的例子吧,咱们能够用这句命令去重启minion端,一台名为web1服务器上面的apache。
vim

    

salt web1 apache.signal restart

    同时,咱们也能够在top file里面写死,好比只让minion端的web1,去执行webserver.sls
centos

    

  

  base:
      ’web1’:
         - webserver

    固然,正则什么的也能够用,好比:bash

   

    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表,能够针对性的进行操做目标的筛选:
app


      Letter Match Type Example
         G Grans glob G@os:Ubuntu
         E

PCRE Miniondom

ID

E@web\d+\.(dev|qa|prod)\.loc
         P Grains PCRE P@os:(RedHat|Fedora|CentOS)
         L List of minions

L@minion1.example.com,minion3.domain.com or

bl

*

.domain.com

         I Pillar glob I@pdata:foobar
         S

Subnet/IP

address

S@192.168.1.0/24 or S@192.168.1.100
         R Range cluster R@%foo.bar

    官方的概念和介绍就到这里,下面咱们进行实际操做一下:

    先说明下,个人实验环境:

IP OS id character
192.168.139.131 CentOS release 6.5 (Final) 192.168.139.131 master
192.168.139.128 CentOS release 6.5 (Final) 192.168.139.128-centos minion
192.168.139.130 Ubuntu 12.04.4 192.168.139.130-ubuntu minion

    操做前,先照例看一下salt-key:

[root@ www]# salt-key  -L
Accepted Keys:
192.168.139.128-centos
192.168.139.130-ubuntu
Unaccepted Keys:
Rejected Keys:

    咱们先在master端进行操做,先进行分组,由于只有2台,咱们能够按照上面表格的Grans glob进行简单分组(分组这块,感受应该是要着重考虑的。良好的roles,能够大大提高效率。那么,是按服务、OS、业务来分呢仍是他们某几个的并集呢,等等,我也尚未想好。。。哈哈。。。)

    打开master配置文件,并找到Node Groups,并进行相应的修改。

[root@ www]# vi /etc/salt/master
nodegroups:
  group1: 'G@os:Centos'
  group2: 'G@os:Ubuntu'

    乱入一下....:这里忽然想到一个问题,用户权限的问题。好比我下发权限给Tom,只容许他有test.ping和status.uptime,没有其余权限。(好比,若是执行cmd.run rm -f 就会报错)。咱们也能够再/etc/salt/master里这么来增长:

    client_acl:

       Tom:

         - test.ping

         - status.uptime

    

    修改好后,重启salt-master服务,并看下一下结果:

[root@ www]# service salt-master restart
Stopping salt-master daemon: [  OK  ]
Starting salt-master daemon: [  OK  ]
[root@ www]# salt -N group2 test.ping
192.168.139.130-ubuntu:
    True
[root@ www]# salt -N group1 test.ping
192.168.139.128-centos:
    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
drwx------  4 root root 4096 Jul 28 06:09 ./
drwxr-xr-x 22 root root 4096 Jul  1 23:55 ../
-rw-r--r--  1 root root    6 Jul 28 06:09 1234
-rw-------  1 root root  525 Jul 28 02:01 .bash_history
-rw-r--r--  1 root root 3106 Apr 19  2012 .bashrc
drwx------  2 root root 4096 Jul  2 00:02 .cache/
-rw-r--r--  1 root root  140 Apr 19  2012 .profile
drwxr-xr-x  2 root root 4096 Jul 28 02:47 .rpmdb/
-rw-------  1 root root  749 Jul 28 01:52 .viminfo
root@ubuntu:~# cat 1234
adada
[root@ ~]# cd /root/
[root@ ~]# ll
total 24
-rw-r--r--. 1 root root    6 Jul 29 12:06 1234
-rw-------. 1 root root 1090 Apr 10 00:44 anaconda-ks.cfg
-rw-r--r--. 1 root root 9017 Apr 10 00:44 install.log
-rw-r--r--. 1 root root 3091 Apr 10 00:41 install.log.syslog
[root@ ~]# cat 1234 
adada

    呼。。。文件传送演示好了。唉,你有可能会说,“我手动去那些服务器上,一个个改那些东西不是更快么?”,的确(在数量少的状况下,你不想"偷懒"谁也拦不住你。。。)。总之salt是在有必定规模的服务器后,才能显示出其魅力的。 

    另外,咱们也能够在master端,经过salt进行对某台、某些或者所有minions的操做。(还记得上面提到的compound matcher表和and or not么),稍微演示一下:

[root@ salt]# salt '*'  cmd 'ps -ef|grep http'
192.168.139.130-ubuntu:
    'cmd' is not available.
192.168.139.128-centos:
    'cmd' is not available.
[root@ salt]# salt '*'  cmd.run 'ps -ef|grep http'
192.168.139.130-ubuntu:
    root      3584  3583  0 02:14 ?        00:00:00 /bin/sh -c vi /etc/www/httpd/conf/httpd.conf
    root      3585  3584  0 02:14 ?        00:00:00 vi /etc/www/httpd/conf/httpd.conf
    root      5724  5723  0 06:24 ?        00:00:00 /bin/sh -c ps -ef|grep http
    root      5726  5724  0 06:24 ?        00:00:00 grep http
192.168.139.128-centos:
    root     28979     1  0 09:40 ?        00:00:00 /usr/sbin/httpd
    apache   30026 28979  0 11:19 ?        00:00:00 /usr/sbin/httpd
    apache   30027 28979  0 11:19 ?        00:00:00 /usr/sbin/httpd
    apache   30028 28979  0 11:19 ?        00:00:00 /usr/sbin/httpd
    apache   30029 28979  0 11:19 ?        00:00:00 /usr/sbin/httpd
    apache   30030 28979  0 11:19 ?        00:00:00 /usr/sbin/httpd
    apache   30031 28979  0 11:19 ?        00:00:00 /usr/sbin/httpd
    apache   30032 28979  0 11:19 ?        00:00:00 /usr/sbin/httpd
    apache   30033 28979  0 11:19 ?        00:00:00 /usr/sbin/httpd
    root     30566 30565  0 12:21 ?        00:00:00 /bin/sh -c ps -ef|grep http
    root     30568 30566  0 12:21 ?        00:00:00 grep http
[root@ salt]# salt '*'  cmd.run 'date'
192.168.139.130-ubuntu:
    Mon Jul 28 06:24:17 EDT 2014
192.168.139.128-centos:
    Tue Jul 29 12:21:34 CST 2014
[root@ salt]# salt -N group1  cmd.run 'df -h'
192.168.139.128-centos:
    Filesystem            Size  Used Avail Use% Mounted on
    /dev/mapper/VolGroup-lv_root
                           19G  1.3G   17G   8% /
    tmpfs                 242M     0  242M   0% /dev/shm
    /dev/sda1             485M   53M  407M  12% /boot

    基本的操做写的差很少了,尝到甜头了么,那咱们后续的更新见~

相关文章
相关标签/搜索