Saltstack系列之二——Targeting

    昨天原本打算是寫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

[email protected],minion3.domain.com or

bl

*

.domain.com

         IPillar globI@pdata:foobar
         S

Subnet/IP

address

[email protected]/24 or [email protected]
         RRange clusterR@%foo.bar

    官方的概念和介紹就到這裏,下面我們進行實際操作一下:

    先說明下,我的實驗環境:

IPOSidcharacter
192.168.139.131CentOS release 6.5 (Final)192.168.139.131master
192.168.139.128CentOS release 6.5 (Final)192.168.139.128-centosminion
192.168.139.130Ubuntu 12.04.4192.168.139.130-ubuntuminion

    操作前,先照例看一下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

    基本的操作寫的差不多了,嚐到甜頭了麼,那我們後續的更新見~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章