当我们使用Salt执行一条远程命令
salt ‘*‘ cmd.run "df -h"
2.1目标(Target)
1.通配符匹配方式
//*代表匹配所有主机
[[email protected] ~]# salt ‘*‘ test.ping [[email protected] ~]# salt ‘salt1-minion.example.com‘ test.ping [[email protected] ~]# salt ‘salt1*‘ test.ping [[email protected] ~]# salt ‘salt[1|2]*‘ test.ping [[email protected] ~]# salt ‘salt?-minion.example.com‘ test.ping [[email protected] ~]# salt ‘salt[!1|2]-minion.example.com‘ test.ping
2.列表匹配方式
[[email protected] ~]# salt -L ‘salt1-minion.example.com,salt2-minion.example.com‘ test.ping
3.正则表达式
[[email protected] ~]# salt -E ‘salt(1|2|3|4)*‘ test.ping [[email protected] ~]# salt -E ‘salt(1|2|3|4)-minion.example.com‘ test.ping
4.IP匹配方式
[[email protected] ~]# salt -S ‘192.168.70.0/24‘ test.ping [[email protected] ~]# salt -S ‘192.168.70.171‘ test.ping
5.分组匹配方式
[[email protected] ~]# vi /etc/salt/master nodegroups: webserver: ‘salt1-minion.example.com,salt2-minion.example.com‘ dbserver: ‘[email protected],salt2-minion.example.com or salt3*‘ ftpserver: ‘[email protected]:centos and salt1-minion.example.com‘ [[email protected] ~]# systemctl restart salt-master [[email protected] ~]# salt -N ‘webserver‘ test.ping
6.Grains匹配方式
[[email protected] ~]# salt -G ‘os:centos‘ test.ping [[email protected] ~]# salt -G ‘fqdn_ip4:192.168.70.174‘ test.ping
注意:
所有在远程执行中可以匹配到目标的方式,在TopFile定义时指定主机也可以使用该方法指定目标主机
主机名设计方案
1.IP地址 2.根据业务来进行设置www.xuliangwei.com Nginx-PHP-node1-lnmp01-hz-aliyun-www.xuliangwei.com Nginx-PHP-node1 代表第一个PHP架构节点 lnmp01 当前的集群环境 hz-aliyun 在杭州阿里云机房 www 业务线 xuliangwei.com 对应的域名
2.2模块(Modules)
模块:自带模块
1.安装 pkg
2.配置 file
3.启动 service
软件包模块
模块名:pkg
功能: 软件包状态,会根据操作系统不同,选择对应的安装方式(如CentOS系统默认会使用yum,Debian系统默认使用apt-get)
//安装 [[email protected] ~]# salt ‘*‘ pkg.install "httpd" //卸载 [[email protected] ~]# salt ‘*‘ pkg.remove "httpd" //安装最新版本 [[email protected] ~]# salt ‘*‘ pkg.latest_version httpd //查看模块帮助 salt ‘*‘ pkg
文件模块
//文件状态信息 [[email protected] ~]# salt ‘*‘ file.stats /etc/passwd //文件创建 [[email protected] ~]# salt ‘*‘ file.touch "/tmp/bak" //文件软链接 [[email protected] ~]# salt ‘*‘ file.symlink /tmp/123 /root/456 [[email protected] ~]# salt ‘*‘ file.rename /path/to/src /path/to/dst [[email protected] ~]# salt ‘*‘ file.chown /etc/passwd root root //查看模块帮助 salt ‘*‘ file
服务模块
salt ‘*‘ service.disabled <service name> salt ‘*‘ service.enable <service name> salt ‘*‘ service.enabled <service name> salt ‘*‘ service.missing sshd salt ‘*‘ service.reload <service name> salt ‘*‘ service.restart <service name> salt ‘*‘ service.start <service name> salt ‘*‘ service.status <service name> [service signature] salt ‘*‘ service.stop <service name> //查看模块帮助 salt ‘*‘ pkg
2.3返回(Return)
Return
组件可以理解为SaltStack
系统对执行Minion
返回后的数据存储或者返回给其他程序,支持多种存储方式,例如 MysqL、MongoDB 、Redis、Memcache
等。
通过Return
可以对SaltStack
每次的操作进行记录,对以后的日志审计提供了数据源。
1.配置一台数据库
[[email protected] ~]# yum install MysqL-python mariadb-server mariadb [[email protected]master ~]# systemctl start mariadb #建立远程登录账户 MariaDB [(none)]> grant all on salt.* to [email protected]‘%‘ identified by ‘[email protected]‘; #创建对应的库和表 CREATE DATABASE `salt` DEFAULT CHaraCTER SET utf8 DEFAULT COLLATE utf8_general_ci; USE `salt`; DROP TABLE IF EXISTS `jids`; CREATE TABLE `jids` ( `jid` varchar(255) NOT NULL,`load` mediumtext NOT NULL,UNIQUE KEY `jid` (`jid`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE INDEX jid ON jids(jid) USING BTREE; DROP TABLE IF EXISTS `salt_returns`; CREATE TABLE `salt_returns` ( `fun` varchar(50) NOT NULL,`jid` varchar(255) NOT NULL,`return` mediumtext NOT NULL,`id` varchar(255) NOT NULL,`success` varchar(10) NOT NULL,`full_ret` mediumtext NOT NULL,`alter_time` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,KEY `id` (`id`),KEY `jid` (`jid`),KEY `fun` (`fun`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; DROP TABLE IF EXISTS `salt_events`; CREATE TABLE `salt_events` ( `id` BIGINT NOT NULL AUTO_INCREMENT,`tag` varchar(255) NOT NULL,`data` mediumtext NOT NULL,`master_id` varchar(255) NOT NULL,PRIMARY KEY (`id`),KEY `tag` (`tag`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
2.Minion
端操作
[[email protected] ~]# yum install MysqL-python -y [[email protected] ~]# vim /etc/salt/minion #return: MysqL MysqL.host: ‘192.168.70.170‘ MysqL.user: ‘salt‘ MysqL.pass: ‘[email protected]‘ MysqL.db: ‘salt‘ MysqL.port: 3306 [[email protected] ~]# systemctl restart salt-minion
3.Master
端操作
[[email protected] ~]# salt ‘salt1*‘ test.ping --return MysqL
4.检查数据库是否有值
*************************** 2. row *************************** fun: test.ping jid: 20180601115142525730 return: true id: salt1-minion.example.com success: 1 full_ret: {"fun_args": [],"jid": "20180601115142525730","return": true,"retcode": 0,"success": true,"fun": "test.ping","id": "salt1-minion.example.com"} alter_time: 2018-06-01 11:51:42