首页 > 其他 > 详细

Saltstack学习(三)-远程执行

时间:2019-08-31 14:48:52      阅读:101      评论:0      收藏:0      [点我收藏+]

一、saltsatck远程执行

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

技术分享图片

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、远程执行模块

所有的执行模块文档:https://docs.saltstack.com/en/latest/ref/modules/all/index.html#all-salt-modules

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

1.2.2 file模块

文档:https://docs.saltstack.com/en/latest/ref/modules/all/salt.modules.file.html

[root@salt-master ~]# salt ‘*‘ file.stats /etc/passwd  #查看文件状态
[root@salt-master ~]# salt ‘*‘ file.touch "/tmp/test"  #创建文件
[root@salt-master ~]# salt ‘*‘ file.symlink /tmp/test /tmp/a #创建文件软件软连接,a==>test
[root@salt-master ~]# salt ‘*‘ file.rename /path/to/src /path/to/dst   #文件改名
[root@salt-master ~]# salt ‘*‘ file.chown /etc/passwd root root   #授权
[root@salt-master ~]# salt ‘*‘ file.copy /path/to/src /path/to/dst
[root@salt-master ~]# salt ‘*‘ file.mkdir /opt/jetty/context   #创建目录
[root@salt-master ~]# salt ‘*‘ file.move /path/to/src /path/to/dst  #移动文件或目录
  
//查看模块帮助 salt ‘*‘ file

1.2.3 service模块

文档:https://docs.saltstack.com/en/latest/ref/modules/all/salt.modules.rh_service.html#modulesalt.modules.rh_service

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>

二、返回(Return)

2.1、简介

Return组件可以理解为SaltStack系统对执行Minion返回后的数据存储或者返回给其他程序,支持多种存储方式,例如 MySQL、MongoDB 、Redis、Memcache等。通过Return可以对SaltStack每次的操作进行记录,对以后的日志审计提供了数据源。

技术分享图片

2.2、return的配置

文档:https://docs.saltstack.com/en/latest/ref/returners/all/salt.returners.mysql.html

1)配置mariadb数据库

#安装软件包并启动
[root@salt-master ~]# yum install mariadb-server mariadb MySQL-python -y   #MySQL-python不可少
[root@salt-master ~]# systemctl enable mariadb
[root@salt-master ~]# systemctl start mariadb

#建立远程连接用户
MariaDB [(none)]> grant all on salt.* to salt@‘%‘ identified by ‘salt@Pass‘;

#创库创表
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,
`alter_time` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
`master_id` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
KEY `tag` (`tag`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

2)minion端操作

[root@salt-minion1-c7 ~]#  yum install MySQL-python -y
[root@salt-minion1-c7 ~]# vim /etc/salt/minion
#return: mysql
mysql.host: ‘10.0.0.11‘
mysql.user: ‘salt‘
mysql.pass: ‘salt@Pass‘
mysql.db: ‘salt‘
mysql.port: 3306
[root@salt-minion1-c7 ~]# systemctl restart salt-minion.service

3)master端测试

[root@salt-master ~]# salt ‘salt-minion1-c7‘ test.ping --return mysql

#登录mysql查看
MariaDB [salt]> select * from salt_returns\G
*************************** 1. row ***************************
       fun: test.ping
       jid: 20190831143343957298
    return: true
        id: salt-minion1-c7
   success: 1
  full_ret: {"fun_args": [], "jid": "20190831143343957298", "return": true, "retcode": 0, "success": true, "fun": "test.ping", "id": "salt-minion1-c7"}
alter_time: 2019-08-31 14:33:44
1 row in set (0.00 sec)


Saltstack学习(三)-远程执行

原文:https://www.cnblogs.com/hujinzhong/p/11438638.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!