命令格式:
salt '<操作目标>' <方法> <参数>
1.查看minion001主机使用:
[root@master ~]# salt 'minion001' cmd.run 'free -m' minion001: total used free shared buffers cached Mem: 988 560 428 0 32 305 -/+ buffers/cache: 221 766 Swap: 1983 0 1983 [root@master ~]#
2.-E,--pcre 通过正则表达式匹配主机
测试已minion开头的主机是否连通
[root@master ~]# salt -E '^minion' test.ping minion001: True minion002: True [root@master ~]#
3.-L,--list,以主机id名列表的形式过滤,格式与python列表类似
获取minion001,minion002的系统信息
[root@master ~]# salt -L 'minion001,minion002' grains.items osfullname minion002: ---------- SSDs: biosreleasedate: 01/23/2012 biosversion: 6.00 cpu_flags: - fpu minion001: ---------- SSDs: biosreleasedate: 01/23/2012 biosversion: 6.00 cpu_flags: - fpu - vme - de
4.-G,--grain, 根据被控主机的grains信息进行匹配过滤
格式为:'<grain value>:<glob expression>'
例如:过滤内核为Linux的主机:可写成,‘kernel:Linux'
[root@master ~]# salt -G 'osrelease:6.4' cmd.run 'python -V' minion001: Python 2.6.6 minion002: Python 2.6.6 [root@master ~]#
如果需要配合正则表达式,使用参数--grain-pcre,经测试只能冒号后的<glob expression>能用正则
[root@master ~]# salt --grain-pcre '[a-z]+:linux' test.ping No minions matched the target. No command was sent, no jid was assigned. [root@master ~]# salt --grain-pcre 'kernel:[a-z]+' test.ping minion001: True minion002: True [root@master ~]#
5.-I,--pillar 根据被控主机的pillar信息进行匹配过滤
格式: "对象名称:对象值"
过滤:'apache: httpd' "nginx: root: /data"
待测。。。
6.-N ,--nodegroup ,根据主控端master配置文件/etc/salt/master中的分组进行过滤
组的定义支持正则表达式,grain,条件运算等
分组,官网:http://docs.saltstack.com/en/latest/topics/targeting/nodegroups.html
[root@master ~]# cat /etc/salt/master interface: 192.168.224.141 auto_accept: True file_roots: base: - /srv/salt nodegroups: group1: 'minion001' group2: 'minion002' group3: 'minion001,minion002' group4: '*' group5: 'E@^minion'
[root@master ~]# salt -N group1 test.ping minion001: True [root@master ~]# salt -N group2 test.ping minion002: True [root@master ~]# salt -N group3 test.ping minion002: True minion001: True [root@master ~]# salt -N group4 test.ping minion001: True minion002: True [root@master ~]# salt -N group5 test.ping minion002: True minion001: True [root@master ~]#
7.-C,--compound,根据条件运算符not,and,or匹配不同规则的主机信息
探测id以minion开头,系统为Centos的主机的连通性
[root@master ~]# salt -C 'E@^minion and G@os:Centos' test.ping minion002: True minion001: True [root@master ~]#
8.-S,--ipcidr 根据被控主机的IP地址或IP子网进行匹配
[root@master ~]# salt -S '192.168.224.142' test.ping minion001: True [root@master ~]# salt -S '192.168.224.0/24' test.ping minion002: True minion001: True [root@master ~]#