saltstack学习-2 :returnner-将结果发送给syslog和mysql(单边模式)
Posted snailshadow
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了saltstack学习-2 :returnner-将结果发送给syslog和mysql(单边模式)相关的知识,希望对你有一定的参考价值。
一,returnner介绍
1,默认情况下,发送给minion的命令执行结果将返回给salt-master。Saltstack Returnner的接口允许将结果发送给任意系统。
GitHub:https://github.com/saltstack/salt/tree/develop/salt/returners
官网:https://docs.saltstack.com/en/latest/ref/returners/
二,returnner模块列表
三,案例
1,returnner返回到syslog
salt-master执行命令:
[root@saltmaster ~]# salt \'*\' test.ping --return syslog dns02: True dns01: True
查看salt-minion端syslog日志
[root@dns02 ~]# tail -f /var/log/messages Dec 9 22:22:31 dns02 python2.6: {"fun_args": [], "jid": "20171209222231433150", "return": true, "retcode": 0, "success": true, "fun": "test.ping", "id": "dns02"}
2,returnner返回给mysql(单边模式)
- 安装mysql(可以在master也可以在其它服务器)
[root@saltmaster ~]# yum install –y mysql
[root@saltmaster ~]# yum install –y mysql-server
- 启动mysql服务
[root@saltmaster tmp]# service mysqld start Starting MySQL........ SUCCESS!
- 设置root密码
[root@saltmaster tmp]# mysqladmin -u root password \'123456\'
- 创建数据库,并授权
--创建数据库 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; 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,KEY `id` (`id`),KEY `jid` (`jid`),KEY `fun` (`fun`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; --授权 GRANT ALL PRIVILEGES ON salt.* to \'salt\'@\'%\' identified by \'salt\'; flush privileges;
- 修改所有minion配置文件,在配置文件结尾添加:
[root@dns02 ~]# vi /etc/salt/minion mysql.host: \'10.80.0.162\' mysql.user: \'salt\' mysql.pass: \'salt\' mysql.db: \'salt\' mysql.port: 3306
- 重启所有minion服务
[root@saltmaster ~]# salt \'*\' service.restart salt-minion dns01: True dns02: True
- 所有minion安装MySQL-python模块
[root@saltmaster tmp]# salt \'*\' cmd.run \'yum install -y MySQL-python\' dns02: Loaded plugins: fastestmirror, refresh-packagekit, security Setting up Install Process Loading mirror speeds from cached hostfile * epel: mirrors.tongji.edu.cn Resolving Dependencies --> Running transaction check ---> Package MySQL-python.x86_64 0:1.2.3-0.3.c1.1.el6 will be installed --> Finished Dependency Resolution Dependencies Resolved ================================================================================ Package Arch Version Repository Size ================================================================================ Installing: MySQL-python x86_64 1.2.3-0.3.c1.1.el6 base 86 k Transaction Summary ================================================================================ Install 1 Package(s) Total download size: 86 k Installed size: 246 k Downloading Packages: Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Installing : MySQL-python-1.2.3-0.3.c1.1.el6.x86_64 1/1 Verifying : MySQL-python-1.2.3-0.3.c1.1.el6.x86_64 1/1 Installed: MySQL-python.x86_64 0:1.2.3-0.3.c1.1.el6 Complete! dns01: Loaded plugins: fastestmirror, refresh-packagekit, security Setting up Install Process Loading mirror speeds from cached hostfile * epel: mirrors.tongji.edu.cn Resolving Dependencies --> Running transaction check ---> Package MySQL-python.x86_64 0:1.2.3-0.3.c1.1.el6 will be installed --> Finished Dependency Resolution Dependencies Resolved ================================================================================ Package Arch Version Repository Size ================================================================================ Installing: MySQL-python x86_64 1.2.3-0.3.c1.1.el6 base 86 k Transaction Summary ================================================================================ Install 1 Package(s) Total download size: 86 k Installed size: 246 k Downloading Packages: Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Installing : MySQL-python-1.2.3-0.3.c1.1.el6.x86_64 1/1 Verifying : MySQL-python-1.2.3-0.3.c1.1.el6.x86_64 1/1 Installed: MySQL-python.x86_64 0:1.2.3-0.3.c1.1.el6 Complete!
- 测试写入mysql
[root@saltmaster tmp]# salt \'*\' cmd.run \'hostname\' --return mysql dns02: dns02 dns01: dns01
- 结果
mysql> select * from salt_returns; +---------+----------------------+---------+-------+---------+----------------------------------------------------------------------------------------------------------------------------------------------+ | fun | jid | return | id | success | full_ret | +---------+----------------------+---------+-------+---------+----------------------------------------------------------------------------------------------------------------------------------------------+ | cmd.run | 20171210074613759042 | "dns02" | dns02 | 1 | {"fun_args": ["hostname"], "jid": "20171210074613759042", "return": "dns02", "retcode": 0, "success": true, "fun": "cmd.run", "id": "dns02"} | | cmd.run | 20171210074613759042 | "dns01" | dns01 | 1 | {"fun_args": ["hostname"], "jid": "20171210074613759042", "return": "dns01", "retcode": 0, "success": true, "fun": "cmd.run", "id": "dns01"} | +---------+----------------------+---------+-------+---------+----------------------------------------------------------------------------------------------------------------------------------------------+ 2 rows in set (0.00 sec)
以上是关于saltstack学习-2 :returnner-将结果发送给syslog和mysql(单边模式)的主要内容,如果未能解决你的问题,请参考以下文章