Saltstack学习(三)-远程执行

一、saltsatck远程执行

当我们使用salt执行一条远程命令,如:salt '*' cmd.run "df -h",命令的结构是怎样的呢?

image

1.1、目标(target)

文档:https://docs.saltstack.com/en/latest/topics/tutorials/modules.html#target

1)通配符匹配方式

[root@master ~]# salt '*' test.ping
[root@master ~]# salt 'salt1-minion.example.com' test.ping 
[root@master ~]# salt 'salt1*' test.ping 
[root@master ~]# salt 'salt[1|2]*' test.ping 
[root@master ~]# salt 'salt?-minion.example.com' test.ping 
[root@master ~]# salt 'salt[!1|2]-minion.example.com' test.ping

2)列表匹配

[root@master ~]# salt -L 'salt-minion1-c7,salt-minion2-c7'test.ping

3)正则匹配

[root@salt0-master ~]# salt -E 'salt(1|2|3|4)*' test.ping 
[root@salt0-master ~]# salt -E 'salt(1|2|3|4)-minion.example.com' test.ping

4)ip匹配

[root@salt-master pillar]# salt -S '10.0.0.21' test.ping
[root@salt-master pillar]# salt -S '10.0.0.0/24' test.ping

5)分组匹配

[root@salt-master ~]# vim /etc/salt/master
nodegroups:
  webserver: 'salt-minion1-c7,salt-minion2-c7'
  dbserver: 'L@salt-minion3-c7,salt-minion2-c7 or salt-minion4*'
  ftpserver: 'G@os:centos and salt-minion1-c7'

[root@salt-master ~]# systemctl restart salt-master.service 
[root@salt-master ~]# salt -N 'webserver' test.ping

6)grains匹配

[root@salt-master ~]# salt -G 'os:centos' test.ping
[root@salt-master ~]# salt -G 'fqdn_ip4:10.0.0.21' test.ping

1.2、远程执行模块

1.2.1 pkg模块

文档:https://docs.saltstack.com/en/latest/ref/modules/all/salt.modules.yumpkg.html#module-salt.modules.yumpkg

根据操作系统不同,选择对应的安装方式(如CentOS系统默认会使用yum,Debian系统默认使用apt-get)

[root@salt-master ~]# salt '*' pkg.install httpd 
[root@salt-master ~]# salt '*' pkg.install httpd reinstall=True  #重装
[root@salt-master ~]# salt '*' pkg.remove httpd
[root@salt-master ~]# salt '*' pkg.latest_version httpd  #查看最新版本

#查看模块帮助 salt '*' pkg

猜你喜欢

转载自www.cnblogs.com/hujinzhong/p/11438638.html