saltstack-2 执行命令

命令格式: salt '<操作目标>'  <操作方法>

操作目标过滤方法:

(1)-E 正则表达式过滤

salt -E "db*" cmd.run "df -k"

(2)-L 列表指定访问目标

salt -L "db_01,db_02" cmd.run "free -m"

(3)-G 根据主机的grains信息进行过滤

salt -G "osrelease:6.5" cmd.run "python -V"
salt -G "osfullname:Centos" cmd.run "python -V"

 (4)-I  根据主机的pillar信息进行过滤

salt -I 'appname:website' test.ping

(5)-N 按照组来执行命令

编辑/etc/salt/master文件:

nodegroups:
#  group1: 'L@foo.domain.com,bar.domain.com,baz.domain.com and bl*.domain.com'
#  group2: 'G@os:Debian and foo.domain.com'
  all: 'L@db_02,db_01'
  gdb: 'L@db'
  gdb1: 'L@db01'
#L@表示后面的主机id格式为列表,即主机id以逗号分隔;G@表示以grain格式描述;S@表示以IP子网或地址格式描述

执行命令:salt -N "all" test.ping

这里尝试失败,原因暂时未能找到

(6)-C 根据not、and、or匹配

salt -C "E@db* and G@os:CentOs" test.ping
salt -C "* and not E@^adb1*" test.ping

(7)-S 根据被控主机的IP地址或IP子网进行匹配

salt -S 192.168.100.0/24 test.ping

原文地址:https://www.cnblogs.com/zxw-xxcsl/p/11381227.html