SaltStack实战之远程执行-Targeting


SaltStack远程执行组成部分:

  • 目标(Targeting)

  • 模块(Module)

  • 返回(Returnners)

1. minion id配置

minion id可以定义在minion配置文件中,如果未定义,默认使用的是hostname。minion id是不能变动的,因为minion与master认证公钥是以minion id命名文件名的。 
[root@salt-master111 pillar]# vim /etc/salt/minion

id: salt-master111

Minion id命名越规范越详细,在使用minion id匹配过程中就越准确。

2. Targeting分类

和Minion ID有关,需要使用Minion ID:

  • Globbing(通配符)

  • regex(正则表达式)

  • List(列表)

通配符匹配

[root@salt-master111 pillar]# salt "10.1.0.*" test.ping  
10.1.0.112:
   True
10.1.0.95:
   True
10.1.0.50:
   True
10.1.0.96:
   True
[root@salt-master111 pillar]# salt "10.1.0.[!1]*" test.ping
10.1.0.95:
   True
10.1.0.50:
   True
10.1.0.96:
   True

正则匹配

[root@salt-master111 pillar]# salt -E "10.1.0.(95|96)" test.ping  
10.1.0.95:
   True
10.1.0.96:
   True

列表匹配

[root@salt-master111 pillar]# salt -L "10.1.0.95,10.1.0.96" test.ping       
10.1.0.95:
   True
10.1.0.96:
   True

和Minion ID无关,不涉及到Minion ID:

  • 子网/IP地址

  • Grains

  • Pillar

  • Compound matchers(复合匹配)

  • Node groups(节点组)

  • Batching execution(批处理执行)

IP地址匹配

[root@salt-master111 pillar]# salt -S "10.1.0.50" test.ping       
10.1.0.50:
   True

Grains匹配

[root@salt-master111 pillar]# salt -G "os:CentOS" test.ping          
10.1.0.95:
   True
10.1.0.112:
   True
10.1.0.50:
   True
salt-master111:
   True
10.1.0.96:
   True

Pillar匹配

[root@salt-master111 salt]# salt -I "Zabbix_Server:10.1.0.111" test.ping
10.1.0.112:
   True    

复合匹配

LetterMatch Type例如:Alt Delimiter?
GGrains globG@os:UbuntuYes
EPCRE Minion IDE@web\d+\.(dev|qa|prod)\.locNo
PGrains PCREP@os:(RedHat|Fedora|CentOS)Yes
LList of minionsL@minion1.example.com,minion3.domain.com or bl*.domain.comNo
IPillar globI@pdata:foobarYes
JPillar PCREJ@pdata:^(foo|bar)$Yes
SSubnet/IP addressS@192.168.1.0/24 or S@192.168.1.100No
RRange clusterR@%foo.barNo

Matchers can be joined using boolean andor, and not operators.

[root@salt-master111 salt]# salt -C "G@os:CentOS and S@10.1.0.112" test.ping   
10.1.0.112:
   True
[root@salt-master111 salt]#

Nodgroups

nodegroups master配置文件参数用于定义节点组。这里有一个通过/etc/salt/master配置文件配置节点组的例子:

#nodegroups:
#  group1: 'L@foo.domain.com,bar.domain.com,baz.domain.com or bl*.domain.com'
#  group2: 'G@os:Debian and foo.domain.com'
#  group3: 'G@os:Debian and N@group1'
#  group4:
#    - 'G@foo:bar'
#    - 'or'
#    - 'G@foo:baz'
nodegroups:
   test112: '10.1.0.112'
[root@salt-master111 ~]# salt -N 'test112' test.ping           
10.1.0.112:
   True
[root@salt-master111 ~]#

批处理执行

[root@salt-master111 salt]# salt '*' -b 2 test.ping

在top.sls中,使用正则和grains匹配写法:

  "10.1.0.(95|96)":
   - match: pcre
   - apache

 "os:CentOS":
   - match: grain
   - apache


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值