Saltstack系列之二——Targeting

來源:互聯網
上載者:User

標籤:state   matching   targeting   compund matchers   

    昨天原本打算是寫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表,可以針對性的進行操作目標的篩選:


      Letter Match Type Example
         G Grans glob [email protected]:Ubuntu
         E

PCRE Minion

ID

[email protected]\d+\.(dev|qa|prod)\.loc
         P Grains PCRE [email protected]:(RedHat|Fedora|CentOS)
         L List of minions

[email protected],minion3.domain.com or

bl

*

.domain.com

         I Pillar glob [email protected]:foobar
         S

Subnet/IP

address

[email protected]/24 or [email protected]
         R Range cluster [email protected]%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:

[[email protected] www]# salt-key  -LAccepted Keys:192.168.139.128-centos192.168.139.130-ubuntuUnaccepted Keys:Rejected Keys:

    我們先在master端進行操作,先進行分組,因為只有2台,我們可以按照上面表格的Grans glob進行簡單分組(分組這塊,感覺應該是要著重考慮的。良好的roles,可以大大提升效率。那麼,是按服務、OS、業務來分呢還是他們某幾個的並集呢,等等,我也還沒有想好。。。哈哈。。。)

    開啟master設定檔,並找到Node Groups,並進行相應的修改。

[[email protected] www]# vi /etc/salt/master
nodegroups:  group1: ‘[email protected]:Centos‘  group2: ‘[email protected]:Ubuntu‘

    亂入一下....:這裡突然想到一個問題,使用者權限的問題。比如我下發許可權給Tom,只允許他有test.ping和status.uptime,沒有其他許可權。(比如,如果執行cmd.run rm -f 就會報錯)。我們也可以再/etc/salt/master裡這麼來增加:

    client_acl:

       Tom:

         - test.ping

         - status.uptime

    

    修改好後,重啟salt-master服務,並看下一下結果:

[[email protected] www]# service salt-master restartStopping salt-master daemon: [  OK  ]Starting salt-master daemon: [  OK  ][[email protected] www]# salt -N group2 test.ping192.168.139.130-ubuntu:    True[[email protected] www]# salt -N group1 test.ping192.168.139.128-centos:    True

    另外,我們也可以在不修改node groups,直接在命令列進行篩選操作。需要注意的是,這裡的matchers也可以用and、or和not。

    

[[email protected] www]# salt -C ‘[email protected]:Ubuntu or 192*‘ test.ping192.168.139.130-ubuntu:    True192.168.139.128-centos:    True

    同樣的,也可以再top file裡進行限制:

    

   

 base:     ’[email protected]:Ubuntu or 192* ’:        - match: compound        - webserver

    需要注意的是,not 開頭是不支援上面那張compund matchers表格的,我們可以這樣來規避:

    

    salt -C ’* and not [email protected]:Darwin’ test.ping    salt -C ’* and not web-dc1-srv’ test.ping

    接下來,講一下檔案的手動推送。

    首先,在master端的/srv/salt/下面建立一個名為test的檔案,裡面隨便打了一串,adada。然後通過salt進行傳送到minion端,root目錄下,並重新命名為1234。   

[[email protected] salt]# pwd/srv/salt[[email protected] salt]# cat test adada[[email protected] salt]# salt ‘*‘ cp.get_file salt://test /root/1234192.168.139.128-centos:    /root/1234192.168.139.130-ubuntu:    /root/1234

    然後,我們去minion端,查看一下結果。

[email protected]:~# lltotal 36drwx------  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 .bashrcdrwx------  2 root root 4096 Jul  2 00:02 .cache/-rw-r--r--  1 root root  140 Apr 19  2012 .profiledrwxr-xr-x  2 root root 4096 Jul 28 02:47 .rpmdb/-rw-------  1 root root  749 Jul 28 01:52 .viminfo[email protected]:~# cat 1234adada[[email protected] ~]# cd /root/[[email protected] ~]# lltotal 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[[email protected] ~]# cat 1234 adada

    呼。。。檔案傳送示範好了。唉,你有可能會說,“我手動去那些伺服器上,一個個改那些東西不是更快嗎?”,的確(在數量少的情況下,你不想"偷懶"誰也攔不住你。。。)。總之salt是在有一定規模的伺服器後,才能顯示出其魅力的。 

    另外,我們也可以在master端,通過salt進行對某台、某些或者全部minions的操作。(還記得上面提到的compound matcher表和and or not麼),稍微示範一下:

[[email protected] 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.[[email protected] 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 http192.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[[email protected] salt]# salt ‘*‘  cmd.run ‘date‘192.168.139.130-ubuntu:    Mon Jul 28 06:24:17 EDT 2014192.168.139.128-centos:    Tue Jul 29 12:21:34 CST 2014[[email protected] 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

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

本文出自 “Aaron” 部落格,請務必保留此出處http://qishiding.blog.51cto.com/3381613/1532123

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.