saltstack_target格式
salt <target> <function> <args>
舉個例子,安裝個gcc
salt \* pkg.installed gcc
<function>就是pkg.installed這東西,說的詳細點應該是pkg這東西是個python模塊,installed纔是裏面定義的一個具體幹活的函數。
<args>顯而易見gcc就是傳給pkg.insatlled這個函數的參數
<function>和<args>這些個東西都比較簡單,大頭是<target>,其實<function>也不簡單,這麼多模塊全搞會也挺難的。
<target>是個什麼東西呢?望文生義,沒錯,就是目標,也就是我們minion的id。我們要執行的函數或者命令,將要作用在哪些機器上面。
樓主在官網和doc裏面查了下,大概有以下幾種匹配方式。
glob(default):"*minion"
PCRE:"web[1-4](apache|nginx)"
List:"minion1,minion2,minion3"
Grans:"os:Debian"
Gran PCRE:'os:(Debian|CentOs)'
NodeGroup:(defined in master config file)
Pillar:'test:test'
Subnet:10.0.0.1/24
compound:混合方式
-X, --exsel
下面就舉個例子看一看
第 1種方式glob聽起來不太懂,其實就shell-style,shell裏能用的,它也就能用。爲啥叫它glob呢,因爲它是默認的,用它的時候不用加 參數,用別人的時候,要加上參數才成,比如用正則表達式要用salt -E,用列表要用salt -L。官網上有幾個例子,我記錄一下。
salt "*" test.ping //這個匹配所有minion salt "*minion" test.ping //這個匹配所有以minion結尾的minion salt "salt?minion" test.ping //這裏面的?匹配任意一個字符 salt "web[1-5]" test.ping //匹配web1,web2,web3,web4,web5 salt "web[1,3]" test.ping //匹配web1,web3 salt "web[a-c]" test.ping //匹配weba,webb,webc
第2種方式,可以用正則表達式去匹配minion id,我自己選了幾個正則測了一下
root@salt-master:~# salt -E "salt-minio." test.ping salt-minion: True root@salt-master:~# root@salt-master:~# salt -E "salt-minio*" test.ping salt-minion: True root@salt-master:~# root@salt-master:~# salt -E "salt(-|~)minion" test.ping salt-minion: True root@salt-master:~# root@salt-master:~# salt -E "^salt" test.ping salt-minion: True
第3種方式,是列表的方式,有啥都羅列出來就OK了
salt -L "minion1,minion2,minion3" test.ping
第4種方式,是根據Grain匹配,Grain是個啥,下一篇再寫吧
salt -G 'oscodename:Wheezy' test.ping
第5種方式是,Grain和PCRE匹配的混合
salt -grain-pcre 'oscodename:(Wheezy|Squeeze)' test.ping
第 6種方式,定義Group組,前面的幾種方式定義minion都可以用在這裏面,如果用列表的話,寫成這樣就可以了 L@minion1,minion2,minion3,用Granin匹配的話寫成G@oscodename:Wheezy,當然也可以用and,or做 進一步匹配
下面的webs就是一個組,用到了列表,和Grains和or
root@salt-master:~# sed -n '583,584p' /etc/salt/master nodegroups: webs: 'L@minion1,minion2,minion3 or G@oscodename:Wheezy' root@salt-master:~# root@salt-master:~# /etc/init.d/salt-master restart [ ok ] Restarting salt master control daemon: salt-master. root@salt-master:~# salt -N "webs" test.ping salt-minion: True root@salt-master:~#
第7種方式,根據pillar匹配,pillar下一篇再寫。先來個例子
root@salt-master:~# salt -I 'apache:apache2' test.ping salt-minion: True root@salt-master:~#
第8種方式,可以根據minion所在的子網,來匹配minion
root@salt-master:~# salt -S "10.240.160.0/24" test.ping salt-minion: True root@salt-master:~#
第9種方式,這個其實是個混合方式,前面的幾種都可以用,還可以用and,or連接起來
root@salt-master:~# salt -C 'G@os:RedHat or L@salt-minion,minion1' test.ping salt-minion: True root@salt-master:~#