liwya 发表于 2018-8-1 10:24:07

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’ test.ping         #web1到web5
  
    salt ’web’ test.ping         #web1和web3
  
    salt ’web-’ 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:# 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,并进行相应的修改。
# vi /etc/salt/masternodegroups:  
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服务,并看下一下结果:
# service salt-master restart  
Stopping salt-master daemon:
  
Starting salt-master daemon:
  
# salt -N group2 test.ping
  
192.168.139.130-ubuntu:
  
    True
  
# salt -N group1 test.ping
  
192.168.139.128-centos:
  
    True
  另外,我们也可以在不修改node groups,直接在命令行进行筛选操作。需要注意的是,这里的matchers也可以用and、or和not。
# 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。
# pwd  
/srv/salt
  
# cat test
  
adada
  
# 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 Jul1 23:55 ../
  
-rw-r--r--1 root root    6 Jul 28 06:09 1234
  
-rw-------1 root root525 Jul 28 02:01 .bash_history
  
-rw-r--r--1 root root 3106 Apr 192012 .bashrc
  
drwx------2 root root 4096 Jul2 00:02 .cache/
  
-rw-r--r--1 root root140 Apr 192012 .profile
  
drwxr-xr-x2 root root 4096 Jul 28 02:47 .rpmdb/
  
-rw-------1 root root749 Jul 28 01:52 .viminfo
  
root@ubuntu:~# cat 1234
  
adada
  
# cd /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
  
# cat 1234
  
adada
  呼。。。文件传送演示好了。唉,你有可能会说,“我手动去那些服务器上,一个个改那些东西不是更快么?”,的确(在数量少的情况下,你不想"偷懒"谁也拦不住你。。。)。总之salt是在有一定规模的服务器后,才能显示出其魅力的。
  另外,我们也可以在master端,通过salt进行对某台、某些或者全部minions的操作。(还记得上面提到的compound matcher表和and or not么),稍微演示一下:
# salt '*'cmd 'ps -ef|grep http'  
192.168.139.130-ubuntu:
  
    'cmd' is not available.
  
192.168.139.128-centos:
  
    'cmd' is not available.
  
# salt '*'cmd.run 'ps -ef|grep http'
  
192.168.139.130-ubuntu:
  
    root      358435830 02:14 ?      00:00:00 /bin/sh -c vi /etc/www/httpd/conf/httpd.conf
  
    root      358535840 02:14 ?      00:00:00 vi /etc/www/httpd/conf/httpd.conf
  
    root      572457230 06:24 ?      00:00:00 /bin/sh -c ps -ef|grep http
  
    root      572657240 06:24 ?      00:00:00 grep http
  
192.168.139.128-centos:
  
    root   28979   10 09:40 ?      00:00:00 /usr/sbin/httpd
  
    apache   30026 289790 11:19 ?      00:00:00 /usr/sbin/httpd
  
    apache   30027 289790 11:19 ?      00:00:00 /usr/sbin/httpd
  
    apache   30028 289790 11:19 ?      00:00:00 /usr/sbin/httpd
  
    apache   30029 289790 11:19 ?      00:00:00 /usr/sbin/httpd
  
    apache   30030 289790 11:19 ?      00:00:00 /usr/sbin/httpd
  
    apache   30031 289790 11:19 ?      00:00:00 /usr/sbin/httpd
    apache   30032 289790 11:19 ?      00:00:00 /usr/sbin/httpd  
    apache   30033 289790 11:19 ?      00:00:00 /usr/sbin/httpd
  
    root   30566 305650 12:21 ?      00:00:00 /bin/sh -c ps -ef|grep http
  
    root   30568 305660 12:21 ?      00:00:00 grep http
  
# 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
  
# salt -N group1cmd.run 'df -h'
  
192.168.139.128-centos:
  
    Filesystem            SizeUsed Avail Use% Mounted on
  
    /dev/mapper/VolGroup-lv_root
  
                           19G1.3G   17G   8% /
  
    tmpfs               242M   0242M   0% /dev/shm
  
    /dev/sda1             485M   53M407M12% /boot
  基本的操作写的差不多了,尝到甜头了么,那我们后续的更新见~
页: [1]
查看完整版本: Saltstack系列之二——Targeting