(三)SaltStack远程执行进阶之目标、模块、返回

目录

环境

主机名IP地址备注
linux-node1.example.com10.0.0.77/16Master&Minion
linux-node2.example.com10.0.0.88/16Minion

/etc/hosts文件解析

# 两个节点都做解析
cat >>/etc/hosts<<EOF
10.0.0.77   linux-node1.example.com linux-node1
10.0.0.88   linux-node2.example.com linux-node2
EOF

操作的时候一定要注意空格的数量,格式正确

1 目标

官方文档

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

基于minion-id的匹配

[root@linux-node1 ~]# salt 'linux-node1.example.com' cmd.run "w"
linux-node1.example.com:
     23:53:18 up 1 day, 13:24,  1 user,  load average: 0.18, 0.04, 0.01
    USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
    root     pts/0    10.0.0.1         21:51    1.00s  0.42s  0.39s /usr/bin/python

基于通配符的匹配

[root@linux-node1 ~]# salt 'linux-node[1-2].example.com' cmd.run "w"
linux-node2.example.com:
     23:54:15 up 1 day, 11:25,  1 user,  load average: 0.00, 0.00, 0.00
    USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
    root     pts/1    10.0.0.1         21:51    2:02m  0.00s  0.00s -bash
linux-node1.example.com:
     23:54:15 up 1 day, 13:25,  1 user,  load average: 0.07, 0.03, 0.01
    USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
    root     pts/0    10.0.0.1         21:51    1.00s  0.40s  0.37s /usr/bin/python
[root@linux-node1 ~]# salt '*.example.*' cmd.run "w"
linux-node1.example.com:
     23:54:39 up 1 day, 13:26,  1 user,  load average: 0.05, 0.03, 0.00
    USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
    root     pts/0    10.0.0.1         21:51    0.00s  0.39s  0.35s /usr/bin/python
linux-node2.example.com:
     23:54:39 up 1 day, 11:25,  1 user,  load average: 0.00, 0.00, 0.00
    USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
    root     pts/1    10.0.0.1         21:51    2:02m  0.00s  0.00s -bash

基于正则表达式的匹配(-E)

[root@linux-node1 ~]# salt -E 'linux-node(1|2).*' cmd.run "w"
linux-node2.example.com:
     23:59:41 up 1 day, 11:30,  1 user,  load average: 0.00, 0.00, 0.00
    USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
    root     pts/1    10.0.0.1         21:51    2:07m  0.00s  0.00s -bash
linux-node1.example.com:
     23:59:41 up 1 day, 13:31,  1 user,  load average: 0.10, 0.03, 0.01
    USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
    root     pts/0    10.0.0.1         21:51    0.00s  0.40s  0.32s /usr/bin/python

基于List的匹配(-L)

[root@linux-node1 ~]# salt -L 'linux-node1.example.com,linux-node2.example.com' cmd.run "w"
linux-node2.example.com:
     23:58:11 up 1 day, 11:29,  1 user,  load average: 0.00, 0.00, 0.00
    USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
    root     pts/1    10.0.0.1         21:51    2:06m  0.00s  0.00s -bash
linux-node1.example.com:
     23:58:11 up 1 day, 13:29,  1 user,  load average: 0.08, 0.02, 0.01
    USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
    root     pts/0    10.0.0.1         21:51    0.00s  0.40s  0.35s /usr/bin/python

基于Grains的匹配(-G)

[root@linux-node1 ~]# salt -G 'os:CentOS' cmd.run "w"
linux-node2.example.com:
     00:03:19 up 1 day, 11:34,  1 user,  load average: 0.00, 0.00, 0.00
    USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
    root     pts/1    10.0.0.1         21:51    2:11m  0.00s  0.00s -bash
linux-node1.example.com:
     00:03:19 up 1 day, 13:34,  1 user,  load average: 0.00, 0.01, 0.00
    USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
    root     pts/0    10.0.0.1         21:51    0.00s  0.42s  0.34s /usr/bin/python

基于子网及IP地址的匹配(-S)

[root@linux-node1 ~]# salt -S 10.0.0.88 test.ping
linux-node2.example.com:
    True
[root@linux-node1 ~]# salt -S 10.0.0.0/16 test.ping
linux-node2.example.com:
    True
linux-node1.example.com:
    True

基于混合的匹配(-C)

基于IP地址和Grains的匹配
[root@linux-node1 ~]# salt -C 'S@10.0.0.88 or G@web:nginx' test.ping
linux-node2.example.com:
    True
linux-node1.example.com:
    True
[root@linux-node1 ~]# salt -C 'S@10.0.0.77 and G@web:nginx' test.ping
linux-node1.example.com:
    True
[root@linux-node1 ~]# salt -C 'S@10.0.0.88 not G@web:nginx' test.ping
linux-node2.example.com:
    True
LetterMatch TypeExample
GGrains globG@os:Ubuntu
EPCRE Minion IDE@web\d+\.(dev|qa|prod)\.loc
PGrains PCREP@os:(RedHat|Fedora|CentOS)
LList of minionsL@minion1.example.com,minion3.domain.com or b1*.domain.com
IPillar globI@pdata:foobar
JPillar PCREJ@pdata:^(foo|bar)$
SSubnet/IP addressS@192.168.1.0/24 or S@192.168.1.100
RRange clusterR@%foo.bar

基于group组的匹配(不推荐)

# 在/etc/salt/master配置文件中
vim /etc/salt/master
nodegroups:
  group1: 'G@os:debian and foo.domain.com'
  group2: 'G@os:Debian and N@group1'
  group3:
    - 'G@foo:bar'
    - 'or'
    - 'G@foo:baz'

# 重启master
/etc/init.d/salt-master restart

# 测试执行
salt -N group1 test.ping

2 模块

官方文档

https://docs.saltstack.com/en/latest/ref/modules/all/index.html#all-salt-modules

2.1 service模块

[root@linux-node1 ~]# salt '*' service.available sshd
linux-node2.example.com:
    True
linux-node1.example.com:
    True

get_all()方法,获取所有处于活动的服务

[root@linux-node1 ~]# salt 'linux-node1.example.com' service.get_all
linux-node1.example.com:
    - abrt-ccpp
    - abrt-oops
    - abrtd
    - acpid
    - atd
    - auditd
    - blk-availability
    - bmc-snmp-proxy
    - ck-log-system-restart
    - ck-log-system-start
    - ck-log-system-stop
    - control-alt-delete
    - cpuspeed
    - crond
    - exchange-bmc-os-info
    - haldaemon
    - halt
    - htcacheclean
    - httpd
    - init-system-dbus
    - ip6tables
    - ipmi
    - ipmievd
    - iptables
    - irqbalance
    - kdump
    - kexec-disable
    - killall
    - lvm2-lvmetad
    - lvm2-monitor
    - mdmonitor
    - messagebus
    - mysqld
    - netconsole
    - netfs
    - network
    - nfs-rdma
    - ntpd
    - ntpdate
    - plymouth-shutdown
    - postfix
    - prefdm
    - psacct
    - quit-plymouth
    - quota_nld
    - rc
    - rcS
    - rcS-emergency
    - rcS-sulogin
    - rdisc
    - rdma
    - readahead
    - readahead-collector
    - readahead-disable-services
    - redis
    - redis-sentinel
    - restorecond
    - rngd
    - rsyslog
    - salt-master
    - salt-minion
    - sandbox
    - saslauthd
    - serial
    - single
    - smartd
    - snmpd
    - snmptrapd
    - splash-manager
    - sshd
    - start-ttys
    - svnserve
    - sysstat
    - tty
    - udev-post
    - zabbix-agent
    - zabbix-server

missing方法,和available相反,处于活动的返回False

[root@linux-node1 ~]# salt '*' service.missing sshd
linux-node2.example.com:
    False
linux-node1.example.com:
    False

reload重载、status状态、stop停止、start启动

[root@linux-node1 ~]# salt '*' service.reload httpd
linux-node2.example.com:
    True
linux-node1.example.com:
    True
[root@linux-node1 ~]# salt '*' service.status httpd
linux-node2.example.com:
    True
linux-node1.example.com:
    True
[root@linux-node1 ~]# salt '*' service.stop httpd
linux-node2.example.com:
    True
linux-node1.example.com:
    True
[root@linux-node1 ~]# salt '*' service.status httpd
linux-node2.example.com:
    False
linux-node1.example.com:
    False
[root@linux-node1 ~]# salt '*' service.start httpd
linux-node2.example.com:
    True
linux-node1.example.com:
    True
[root@linux-node1 ~]# salt '*' service.status httpd
linux-node1.example.com:
    True
linux-node2.example.com:
    True

2.2 network模块

active_tcp方法:返回所有活动的TCP连接

[root@linux-node1 ~]# salt 'linux-node1.example.com' network.active_tcp
linux-node1.example.com:
    ----------
    0:
        ----------
        local_addr:
            0.0.0.0
        local_port:
            10050
        remote_addr:
            0.0.0.0
        remote_port:
            0
    1:
        ----------
        local_addr:
            0.0.0.0
        local_port:
            10051
        remote_addr:
            0.0.0.0
        remote_port:
            0
    10:
        ----------
        local_addr:
            10.0.0.77
        local_port:
            4505
        remote_addr:
            10.0.0.88
        remote_port:
            31465
    11:
        ----------
        local_addr:
            10.0.0.77
        local_port:
            22
        remote_addr:
            10.0.0.1
        remote_port:
            57846
    12:
        ----------
        local_addr:
            10.0.0.77
        local_port:
            4506
        remote_addr:
            10.0.0.77
        remote_port:
            47906
    13:
        ----------
        local_addr:
            10.0.0.77
        local_port:
            47906
        remote_addr:
            10.0.0.77
        remote_port:
            4506
    14:
        ----------
        local_addr:
            127.0.0.1
        local_port:
            24062
        remote_addr:
            127.0.0.1
        remote_port:
            4506
    2:
        ----------
        local_addr:
            0.0.0.0
        local_port:
            3306
        remote_addr:
            0.0.0.0
        remote_port:
            0
    3:
        ----------
        local_addr:
            127.0.0.1
        local_port:
            6379
        remote_addr:
            0.0.0.0
        remote_port:
            0
    4:
        ----------
        local_addr:
            0.0.0.0
        local_port:
            22
        remote_addr:
            0.0.0.0
        remote_port:
            0
    5:
        ----------
        local_addr:
            0.0.0.0
        local_port:
            4505
        remote_addr:
            0.0.0.0
        remote_port:
            0
    6:
        ----------
        local_addr:
            0.0.0.0
        local_port:
            4506
        remote_addr:
            0.0.0.0
        remote_port:
            0
    7:
        ----------
        local_addr:
            10.0.0.77
        local_port:
            58100
        remote_addr:
            10.0.0.77
        remote_port:
            4505
    8:
        ----------
        local_addr:
            10.0.0.77
        local_port:
            4506
        remote_addr:
            10.0.0.88
        remote_port:
            40763
    9:
        ----------
        local_addr:
            10.0.0.77
        local_port:
            4505
        remote_addr:
            10.0.0.77
        remote_port:
            58100

arp():返回arp信息

[root@linux-node1 ~]# salt 'linux-node1.example.com' network.arp
linux-node1.example.com:
    ----------
    00:0c:29:46:e3:26:
        10.0.0.88
    00:50:56:c0:00:08:
        10.0.0.1
    00:50:56:f0:ca:4c:
        10.0.0.2

connect():测试连接的方法

[root@linux-node1 ~]# salt 'linux-node1.example.com' network.connect baidu.com 80
linux-node1.example.com:
    ----------
    comment:
        Successfully connected to baidu.com (220.181.57.216) on tcp port 80
    result:
        True

dig():dig查询dns信息

[root@linux-node1 ~]# salt 'linux-node1.example.com' network.dig baidu.com
linux-node1.example.com:

    ; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.62.rc1.el6 <<>> baidu.com
    ;; global options: +cmd
    ;; Got answer:
    ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 6419
    ;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 0

    ;; QUESTION SECTION:
    ;baidu.com.                 IN      A

    ;; ANSWER SECTION:
    baidu.com.          5       IN      A       123.125.115.110
    baidu.com.          5       IN      A       220.181.57.216

    ;; Query time: 9 msec
    ;; SERVER: 10.0.0.2#53(10.0.0.2)
    ;; WHEN: Fri Jun 29 00:49:39 2018
    ;; MSG SIZE  rcvd: 59

interface():返回接口信息

[root@linux-node1 ~]# salt 'linux-node1.example.com' network.interface eth0
linux-node1.example.com:
    |_
      ----------
      address:
          10.0.0.77
      broadcast:
          10.0.0.255
      label:
          eth0
      netmask:
          255.255.255.0

2.3 模块的访问控制ACL

(1)master配置文件中开启acl

[root@linux-node1 ~]# vim /etc/salt/master
client_acl:
  rsq:
    - test.ping
    - network.*   

创建用户

[root@linux-node1 ~]# useradd rsq
[root@linux-node1 ~]# echo 123456 | passwd --stdin rsq
Changing password for user rsq.
passwd: all authentication tokens updated successfully.

改变salt权限,使普通用户能使用salt

[root@linux-node1 ~]# chmod 755 /var/cache/salt /var/cache/salt/master /var/cache/salt/master/jobs /var/run/salt /var/run/salt/master /var/log/salt/master
[root@linux-node1 ~]# chmod 777 /var/log/salt/master     # log日志给个写权限,否则会警告,但不会报错
[root@linux-node1 ~]# /etc/init.d/salt-master restart
Stopping salt-master daemon:                               [  OK  ]
Starting salt-master daemon:                               [  OK  ]

测试rsq用户权限,重启完后需要稍微等待一会再测试,client需要时间响应

[root@linux-node1 ~]# su - rsq
[rsq@linux-node1 ~]$ salt '*' cmd.run 'w'    # 操作拒绝
Failed to authenticate! This is most likely because this user is not permitted to execute commands, but there is a small possibility that a disk error occurred (check disk/inode usage).
[rsq@linux-node1 ~]$ salt '*' test.ping     # ok
linux-node2.example.com:
    True
linux-node1.example.com:
    True
[rsq@linux-node1 ~]$ salt '*' network.active_tcp    # ok
linux-node2.example.com:
    ----------
    0:
        ----------
        local_addr:
            0.0.0.0
        local_port:
            10050
        remote_addr:
            0.0.0.0
        remote_port:
            0
......

(2)设置某个用户只能对某台或某些minion进行操作

[root@linux-node1 ~]# vim /etc/salt/master   # 注意层级关系
client_acl:
  rsq:
    - test.ping
    - network.*
  rsq01:
    - linux-node1*:
      - test.ping
[root@linux-node1 ~]# /etc/init.d/salt-master restart
Stopping salt-master daemon:                               [  OK  ]
Starting salt-master daemon:                               [  OK  ]

创建用户并认证

[root@linux-node1 ~]# useradd rsq01
[root@linux-node1 ~]# echo 123456|passwd --stdin rsq01
Changing password for user rsq01.
passwd: all authentication tokens updated successfully.

测试rsq01用户是否对linux-node1*只具有test.ping权限

[rsq01@linux-node1 ~]$ salt 'linux-node1*' test.ping
linux-node1.example.com:
    True
[rsq01@linux-node1 ~]$ salt 'linux-node2*' test.ping
Failed to authenticate! This is most likely because this user is not permitted to execute commands, but there is a small possibility that a disk error occurred (check disk/inode usage).
[rsq01@linux-node1 ~]$ salt 'linux-node1*' cmd.run 'w'
Failed to authenticate! This is most likely because this user is not permitted to execute commands, but there is a small possibility that a disk error occurred (check disk/inode usage).

2.4 设置黑名单

禁止某些用户具有某些模块的权限

# 配置文件注释的黑名单配置
[root@linux-node1 ~]# vim /etc/salt/master
#client_acl_blacklist:
#  users:
#    - root
#    - '^(?!sudo_).*$'   #  all non sudo users
#  modules:
#    - cmd

修改为如下,所有用户都不能使用cmd模块

[root@linux-node1 ~]# vim /etc/salt/master     
client_acl_blacklist:
  modules:
    - cmd
[root@linux-node1 ~]# /etc/init.d/salt-master restart
Stopping salt-master daemon:                               [  OK  ]
Starting salt-master daemon:                               [  OK  ]
[root@linux-node1 ~]# salt '*' cmd.run 'w'
Salt request timed out. The master is not responding. If this error persists after verifying the master is up, worker_threads may need to be increased.
[root@linux-node1 ~]# salt '*' test.ping
linux-node2.example.com:
    True
linux-node1.example.com:
    True

3 返回

官方文档

https://docs.saltstack.com/en/latest/ref/returners/all/index.html

3.1 把return导入数据库(这里有两种方法)

return-mysql官方文档

(1)因为返回结果是minion返回的,故所有节点都要安装python依赖库

yum install MySQL-python -y

创建表结构

CREATE DATABASE  `salt`
  DEFAULT CHARACTER SET utf8
  DEFAULT COLLATE utf8_general_ci;

USE `salt`;

--
-- Table structure for table `jids`
--

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;

--
-- Table structure for table `salt_returns`
--

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;

--
-- Table structure for table `salt_events`
--

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;

最后是创建了三个表

mysql> show tables;
+----------------+
| Tables_in_salt |
+----------------+
| jids           |
| salt_events    |
| salt_returns   |
+----------------+
3 rows in set (0.00 sec)

授权

mysql> grant all on salt.* to salt@'10.0.0.0/255.255.255.0' identified by 'salt';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

所有节点,包括master和minion端的配置文件中都要做以下解析

vim /etc/salt/master
mysql.host: '10.0.0.77'
mysql.user: 'salt'
mysql.pass: 'salt'
mysql.db: 'salt'
mysql.port: 3306

vim /etc/salt/minion
mysql.host: '10.0.0.77'
mysql.user: 'salt'
mysql.pass: 'salt'
mysql.db: 'salt'
mysql.port: 3306

重启master和minion

/etc/init.d/salt-master restart
/etc/init.d/salt-minion restart

未写入数据前测试

mysql> select * from salt_returns\G
Empty set (0.00 sec)

写入数据测试

[root@linux-node1 ~]# salt '*' cmd.run "uptime" --return mysql
linux-node2.example.com:
     11:04:32 up 1 day, 13:52,  1 user,  load average: 0.00, 0.00, 0.00
linux-node1.example.com:
     11:04:32 up 1 day, 15:52,  2 users,  load average: 0.28, 0.15, 0.06
mysql> select * from salt_returns\G
*************************** 1. row ***************************
       fun: cmd.run
       jid: 20180629110432266968
    return: " 11:04:32 up 1 day, 13:52,  1 user,  load average: 0.00, 0.00, 0.00"
        id: linux-node2.example.com
   success: 1
  full_ret: {"fun_args": ["uptime"], "jid": "20180629110432266968", "return": " 11:04:32 up 1 day, 13:52,  1 user,  load average: 0.00, 0.00, 0.00", "retcode": 0, "success": true, "fun": "cmd.run", "id": "linux-node2.example.com"}
alter_time: 2018-06-29 11:04:32
*************************** 2. row ***************************
       fun: cmd.run
       jid: 20180629110432266968
    return: " 11:04:32 up 1 day, 15:52,  2 users,  load average: 0.28, 0.15, 0.06"
        id: linux-node1.example.com
   success: 1
  full_ret: {"fun_args": ["uptime"], "jid": "20180629110432266968", "return": " 11:04:32 up 1 day, 15:52,  2 users,  load average: 0.28, 0.15, 0.06", "retcode": 0, "success": true, "fun": "cmd.run", "id": "linux-node1.example.com"}
alter_time: 2018-06-29 11:04:32
2 rows in set (0.01 sec)

在master的配置文件中取消return: mysql的注释,这样就不用每次都要加–return参数

[root@linux-node1 ~]# vim /etc/salt/master
return: mysql

[root@linux-node1 ~]# /etc/init.d/salt-master restart
Stopping salt-master daemon:                               [  OK  ]
Starting salt-master daemon:                               [  OK  ]

mysql> select * from salt_returns\G
*************************** 1. row ***************************
       fun: cmd.run
       jid: 20180629110432266968
    return: " 11:04:32 up 1 day, 13:52,  1 user,  load average: 0.00, 0.00, 0.00"
        id: linux-node2.example.com
   success: 1
  full_ret: {"fun_args": ["uptime"], "jid": "20180629110432266968", "return": " 11:04:32 up 1 day, 13:52,  1 user,  load average: 0.00, 0.00, 0.00", "retcode": 0, "success": true, "fun": "cmd.run", "id": "linux-node2.example.com"}
alter_time: 2018-06-29 11:04:32
*************************** 2. row ***************************
       fun: cmd.run
       jid: 20180629110432266968
    return: " 11:04:32 up 1 day, 15:52,  2 users,  load average: 0.28, 0.15, 0.06"
        id: linux-node1.example.com
   success: 1
  full_ret: {"fun_args": ["uptime"], "jid": "20180629110432266968", "return": " 11:04:32 up 1 day, 15:52,  2 users,  load average: 0.28, 0.15, 0.06", "retcode": 0, "success": true, "fun": "cmd.run", "id": "linux-node1.example.com"}
alter_time: 2018-06-29 11:04:32
*************************** 3. row ***************************
       fun: cmd.run
       jid: 20180629111034997732
    return: "Filesystem      Size  Used Avail Use% Mounted on\n/dev/sda3       3.7G  1.9G  1.7G  54% /\ntmpfs           238M   16K  238M   1% /dev/shm\n/dev/sda1       190M   35M  146M  19% /boot"
        id: linux-node1.example.com
   success: 1
  full_ret: {"fun_args": ["df -h"], "jid": "20180629111034997732", "return": "Filesystem      Size  Used Avail Use% Mounted on\n/dev/sda3       3.7G  1.9G  1.7G  54% /\ntmpfs           238M   16K  238M   1% /dev/shm\n/dev/sda1       190M   35M  146M  19% /boot", "retcode": 0, "success": true, "fun": "cmd.run", "id": "linux-node1.example.com"}
alter_time: 2018-06-29 11:10:35
*************************** 4. row ***************************
       fun: cmd.run
       jid: 20180629111034997732
    return: "Filesystem      Size  Used Avail Use% Mounted on\n/dev/sda3       3.7G  1.7G  1.9G  47% /\ntmpfs           238M   12K  238M   1% /dev/shm\n/dev/sda1       190M   35M  146M  19% /boot"
        id: linux-node2.example.com
   success: 1
  full_ret: {"fun_args": ["df -h"], "jid": "20180629111034997732", "return": "Filesystem      Size  Used Avail Use% Mounted on\n/dev/sda3       3.7G  1.7G  1.9G  47% /\ntmpfs           238M   12K  238M   1% /dev/shm\n/dev/sda1       190M   35M  146M  19% /boot", "retcode": 0, "success": true, "fun": "cmd.run", "id": "linux-node2.example.com"}
alter_time: 2018-06-29 11:10:35
4 rows in set (0.00 sec)

(2)minion端不装MySQL-python包也能使数据写入到数据库

此方法利用的是返回后的本地master cache写入的数据,把/etc/salt/master中的return: mysql再重新注释,然后加入以下解析

[root@linux-node1 ~]# vim /etc/salt/master
master_job_cache: mysql
[root@linux-node1 ~]# /etc/init.d/salt-master restart
Stopping salt-master daemon:                               [  OK  ]
Starting salt-master daemon:                               [  OK  ]

测试返回数据写入mysql

[root@linux-node1 ~]# salt '*' cmd.run "free -m"  
mysql> select * from salt_returns\G

......

*************************** 5. row ***************************
       fun: cmd.run
       jid: 20180629111809991942
    return: "             total       used       free     shared    buffers     cached\nMem:           474        381         92          0         24       220\n-/+ buffers/cache:        136        338\nSwap:         1023          1       1022"
        id: linux-node2.example.com
   success: 1
  full_ret: {"fun_args": ["free -m"], "jid": "20180629111809991942", "return": "             total       used       free     shared    buffers     cached\nMem:           474        381         92          0         24        220\n-/+ buffers/cache:        136        338\nSwap:         1023          1       1022", "retcode": 0, "success": true, "cmd": "_return", "_stamp": "2018-06-29T03:18:10.134229", "fun": "cmd.run", "id": "linux-node2.example.com"}
alter_time: 2018-06-29 11:18:10
*************************** 6. row ***************************
       fun: cmd.run
       jid: 20180629111809991942
    return: "             total       used       free     shared    buffers     cached\nMem:           474        469          5          6         26       140\n-/+ buffers/cache:        302        171\nSwap:         1023         29        994"
        id: linux-node1.example.com
   success: 1
  full_ret: {"fun_args": ["free -m"], "jid": "20180629111809991942", "return": "             total       used       free     shared    buffers     cached\nMem:           474        469          5          6         26        140\n-/+ buffers/cache:        302        171\nSwap:         1023         29        994", "retcode": 0, "success": true, "cmd": "_return", "_stamp": "2018-06-29T03:18:10.156867", "fun": "cmd.run", "id": "linux-node1.example.com"}
alter_time: 2018-06-29 11:18:10
6 rows in set (0.00 sec)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

RSQ博客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值