您的位置:首页 > 其它

saltstack(二)target

2016-04-05 14:49 337 查看
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:~#
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: