今天2015年10月26號是我學習saltstack的第22天,在工作中用到的第二次自己總結出來的經驗10月4號寫下了篇自己第一次用的saltstack的文檔,繼續更新:感謝大家在第一篇給予的意見,多多指導互相學習。
爲了便於管理功能業務相似的minion,Saltstack提供了分組的模式,因爲線上機器多,不可能每次都是“*”或者“ip主機名” 這樣不現實的。 所以我們現在都是提供分組,哪裏的機器分哪個組這樣你操作起來就方便而且容易排查。
官方文檔:http://docs.saltstack.com/topics/targeting/nodegroups.html
Node group爲預先在master配置文件中定義的minion組.用來進行批量對minion進行操作.
編輯配置文件 vi /etc/salt/master
##### Node Groups #####
##########################################
# Node groups allow for logical groupings of minion nodes.
# A group consists of a group name and a compound target.
#
#nodegroups:
# group1: ‘[email protected],bar.domain.com,baz.domain.com and bl*.domain.com’
# group2: ‘G@os:Debian and foo.domain.com’
nodegroups:
testgroup1: '[email protected],test83.salt.cn'
testgroup2: '192.168.2.84'
這裏我設置自己需要的組。
之後重啓master
測試:
[root@test81 ~]# salt -N testgroup1 test.ping
test83.salt.cn:
Truetest82.salt.cn:
True
這裏salt -N #-N 是分組的命令符 後面接分組下發命令。
[root@test81 ~]# salt -N testgroup2 test.ping192.168.2.84:
True
顯示 true
高級用法:
關於組的混合匹配:(Compound matchers)
匹配中可以使用and、or及not等boolean型操作
例如,想匹配所有minion中主機名(minion id)以webserv開頭並且運行在Debian系統上或者minion的主機名(minion id)匹配正則表達式web-dc1-srv.*:
salt -C ‘webserv* and G@os:Debian or E@web-dc1-srv.*’ test.ping
在top.sls中可以如下使用:
base:
‘webserv* and G@os:Debian or E@web-dc1-srv.*’:
– match: compound
– webserver
這裏在提一點,是很多論壇上面我都沒有看到的,我工作中用到了,非常方便。因爲分組也有地區分開,還有就是每次添加分組,編輯/etc/salt/master 非常麻煩。
我們這裏設置了,單獨目錄下面設置group.conf 裏面放分組。 #/etc/salt/include/group.conf
這樣就直接每次按要求格式寫。
特別提示下,這裏分組,後面接的是用戶的hostname,第一個要空格。