1.目标 
2.执行模块 
3.返回

salt    ‘*’    cmd.run    ‘uptime’
命令 目标 执行模块 执行模块参数

1、SlatStack远程执行–目标

执行目标:https://docs.saltstack.com/en/latest/topics/targeting/index.html#advanced-targeting-methods

  • (1)和Minion ID相关的目标匹配方式
、MinionID匹配
[root@linux-node1 ~]# salt 'linux-node1.example.com' service.status sshd
linux-node1.example.com:
True 、通配符* ? [-]等匹配
[root@linux-node1 ~]# salt 'linux*' service.status sshd
linux-node2.example.com:
True
linux-node1.example.com:
True
[root@linux-node1 ~]# salt 'linux-node?.example.com' service.status sshd
linux-node2.example.com:
True
linux-node1.example.com:
True
[root@linux-node1 ~]# salt 'linux-node[1-2].example.com' service.status sshd
linux-node2.example.com:
True
linux-node1.example.com:
True 、列表匹配
[root@linux-node1 ~]# salt -L 'linux-node1.example.com,linux-node2.example.com' test.ping
linux-node2.example.com:
True
linux-node1.example.com:
True 、正则表达式匹配
[root@linux-node1 ~]# salt -E 'linux-(node1|node2)*' test.ping
linux-node2.example.com:
True
linux-node1.example.com:
True
  • (2)和Minion无关匹配

    、Grains匹配
    [root@linux-node1 ~]# salt -G 'os:CentOS' test.ping
    linux-node2.example.com:
    True
    linux-node1.example.com:
    True 、子网、IP地址匹配
    [root@linux-node1 ~]# salt -S '192.168.56.0/24' test.ping
    linux-node1.example.com:
    True
    linux-node2.example.com:
    True 、Pillar匹配
    #这里目标key:value,是在pillar系统中定义
    [root@linux-node1 ~]# salt -I 'apache:httpd' test.ping
    linux-node2.example.com:
    True
    linux-node1.example.com:
    True
  • (3)混合匹配(少用)
  • (4)Node Groups匹配
    #在master配置文件进行定义node-groups
    [root@linux-node1 ~]# vim /etc/salt/master
    nodegroups:
    web-group: 'L@linux-node1.example.com,linux-node2.example.com'
    [root@linux-node1 ~]# systemctl restart salt-master
    [root@linux-node1 ~]# salt -N web-group test.ping
    linux-node2.example.com:
    True
    linux-node1.example.com:
    True
  • (5)批处理执行–Batch size
    #先执行1台完成后再执行一台,按比例去执行
    [root@linux-node1 ~]# salt '*' -b test.ping Executing run on ['linux-node2.example.com'] jid: linux-node2.example.com:
    True
    retcode: Executing run on ['linux-node1.example.com'] jid: linux-node1.example.com:
    True
    retcode: #按比例匹配执行,好比在重启服务器时,为了不影响业务,可以先重启一部分,再重启后面一部分
    [root@linux-node1 ~]# salt -G 'os:CentOS' --batch-size % test.ping Executing run on ['linux-node2.example.com'] jid: linux-node2.example.com:
    True
    retcode: Executing run on ['linux-node1.example.com'] jid: linux-node1.example.com:
    True
    retcode:

2、SlatStack远程执行–执行模块

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

3、SlatStack远程执行–返回

返回模块:https://docs.saltstack.com/en/latest/ref/returners/index.html 
Return组件可以理解为SaltStack系统对执行Minion返回后的数据进行存储或者返回给其他程序,它支持多种存储方式,如MySQL、Redis、ELK、zabbix,通过Return我们可以对SaltStack的每次操作进行记录,对以后的日志审计提供了数据来源。 
Return是在Master端触发任务,然后Minion接受处理任务直接与Return存储服务器建立链接,然后把数据存储到服务器。 
返回是minion直接将命令执行结果写入到MySQL,需要的依赖包:MySQL-python

  • (1)SATL.RETURNERS.MYSQL(minion返回MySQL)

    ()所有minion需要安装MySQL-python
    [root@linux-node1 ~]# salt '*' cmd.run 'yum install -y MySQL-python'
    [root@linux-node1 ~]# salt '*' pkg.install MySQL-python #使用pkg模块安装MySQL-python ()安装mariadb数据库
    [root@linux-node1 ~]# yum install -y mariadb-server
    [root@linux-node1 ~]# systemctl start mariadb ()创建salt库,创建jid、salt_returns、salt_events表,授权
    [root@linux-node1 ~]# mysql -uroot -p
    Enter password:
    MariaDB [(none)]> CREATE DATABASE `salt`
    -> DEFAULT CHARACTER SET utf8
    -> DEFAULT COLLATE utf8_general_ci;
    Query OK, row affected (0.00 sec) MariaDB [(none)]> USE `salt`;
    Database changed MariaDB [salt]> CREATE TABLE `jids` (
    -> `jid` varchar() NOT NULL,
    -> `load` mediumtext NOT NULL,
    -> UNIQUE KEY `jid` (`jid`)
    -> ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    Query OK, rows affected (0.00 sec) MariaDB [salt]> CREATE TABLE `salt_returns` (
    -> `fun` varchar() NOT NULL,
    -> `jid` varchar() NOT NULL,
    -> `return` mediumtext NOT NULL,
    -> `id` varchar() NOT NULL,
    -> `success` varchar() 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;
    Query OK, rows affected (0.03 sec) MariaDB [salt]> CREATE TABLE `salt_events` (
    -> `id` BIGINT NOT NULL AUTO_INCREMENT,
    -> `tag` varchar() NOT NULL,
    -> `data` mediumtext NOT NULL,
    -> `alter_time` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    -> `master_id` varchar() NOT NULL,
    -> PRIMARY KEY (`id`),
    -> KEY `tag` (`tag`)
    -> ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    Query OK, rows affected (0.02 sec) MariaDB [salt]> show tables;
    +----------------+
    | Tables_in_salt |
    +----------------+
    | jids |
    | salt_events |
    | salt_returns |
    +----------------+
    rows in set (0.00 sec) MariaDB [salt]> grant all on salt.* to salt@'%' identified by 'salt';
    Query OK, rows affected (0.00 sec) ()修改salt-minion,配置MySQL链接
    [root@linux-node2 ~]# vim /etc/salt/minion
    ###### Returner settings ######
    ############################################
    mysql.host: '192.168.56.11'
    mysql.user: 'salt'
    mysql.pass: 'salt'
    mysql.db: 'salt'
    mysql.port:
    [root@linux-node2 ~]# systemctl restart salt-minion
    [root@linux-node1 ~]# vim /etc/salt/minion
    ###### Returner settings ######
    ############################################
    mysql.host: '192.168.56.11'
    mysql.user: 'salt'
    mysql.pass: 'salt'
    mysql.db: 'salt'
    mysql.port:
    [root@linux-node1 ~]# systemctl restart salt-minion ()测试,并在数据库查看返回结果
    [root@linux-node1 ~]# salt '*' test.ping --return mysql
    linux-node2.example.com:
    True
    linux-node1.example.com:
    True
    MariaDB [salt]> select * from salt_returns;
    +-----------+----------------------+--------+-------------------------+---------+-----------------------------------------------------------------------------------------------------------------------------------------------------+---------------------+
    | fun | jid | return | id | success | full_ret | alter_time |
    +-----------+----------------------+--------+-------------------------+---------+-----------------------------------------------------------------------------------------------------------------------------------------------------+---------------------+
    | test.ping | | true | linux-node2.example.com | | {"fun_args": [], "jid": "", "return": true, "retcode": , "success": true, "fun": "test.ping", "id": "linux-node2.example.com"} | -- :: |
    | test.ping | | true | linux-node1.example.com | | {"fun_args": [], "jid": "", "return": true, "retcode": , "success": true, "fun": "test.ping", "id": "linux-node1.example.com"} | -- :: |
    +-----------+----------------------+--------+-------------------------+---------+-----------------------------------------------------------------------------------------------------------------------------------------------------+---------------------+
    rows in set (0.00 sec)
  • 使用salt的job_cache机制将命令写入mysql(常用方法)
  • 执行的所有命令都会写入mysql,不用使用return,把cache写在mysql
    [root@linux-node1 ~]# vim /etc/salt/master
    master_job_cache: mysql
    mysql.host: '192.168.56.11'
    mysql.user: 'salt'
    mysql.pass: 'salt'
    mysql.db: 'salt'
    mysql.port:
    [root@linux-node1 ~]# systemctl restart salt-master
    [root@linux-node1 ~]# salt '*' cmd.run 'w'
    [root@linux-node1 ~]# mysql -uroot -p123456 -e "select * from salt.salt_returns;" #加上-v参数可以看到jid,并且通过jid可以查看运行的结果
    [root@linux-node1 ~]# salt '*' cmd.run 'uptime' -v
    Executing job with jid
    ------------------------------------------- linux-node2.example.com:
    :: up days, :, users, load average: 0.00, 0.01, 0.05
    linux-node1.example.com:
    :: up days, :, users, load average: 0.00, 0.06, 0.18
    [root@linux-node1 ~]# salt-run jobs.lookup_jid
    linux-node1.example.com:
    :: up days, :, users, load average: 0.00, 0.06, 0.18
    linux-node2.example.com:
    :: up days, :, users, load average: 0.00, 0.01, 0.05

SaltStack入门篇(四)之深入理解SaltStack远程执行的更多相关文章

  1. SaltStack入门篇(一)之SaltStack部署

    一.SaltStack概述 Salt,,一种全新的基础设施管理方式,部署轻松,在几分钟内可运行起来,扩展性好,很容易管理上万台服务器,速度够快,服务器之间秒级通讯. salt底层采用动态的连接总线, ...

  2. 【SSRS】入门篇(四) -- 向报表添加数据

    原文:[SSRS]入门篇(四) -- 向报表添加数据 定义好数据集后 [SSRS]入门篇(三) -- 为报表定义数据集 ,就可以开始设计报表了,将要显示在报表的字段.文本框.图像和其他项从工具箱拖放到 ...

  3. FPGA基础入门篇(四) 边沿检测电路

    FPGA基础入门篇(四)--边沿检测电路 一.边沿检测 边沿检测,就是检测输入信号,或者FPGA内部逻辑信号的跳变,即上升沿或者下降沿的检测.在检测到所需要的边沿后产生一个高电平的脉冲.这在FPGA电 ...

  4. 001-SaltStack入门篇(一)之SaltStack部署

    早期运维工作中用过稍微复杂的Puppet,下面介绍下更为简单实用的Saltstack自动化运维的使用. Saltstack知多少Saltstack是一种全新的基础设施管理方式,是一个服务器基础架构集中 ...

  5. SaltStack入门篇(五)之salt-ssh的使用以及LAMP状态设计部署

    1.salt-ssh的使用 官方文档:https://docs.saltstack.com/en/2016.11/topics/ssh/index.html ()安装salt-ssh [root@li ...

  6. SaltStack入门篇(二)之远程执行和配置管理

    1.远程执行 第一条命令: [root@linux-node1 master]# salt '*' test.ping linux-node2.example.com: True linux-node ...

  7. SaltStack入门篇(三)之数据系统Grains、Pillar

    1.什么是Grains? Grains是saltstack的组件,用于收集salt-minion在启动时候的信息,又称为静态信息.可以理解为Grains记录着每台Minion的一些常用属性,比如CPU ...

  8. SaltStack入门篇(七)之架构部署实战

    模块:https://docs.saltstack.com/en/2016.11/ref/states/all/index.html 实战架构图: 实验环境设置: 主机名 IP地址 角色 linux- ...

  9. SaltStack入门篇(六)之部署Redis主从实现和Job管理

    一.部署Redis主从 需求: 192.168.56.11是主,192.168.56.12是从 redis监听自己的ip地址,而不是0.0.0.0 分析: linux-node1 安装 配置 启动 l ...

随机推荐

  1. office2013密钥

    GYWDG-NMV9P-746HR-Y2VQW-YPXKK6HDB9-BNRGY-J3F83-CF43C-D67TXG9N3P-GRJK6-VM63J-F9M27-KHGXKX2YWD-NWJ42-3 ...

  2. programming-languages学习笔记--第4部分

    programming-languages学习笔记–第4部分 */--> pre.src {background-color: #292b2e; color: #b2b2b2;} program ...

  3. [19/04/25-星期四] GOF23_结构型模式(适配器模式、代理模式)

    一.引言 结构模式:核心作用就是从程序的结构上实现松耦合,从而扩大整体的类结构,用来解决更大的问题. 二.适配器模式(adapter) 生活中假设笔记本是标准的USB接口但是外置键盘是圆形接口,这时候 ...

  4. Linux学习总结(十七)-shell 基础知识

    一 先介绍几种常用字符: 1 * 匹配任意个任意字符2 ?匹配一个任意字符3 # 注释符号,符号后的语句不被执行4 \脱意字符,后面跟带含义字符时,照原字符输出5 []匹配包含在[]之中的任意一个字符 ...

  5. virtualbox+vagrant学习-4-Vagrantfile-3-Minimum Vagrant Version

    Minimum Vagrant Version 可以在Vagrantfile中指定一组vagrant版本需求,以强制人们使用带有Vagrantfile文件的vagrant特定版本.这可以帮助解决使用带 ...

  6. Jmeter--thrift接口压测,调用jar包失败报错:java.lang.NoSuchMethodError:

    调用thrift接口压测的jar包,出现了错误:java.lang.NoSuchMethodError: 错误可能的原因: 有这个类,该类真的没有这个方法 有这个类,而且有好几个,他们之间发生了冲突 ...

  7. JNI由浅入深_9_JNI 异常处理

    1 .本地代码中如何缓存和抛出异常 下面的代码中演示了如何声明一个会抛出异常的本地方法.CatchThrow这个类声明了一个会抛出IllegalArgumentException异常的名叫doit的本 ...

  8. C#中参数传递

    当调用带有参数的方法,需要向方法传递参数,有三种向方法传递参数的方式. 1.值参数:这种方式复制参数的实际值给形式参数,形参和实参使用的是内存中两个不相同的值,形参发生改变不会影响实参的值,从而保证了 ...

  9. Spring boot Mybatis整合构建Rest服务(超细版)

     Springboot+ Mybatis+MySql整合构建Rest服务(涵盖增.删.改.查) 1.概要 1.1 为什么要使用Spring  boot? 1.1.1 简单方便.配置少.整合了大多数框架 ...

  10. vue中的slot(插槽)

    vue中的插槽----slot 什么是插槽? 插槽(Slot)是Vue提出来的一个概念,正如名字一样,插槽用于决定将所携带的内容,插入到指定的某个位置,从而使模板分块,具有模块化的特质和更大的重用性. ...