学习笔记0722----自动化运维saltstack

1.自动化运维工具

Puppet (www.puppetlabs.com)基于rubby开发,c/s架构,支持多平台,可管理配置文件、用户、cron任务、软件包、系统服务等。 分为社区版(免费)和企业版(收费),企业版支持图形化配置。
Saltstack(官网 https://saltstack.com,文档 docs.saltstack.com )基于python开发,c/s架构,支持多平台,比puppet轻量,在远程执行命令时非常快捷,配置和使用比puppet容易,能实现puppet几乎所有的功能。
Ansible (www.ansible.com )更加简洁的自动化运维工具,不需要在客户端上安装agent,基于python开发。可以实现批量操作系统配置、批量程序的部署、批量运行命令。

2.salt安装

192.168.174.128作为主控端,需要安装salt-master salt-minion;
192.168.174.129作为被控端,需要安装salt-minion;
192.168.174.139作为被控端,需要安装salt-minion。
参考安装salt源:https://repo.saltproject.io/#rhel

[root@linux-001 ~]# yum install -y salt-master salt-minion
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
epel/x86_64/metalink                                                                                        | 8.3 kB  00:00:00
 * base: mirrors.cn99.com
 * epel: mirrors.aliyun.com
 * extras: mirrors.163.com
 * updates: mirrors.163.com
base                                                                                                        | 3.6 kB  00:00:00
epel                                                                                                        | 5.4 kB  00:00:00
extras                                                                                                      | 3.4 kB  00:00:00
mongodb-org-4.0                                                                                             | 2.5 kB  00:00:00
updates                                                                                                     | 3.4 kB  00:00:00
zabbix                                                                                                      | 2.9 kB  00:00:00
zabbix-non-supported                                                                                        |  951 B  00:00:00
(1/2): epel/x86_64/updateinfo                                                                               | 993 kB  00:00:05
(2/2): epel/x86_64/primary_db                                                                               | 6.8 MB  00:00:07
正在解决依赖关系
--> 正在检查事务
---> 软件包 salt-master.noarch.0.2015.5.10-2.el7 将被 安装
--> 正在处理依赖关系 salt = 2015.5.10-2.el7,它被软件包 salt-master-2015.5.10-2.el7.noarch 需要
--> 正在处理依赖关系 systemd-python,它被软件包 salt-master-2015.5.10-2.el7.noarch 需要
---> 软件包 salt-minion.noarch.0.2015.5.10-2.el7 将被 安装
--> 正在检查事务
---> 软件包 salt.noarch.0.2015.5.10-2.el7 将被 安装
--> 正在处理依赖关系 PyYAML,它被软件包 salt-2015.5.10-2.el7.noarch 需要
--> 正在处理依赖关系 m2crypto,它被软件包 salt-2015.5.10-2.el7.noarch 需要
--> 正在处理依赖关系 python-crypto,它被软件包 salt-2015.5.10-2.el7.noarch 需要
--> 正在处理依赖关系 python-jinja2,它被软件包 salt-2015.5.10-2.el7.noarch 需要
--> 正在处理依赖关系 python-msgpack,它被软件包 salt-2015.5.10-2.el7.noarch 需要
--> 正在处理依赖关系 python-requests,它被软件包 salt-2015.5.10-2.el7.noarch 需要
--> 正在处理依赖关系 python-zmq,它被软件包 salt-2015.5.10-2.el7.noarch 需要
---> 软件包 systemd-python.x86_64.0.219-62.el7_6.7 将被 安装
--> 正在处理依赖关系 systemd-libs = 219-62.el7_6.7,它被软件包 systemd-python-219-62.el7_6.7.x86_64 需要
--> 正在处理依赖关系 systemd = 219-62.el7_6.7,它被软件包 systemd-python-219-62.el7_6.7.x86_64 需要
--> 正在检查事务
---> 软件包 PyYAML.x86_64.0.3.10-11.el7 将被 安装
--> 正在处理依赖关系 libyaml-0.so.2()(64bit),它被软件包 PyYAML-3.10-11.el7.x86_64 需要
---> 软件包 m2crypto.x86_64.0.0.21.1-17.el7 将被 安装
---> 软件包 python-jinja2.noarch.0.2.7.2-3.el7_6 将被 安装
--> 正在处理依赖关系 python-babel >= 0.8,它被软件包 python-jinja2-2.7.2-3.el7_6.noarch 需要
--> 正在处理依赖关系 python-markupsafe,它被软件包 python-jinja2-2.7.2-3.el7_6.noarch 需要
---> 软件包 python-requests.noarch.0.2.6.0-1.el7_1 将被 安装
--> 正在处理依赖关系 python-urllib3 >= 1.10.2-1,它被软件包 python-requests-2.6.0-1.el7_1.noarch 需要
---> 软件包 python2-crypto.x86_64.0.2.6.1-16.el7 将被 安装
--> 正在处理依赖关系 libtomcrypt.so.0()(64bit),它被软件包 python2-crypto-2.6.1-16.el7.x86_64 需要
---> 软件包 python2-msgpack.x86_64.0.0.5.6-5.el7 将被 安装
---> 软件包 python2-zmq.x86_64.0.14.7.0-8.el7 将被 安装
--> 正在处理依赖关系 libzmq.so.5()(64bit),它被软件包 python2-zmq-14.7.0-8.el7.x86_64 需要
---> 软件包 systemd.x86_64.0.219-62.el7 将被 升级
--> 正在处理依赖关系 systemd = 219-62.el7,它被软件包 systemd-sysv-219-62.el7.x86_64 需要
---> 软件包 systemd.x86_64.0.219-62.el7_6.7 将被 更新
---> 软件包 systemd-libs.x86_64.0.219-62.el7 将被 升级
---> 软件包 systemd-libs.x86_64.0.219-62.el7_6.7 将被 更新
--> 正在检查事务
---> 软件包 libtomcrypt.x86_64.0.1.17-26.el7 将被 安装
--> 正在处理依赖关系 libtommath >= 0.42.0,它被软件包 libtomcrypt-1.17-26.el7.x86_64 需要
--> 正在处理依赖关系 libtommath.so.0()(64bit),它被软件包 libtomcrypt-1.17-26.el7.x86_64 需要
---> 软件包 libyaml.x86_64.0.0.1.4-11.el7_0 将被 安装
---> 软件包 python-babel.noarch.0.0.9.6-8.el7 将被 安装
---> 软件包 python-markupsafe.x86_64.0.0.11-10.el7 将被 安装
---> 软件包 python-urllib3.noarch.0.1.10.2-5.el7 将被 安装
--> 正在处理依赖关系 python-six,它被软件包 python-urllib3-1.10.2-5.el7.noarch 需要
--> 正在处理依赖关系 python-ipaddress,它被软件包 python-urllib3-1.10.2-5.el7.noarch 需要
--> 正在处理依赖关系 python-backports-ssl_match_hostname,它被软件包 python-urllib3-1.10.2-5.el7.noarch 需要
---> 软件包 systemd-sysv.x86_64.0.219-62.el7 将被 升级
---> 软件包 systemd-sysv.x86_64.0.219-62.el7_6.7 将被 更新
---> 软件包 zeromq.x86_64.0.4.1.4-6.el7 将被 安装
--> 正在处理依赖关系 libpgm-5.2.so.0()(64bit),它被软件包 zeromq-4.1.4-6.el7.x86_64 需要
--> 正在处理依赖关系 libsodium.so.23()(64bit),它被软件包 zeromq-4.1.4-6.el7.x86_64 需要
--> 正在检查事务
---> 软件包 libsodium.x86_64.0.1.0.18-1.el7 将被 安装
---> 软件包 libtommath.x86_64.0.0.42.0-6.el7 将被 安装
---> 软件包 openpgm.x86_64.0.5.2.122-2.el7 将被 安装
---> 软件包 python-backports-ssl_match_hostname.noarch.0.3.5.0.1-1.el7 将被 安装
--> 正在处理依赖关系 python-backports,它被软件包 python-backports-ssl_match_hostname-3.5.0.1-1.el7.noarch 需要
---> 软件包 python-ipaddress.noarch.0.1.0.16-2.el7 将被 安装
---> 软件包 python-six.noarch.0.1.9.0-2.el7 将被 安装
--> 正在检查事务
---> 软件包 python-backports.x86_64.0.1.0-8.el7 将被 安装
--> 解决依赖关系完成

依赖关系解决

===================================================================================================================================
 Package                                           架构                 版本                           源                     大小
===================================================================================================================================
正在安装:
 salt-master                                       noarch               2015.5.10-2.el7                epel                  1.0 M
 salt-minion                                       noarch               2015.5.10-2.el7                epel                   26 k
为依赖而安装:
 PyYAML                                            x86_64               3.10-11.el7                    base                  153 k
 libsodium                                         x86_64               1.0.18-1.el7                   epel                  147 k
 libtomcrypt                                       x86_64               1.17-26.el7                    extras                224 k
 libtommath                                        x86_64               0.42.0-6.el7                   extras                 36 k
 libyaml                                           x86_64               0.1.4-11.el7_0                 base                   55 k
 m2crypto                                          x86_64               0.21.1-17.el7                  base                  429 k
 openpgm                                           x86_64               5.2.122-2.el7                  epel                  171 k
 python-babel                                      noarch               0.9.6-8.el7                    base                  1.4 M
 python-backports                                  x86_64               1.0-8.el7                      base                  5.8 k
 python-backports-ssl_match_hostname               noarch               3.5.0.1-1.el7                  base                   13 k
 python-ipaddress                                  noarch               1.0.16-2.el7                   base                   34 k
 python-jinja2                                     noarch               2.7.2-3.el7_6                  updates               518 k
 python-markupsafe                                 x86_64               0.11-10.el7                    base                   25 k
 python-requests                                   noarch               2.6.0-1.el7_1                  base                   94 k
 python-six                                        noarch               1.9.0-2.el7                    base                   29 k
 python-urllib3                                    noarch               1.10.2-5.el7                   base                  102 k
 python2-crypto                                    x86_64               2.6.1-16.el7                   epel                  477 k
 python2-msgpack                                   x86_64               0.5.6-5.el7                    epel                   64 k
 python2-zmq                                       x86_64               14.7.0-8.el7                   epel                  505 k
 salt                                              noarch               2015.5.10-2.el7                epel                  4.1 M
 systemd-python                                    x86_64               219-62.el7_6.7                 updates               133 k
 zeromq                                            x86_64               4.1.4-6.el7                    epel                  556 k
为依赖而更新:
 systemd                                           x86_64               219-62.el7_6.7                 updates               5.1 M
 systemd-libs                                      x86_64               219-62.el7_6.7                 updates               407 k
 systemd-sysv                                      x86_64               219-62.el7_6.7                 updates                84 k

事务概要
===================================================================================================================================
安装  2 软件包 (+22 依赖软件包)
升级           (  3 依赖软件包)

总下载量:16 M
Downloading packages:
updates/7/x86_64/prestodelta                                                                                | 829 kB  00:00:08
Delta RPMs reduced 5.5 M of updates to 2.0 M (63% saved)
(1/27): systemd-219-62.el7_219-62.el7_6.7.x86_64.drpm                                                       | 1.9 MB  00:00:05
(2/27): libsodium-1.0.18-1.el7.x86_64.rpm                                                                   | 147 kB  00:00:05
(3/27): systemd-libs-219-62.el7_219-62.el7_6.7.x86_64.drpm                                                  | 144 kB  00:00:06
(4/27): libtomcrypt-1.17-26.el7.x86_64.rpm                                                                  | 224 kB  00:00:05
(5/27): PyYAML-3.10-11.el7.x86_64.rpm                                                                       | 153 kB  00:00:06
(6/27): openpgm-5.2.122-2.el7.x86_64.rpm                                                                    | 171 kB  00:00:00
(7/27): python-babel-0.9.6-8.el7.noarch.rpm                                                                 | 1.4 MB  00:00:00
(8/27): python-backports-ssl_match_hostname-3.5.0.1-1.el7.noarch.rpm                                        |  13 kB  00:00:00
(9/27): python-ipaddress-1.0.16-2.el7.noarch.rpm                                                            |  34 kB  00:00:00
(10/27): python-jinja2-2.7.2-3.el7_6.noarch.rpm                                                             | 518 kB  00:00:00
(11/27): python-markupsafe-0.11-10.el7.x86_64.rpm                                                           |  25 kB  00:00:00
(12/27): python-requests-2.6.0-1.el7_1.noarch.rpm                                                           |  94 kB  00:00:00
(13/27): python-six-1.9.0-2.el7.noarch.rpm                                                                  |  29 kB  00:00:00
(14/27): python-urllib3-1.10.2-5.el7.noarch.rpm                                                             | 102 kB  00:00:00
(15/27): python2-crypto-2.6.1-16.el7.x86_64.rpm                                                             | 477 kB  00:00:00
(16/27): python2-msgpack-0.5.6-5.el7.x86_64.rpm                                                             |  64 kB  00:00:00
(17/27): python2-zmq-14.7.0-8.el7.x86_64.rpm                                                                | 505 kB  00:00:00
(18/27): salt-2015.5.10-2.el7.noarch.rpm                                                                    | 4.1 MB  00:00:00
(19/27): salt-master-2015.5.10-2.el7.noarch.rpm                                                             | 1.0 MB  00:00:00
(20/27): salt-minion-2015.5.10-2.el7.noarch.rpm                                                             |  26 kB  00:00:00
(21/27): systemd-python-219-62.el7_6.7.x86_64.rpm                                                           | 133 kB  00:00:00
(22/27): libtommath-0.42.0-6.el7.x86_64.rpm                                                                 |  36 kB  00:00:05
(23/27): zeromq-4.1.4-6.el7.x86_64.rpm                                                                      | 556 kB  00:00:00
(24/27): libyaml-0.1.4-11.el7_0.x86_64.rpm                                                                  |  55 kB  00:00:05
(25/27): systemd-sysv-219-62.el7_6.7.x86_64.rpm                                                             |  84 kB  00:00:03
(26/27): python-backports-1.0-8.el7.x86_64.rpm                                                              | 5.8 kB  00:00:05
(27/27): m2crypto-0.21.1-17.el7.x86_64.rpm                                                                  | 429 kB  00:00:06
Finishing delta rebuilds of 1 package(s) (5.1 M)
-----------------------------------------------------------------------------------------------------------------------------------
总计                                                                                               660 kB/s |  12 MB  00:00:18
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安装    : python-ipaddress-1.0.16-2.el7.noarch                                                                          1/30
  正在更新    : systemd-libs-219-62.el7_6.7.x86_64                                                                            2/30
  正在更新    : systemd-219-62.el7_6.7.x86_64                                                                                 3/30
  正在安装    : systemd-python-219-62.el7_6.7.x86_64                                                                          4/30
  正在安装    : libyaml-0.1.4-11.el7_0.x86_64                                                                                 5/30
  正在安装    : PyYAML-3.10-11.el7.x86_64                                                                                     6/30
  正在安装    : python-backports-1.0-8.el7.x86_64                                                                             7/30
  正在安装    : python-backports-ssl_match_hostname-3.5.0.1-1.el7.noarch                                                      8/30
  正在安装    : libtommath-0.42.0-6.el7.x86_64                                                                                9/30
  正在安装    : libtomcrypt-1.17-26.el7.x86_64                                                                               10/30
  正在安装    : python2-crypto-2.6.1-16.el7.x86_64                                                                           11/30
  正在安装    : libsodium-1.0.18-1.el7.x86_64                                                                                12/30
  正在安装    : m2crypto-0.21.1-17.el7.x86_64                                                                                13/30
  正在安装    : python-six-1.9.0-2.el7.noarch                                                                                14/30
  正在安装    : python-urllib3-1.10.2-5.el7.noarch                                                                           15/30
  正在安装    : python-requests-2.6.0-1.el7_1.noarch                                                                         16/30
  正在安装    : python-babel-0.9.6-8.el7.noarch                                                                              17/30
  正在安装    : python2-msgpack-0.5.6-5.el7.x86_64                                                                           18/30
  正在安装    : openpgm-5.2.122-2.el7.x86_64                                                                                 19/30
  正在安装    : zeromq-4.1.4-6.el7.x86_64                                                                                    20/30
  正在安装    : python2-zmq-14.7.0-8.el7.x86_64                                                                              21/30
  正在安装    : python-markupsafe-0.11-10.el7.x86_64                                                                         22/30
  正在安装    : python-jinja2-2.7.2-3.el7_6.noarch                                                                           23/30
  正在安装    : salt-2015.5.10-2.el7.noarch                                                                                  24/30
  正在安装    : salt-master-2015.5.10-2.el7.noarch                                                                           25/30
  正在安装    : salt-minion-2015.5.10-2.el7.noarch                                                                           26/30
  正在更新    : systemd-sysv-219-62.el7_6.7.x86_64                                                                           27/30
  清理        : systemd-sysv-219-62.el7.x86_64                                                                               28/30
  清理        : systemd-219-62.el7.x86_64                                                                                    29/30
  清理        : systemd-libs-219-62.el7.x86_64                                                                               30/30
  验证中      : python-backports-ssl_match_hostname-3.5.0.1-1.el7.noarch                                                      1/30
  验证中      : python-markupsafe-0.11-10.el7.x86_64                                                                          2/30
  验证中      : openpgm-5.2.122-2.el7.x86_64                                                                                  3/30
  验证中      : python2-msgpack-0.5.6-5.el7.x86_64                                                                            4/30
  验证中      : systemd-libs-219-62.el7_6.7.x86_64                                                                            5/30
  验证中      : python2-crypto-2.6.1-16.el7.x86_64                                                                            6/30
  验证中      : systemd-219-62.el7_6.7.x86_64                                                                                 7/30
  验证中      : python-jinja2-2.7.2-3.el7_6.noarch                                                                            8/30
  验证中      : python-babel-0.9.6-8.el7.noarch                                                                               9/30
  验证中      : zeromq-4.1.4-6.el7.x86_64                                                                                    10/30
  验证中      : python-six-1.9.0-2.el7.noarch                                                                                11/30
  验证中      : python-urllib3-1.10.2-5.el7.noarch                                                                           12/30
  验证中      : m2crypto-0.21.1-17.el7.x86_64                                                                                13/30
  验证中      : libsodium-1.0.18-1.el7.x86_64                                                                                14/30
  验证中      : salt-master-2015.5.10-2.el7.noarch                                                                           15/30
  验证中      : salt-minion-2015.5.10-2.el7.noarch                                                                           16/30
  验证中      : libtommath-0.42.0-6.el7.x86_64                                                                               17/30
  验证中      : python-backports-1.0-8.el7.x86_64                                                                            18/30
  验证中      : python2-zmq-14.7.0-8.el7.x86_64                                                                              19/30
  验证中      : libyaml-0.1.4-11.el7_0.x86_64                                                                                20/30
  验证中      : salt-2015.5.10-2.el7.noarch                                                                                  21/30
  验证中      : python-requests-2.6.0-1.el7_1.noarch                                                                         22/30
  验证中      : systemd-sysv-219-62.el7_6.7.x86_64                                                                           23/30
  验证中      : python-ipaddress-1.0.16-2.el7.noarch                                                                         24/30
  验证中      : systemd-python-219-62.el7_6.7.x86_64                                                                         25/30
  验证中      : PyYAML-3.10-11.el7.x86_64                                                                                    26/30
  验证中      : libtomcrypt-1.17-26.el7.x86_64                                                                               27/30
  验证中      : systemd-sysv-219-62.el7.x86_64                                                                               28/30
  验证中      : systemd-libs-219-62.el7.x86_64                                                                               29/30
  验证中      : systemd-219-62.el7.x86_64                                                                                    30/30

已安装:
  salt-master.noarch 0:2015.5.10-2.el7                             salt-minion.noarch 0:2015.5.10-2.el7

作为依赖被安装:
  PyYAML.x86_64 0:3.10-11.el7                            libsodium.x86_64 0:1.0.18-1.el7
  libtomcrypt.x86_64 0:1.17-26.el7                       libtommath.x86_64 0:0.42.0-6.el7
  libyaml.x86_64 0:0.1.4-11.el7_0                        m2crypto.x86_64 0:0.21.1-17.el7
  openpgm.x86_64 0:5.2.122-2.el7                         python-babel.noarch 0:0.9.6-8.el7
  python-backports.x86_64 0:1.0-8.el7                    python-backports-ssl_match_hostname.noarch 0:3.5.0.1-1.el7
  python-ipaddress.noarch 0:1.0.16-2.el7                 python-jinja2.noarch 0:2.7.2-3.el7_6
  python-markupsafe.x86_64 0:0.11-10.el7                 python-requests.noarch 0:2.6.0-1.el7_1
  python-six.noarch 0:1.9.0-2.el7                        python-urllib3.noarch 0:1.10.2-5.el7
  python2-crypto.x86_64 0:2.6.1-16.el7                   python2-msgpack.x86_64 0:0.5.6-5.el7
  python2-zmq.x86_64 0:14.7.0-8.el7                      salt.noarch 0:2015.5.10-2.el7
  systemd-python.x86_64 0:219-62.el7_6.7                 zeromq.x86_64 0:4.1.4-6.el7

作为依赖被升级:
  systemd.x86_64 0:219-62.el7_6.7         systemd-libs.x86_64 0:219-62.el7_6.7         systemd-sysv.x86_64 0:219-62.el7_6.7

完毕!

在被控端安装salt只需要安装salt-minion

[root@linux-02 ~]# yum install -y salt-minion
已加载插件:fastestmirror
Repository base is listed more than once in the configuration
Repository updates is listed more than once in the configuration
Repository extras is listed more than once in the configuration
Repository centosplus is listed more than once in the configuration
Repository contrib is listed more than once in the configuration
Determining fastest mirrors
epel/x86_64/metalink                                                                                        | 8.3 kB  00:00:00
 * base: mirrors.aliyun.com
 * epel: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
base                                                                                                        | 3.6 kB  00:00:00
epel                                                                                                        | 5.4 kB  00:00:00
extras                                                                                                      | 3.4 kB  00:00:00
mariadb-main                                                                                                | 2.9 kB  00:00:00
mariadb-maxscale                                                                                            | 2.4 kB  00:00:00
mariadb-tools                                                                                               | 2.9 kB  00:00:00
mongodb-org-4.0                                                                                             | 2.5 kB  00:00:00
updates                                                                                                     | 3.4 kB  00:00:00
zabbix                                                                                                      | 2.9 kB  00:00:00
zabbix-non-supported                                                                                        |  951 B  00:00:00
(1/3): epel/x86_64/updateinfo                                                                               | 993 kB  00:00:05
(2/3): updates/7/x86_64/primary_db                                                                          | 6.5 MB  00:00:07
(3/3): epel/x86_64/primary_db                                                                               | 6.8 MB  00:00:11
正在解决依赖关系
--> 正在检查事务
---> 软件包 salt-minion.noarch.0.2015.5.10-2.el7 将被 安装
--> 正在处理依赖关系 salt = 2015.5.10-2.el7,它被软件包 salt-minion-2015.5.10-2.el7.noarch 需要
--> 正在检查事务
---> 软件包 salt.noarch.0.2015.5.10-2.el7 将被 安装
--> 正在处理依赖关系 PyYAML,它被软件包 salt-2015.5.10-2.el7.noarch 需要
--> 正在处理依赖关系 m2crypto,它被软件包 salt-2015.5.10-2.el7.noarch 需要
--> 正在处理依赖关系 python-crypto,它被软件包 salt-2015.5.10-2.el7.noarch 需要
--> 正在处理依赖关系 python-jinja2,它被软件包 salt-2015.5.10-2.el7.noarch 需要
--> 正在处理依赖关系 python-msgpack,它被软件包 salt-2015.5.10-2.el7.noarch 需要
--> 正在处理依赖关系 python-requests,它被软件包 salt-2015.5.10-2.el7.noarch 需要
--> 正在处理依赖关系 python-zmq,它被软件包 salt-2015.5.10-2.el7.noarch 需要
--> 正在处理依赖关系 systemd-python,它被软件包 salt-2015.5.10-2.el7.noarch 需要
--> 正在检查事务
---> 软件包 PyYAML.x86_64.0.3.10-11.el7 将被 安装
---> 软件包 m2crypto.x86_64.0.0.21.1-17.el7 将被 安装
---> 软件包 python-jinja2.noarch.0.2.7.2-3.el7_6 将被 安装
--> 正在处理依赖关系 python-babel >= 0.8,它被软件包 python-jinja2-2.7.2-3.el7_6.noarch 需要
--> 正在处理依赖关系 python-markupsafe,它被软件包 python-jinja2-2.7.2-3.el7_6.noarch 需要
---> 软件包 python-requests.noarch.0.2.6.0-1.el7_1 将被 安装
--> 正在处理依赖关系 python-urllib3 >= 1.10.2-1,它被软件包 python-requests-2.6.0-1.el7_1.noarch 需要
---> 软件包 python2-crypto.x86_64.0.2.6.1-16.el7 将被 安装
--> 正在处理依赖关系 libtomcrypt.so.0()(64bit),它被软件包 python2-crypto-2.6.1-16.el7.x86_64 需要
---> 软件包 python2-msgpack.x86_64.0.0.5.6-5.el7 将被 安装
---> 软件包 python2-zmq.x86_64.0.14.7.0-8.el7 将被 安装
--> 正在处理依赖关系 libzmq.so.5()(64bit),它被软件包 python2-zmq-14.7.0-8.el7.x86_64 需要
---> 软件包 systemd-python.x86_64.0.219-62.el7_6.7 将被 安装
--> 正在处理依赖关系 systemd-libs = 219-62.el7_6.7,它被软件包 systemd-python-219-62.el7_6.7.x86_64 需要
--> 正在处理依赖关系 systemd = 219-62.el7_6.7,它被软件包 systemd-python-219-62.el7_6.7.x86_64 需要
--> 正在检查事务
---> 软件包 libtomcrypt.x86_64.0.1.17-26.el7 将被 安装
--> 正在处理依赖关系 libtommath >= 0.42.0,它被软件包 libtomcrypt-1.17-26.el7.x86_64 需要
--> 正在处理依赖关系 libtommath.so.0()(64bit),它被软件包 libtomcrypt-1.17-26.el7.x86_64 需要
---> 软件包 python-babel.noarch.0.0.9.6-8.el7 将被 安装
---> 软件包 python-markupsafe.x86_64.0.0.11-10.el7 将被 安装
---> 软件包 python-urllib3.noarch.0.1.10.2-5.el7 将被 安装
--> 正在处理依赖关系 python-six,它被软件包 python-urllib3-1.10.2-5.el7.noarch 需要
--> 正在处理依赖关系 python-ipaddress,它被软件包 python-urllib3-1.10.2-5.el7.noarch 需要
--> 正在处理依赖关系 python-backports-ssl_match_hostname,它被软件包 python-urllib3-1.10.2-5.el7.noarch 需要
---> 软件包 systemd.x86_64.0.219-62.el7 将被 升级
--> 正在处理依赖关系 systemd = 219-62.el7,它被软件包 systemd-sysv-219-62.el7.x86_64 需要
---> 软件包 systemd.x86_64.0.219-62.el7_6.7 将被 更新
---> 软件包 systemd-libs.x86_64.0.219-62.el7 将被 升级
---> 软件包 systemd-libs.x86_64.0.219-62.el7_6.7 将被 更新
---> 软件包 zeromq.x86_64.0.4.1.4-6.el7 将被 安装
--> 正在处理依赖关系 libpgm-5.2.so.0()(64bit),它被软件包 zeromq-4.1.4-6.el7.x86_64 需要
--> 正在处理依赖关系 libsodium.so.23()(64bit),它被软件包 zeromq-4.1.4-6.el7.x86_64 需要
--> 正在检查事务
---> 软件包 libsodium.x86_64.0.1.0.18-1.el7 将被 安装
---> 软件包 libtommath.x86_64.0.0.42.0-6.el7 将被 安装
---> 软件包 openpgm.x86_64.0.5.2.122-2.el7 将被 安装
---> 软件包 python-backports-ssl_match_hostname.noarch.0.3.5.0.1-1.el7 将被 安装
--> 正在处理依赖关系 python-backports,它被软件包 python-backports-ssl_match_hostname-3.5.0.1-1.el7.noarch 需要
---> 软件包 python-ipaddress.noarch.0.1.0.16-2.el7 将被 安装
---> 软件包 python-six.noarch.0.1.9.0-2.el7 将被 安装
---> 软件包 systemd-sysv.x86_64.0.219-62.el7 将被 升级
---> 软件包 systemd-sysv.x86_64.0.219-62.el7_6.7 将被 更新
--> 正在检查事务
---> 软件包 python-backports.x86_64.0.1.0-8.el7 将被 安装
--> 解决依赖关系完成

依赖关系解决

===================================================================================================================================
 Package                                           架构                 版本                           源                     大小
===================================================================================================================================
正在安装:
 salt-minion                                       noarch               2015.5.10-2.el7                epel                   26 k
为依赖而安装:
 PyYAML                                            x86_64               3.10-11.el7                    base                  153 k
 libsodium                                         x86_64               1.0.18-1.el7                   epel                  147 k
 libtomcrypt                                       x86_64               1.17-26.el7                    extras                224 k
 libtommath                                        x86_64               0.42.0-6.el7                   extras                 36 k
 m2crypto                                          x86_64               0.21.1-17.el7                  base                  429 k
 openpgm                                           x86_64               5.2.122-2.el7                  epel                  171 k
 python-babel                                      noarch               0.9.6-8.el7                    base                  1.4 M
 python-backports                                  x86_64               1.0-8.el7                      base                  5.8 k
 python-backports-ssl_match_hostname               noarch               3.5.0.1-1.el7                  base                   13 k
 python-ipaddress                                  noarch               1.0.16-2.el7                   base                   34 k
 python-jinja2                                     noarch               2.7.2-3.el7_6                  updates               518 k
 python-markupsafe                                 x86_64               0.11-10.el7                    base                   25 k
 python-requests                                   noarch               2.6.0-1.el7_1                  base                   94 k
 python-six                                        noarch               1.9.0-2.el7                    base                   29 k
 python-urllib3                                    noarch               1.10.2-5.el7                   base                  102 k
 python2-crypto                                    x86_64               2.6.1-16.el7                   epel                  477 k
 python2-msgpack                                   x86_64               0.5.6-5.el7                    epel                   64 k
 python2-zmq                                       x86_64               14.7.0-8.el7                   epel                  505 k
 salt                                              noarch               2015.5.10-2.el7                epel                  4.1 M
 systemd-python                                    x86_64               219-62.el7_6.7                 updates               133 k
 zeromq                                            x86_64               4.1.4-6.el7                    epel                  556 k
为依赖而更新:
 systemd                                           x86_64               219-62.el7_6.7                 updates               5.1 M
 systemd-libs                                      x86_64               219-62.el7_6.7                 updates               407 k
 systemd-sysv                                      x86_64               219-62.el7_6.7                 updates                84 k

事务概要
===================================================================================================================================
安装  1 软件包 (+21 依赖软件包)
升级           (  3 依赖软件包)

总下载量:15 M
Downloading packages:
Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
(1/25): PyYAML-3.10-11.el7.x86_64.rpm                                                                       | 153 kB  00:00:07
(2/25): libtomcrypt-1.17-26.el7.x86_64.rpm                                                                  | 224 kB  00:00:07
(3/25): libtommath-0.42.0-6.el7.x86_64.rpm                                                                  |  36 kB  00:00:07
(4/25): openpgm-5.2.122-2.el7.x86_64.rpm                                                                    | 171 kB  00:00:00
(5/25): m2crypto-0.21.1-17.el7.x86_64.rpm                                                                   | 429 kB  00:00:07
(6/25): libsodium-1.0.18-1.el7.x86_64.rpm                                                                   | 147 kB  00:00:07
(7/25): python-backports-1.0-8.el7.x86_64.rpm                                                               | 5.8 kB  00:00:00
(8/25): python-backports-ssl_match_hostname-3.5.0.1-1.el7.noarch.rpm                                        |  13 kB  00:00:00
(9/25): python-babel-0.9.6-8.el7.noarch.rpm                                                                 | 1.4 MB  00:00:00
(10/25): python-ipaddress-1.0.16-2.el7.noarch.rpm                                                           |  34 kB  00:00:00
(11/25): python-markupsafe-0.11-10.el7.x86_64.rpm                                                           |  25 kB  00:00:00
(12/25): python-six-1.9.0-2.el7.noarch.rpm                                                                  |  29 kB  00:00:00
(13/25): python-requests-2.6.0-1.el7_1.noarch.rpm                                                           |  94 kB  00:00:00
(14/25): python-urllib3-1.10.2-5.el7.noarch.rpm                                                             | 102 kB  00:00:00
(15/25): python2-crypto-2.6.1-16.el7.x86_64.rpm                                                             | 477 kB  00:00:00
(16/25): python2-zmq-14.7.0-8.el7.x86_64.rpm                                                                | 505 kB  00:00:00
(17/25): salt-2015.5.10-2.el7.noarch.rpm                                                                    | 4.1 MB  00:00:00
(18/25): salt-minion-2015.5.10-2.el7.noarch.rpm                                                             |  26 kB  00:00:00
(19/25): systemd-219-62.el7_6.7.x86_64.rpm                                                                  | 5.1 MB  00:00:00
(20/25): systemd-libs-219-62.el7_6.7.x86_64.rpm                                                             | 407 kB  00:00:00
(21/25): systemd-python-219-62.el7_6.7.x86_64.rpm                                                           | 133 kB  00:00:00
(22/25): systemd-sysv-219-62.el7_6.7.x86_64.rpm                                                             |  84 kB  00:00:00
(23/25): zeromq-4.1.4-6.el7.x86_64.rpm                                                                      | 556 kB  00:00:00
(24/25): python-jinja2-2.7.2-3.el7_6.noarch.rpm                                                             | 518 kB  00:00:05
(25/25): python2-msgpack-0.5.6-5.el7.x86_64.rpm                                                             |  64 kB  00:00:05
-----------------------------------------------------------------------------------------------------------------------------------
总计                                                                                               1.1 MB/s |  15 MB  00:00:13
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安装    : python-ipaddress-1.0.16-2.el7.noarch                                                                          1/28
  正在更新    : systemd-libs-219-62.el7_6.7.x86_64                                                                            2/28
  正在更新    : systemd-219-62.el7_6.7.x86_64                                                                                 3/28
  正在安装    : systemd-python-219-62.el7_6.7.x86_64                                                                          4/28
  正在安装    : PyYAML-3.10-11.el7.x86_64                                                                                     5/28
  正在安装    : python-backports-1.0-8.el7.x86_64                                                                             6/28
  正在安装    : python-backports-ssl_match_hostname-3.5.0.1-1.el7.noarch                                                      7/28
  正在安装    : libtommath-0.42.0-6.el7.x86_64                                                                                8/28
  正在安装    : libtomcrypt-1.17-26.el7.x86_64                                                                                9/28
  正在安装    : python2-crypto-2.6.1-16.el7.x86_64                                                                           10/28
  正在安装    : python-babel-0.9.6-8.el7.noarch                                                                              11/28
  正在安装    : libsodium-1.0.18-1.el7.x86_64                                                                                12/28
  正在安装    : m2crypto-0.21.1-17.el7.x86_64                                                                                13/28
  正在安装    : python-six-1.9.0-2.el7.noarch                                                                                14/28
  正在安装    : python-urllib3-1.10.2-5.el7.noarch                                                                           15/28
  正在安装    : python-requests-2.6.0-1.el7_1.noarch                                                                         16/28
  正在安装    : python2-msgpack-0.5.6-5.el7.x86_64                                                                           17/28
  正在安装    : openpgm-5.2.122-2.el7.x86_64                                                                                 18/28
  正在安装    : zeromq-4.1.4-6.el7.x86_64                                                                                    19/28
  正在安装    : python2-zmq-14.7.0-8.el7.x86_64                                                                              20/28
  正在安装    : python-markupsafe-0.11-10.el7.x86_64                                                                         21/28
  正在安装    : python-jinja2-2.7.2-3.el7_6.noarch                                                                           22/28
  正在安装    : salt-2015.5.10-2.el7.noarch                                                                                  23/28
  正在安装    : salt-minion-2015.5.10-2.el7.noarch                                                                           24/28
  正在更新    : systemd-sysv-219-62.el7_6.7.x86_64                                                                           25/28
  清理        : systemd-sysv-219-62.el7.x86_64                                                                               26/28
  清理        : systemd-219-62.el7.x86_64                                                                                    27/28
  清理        : systemd-libs-219-62.el7.x86_64                                                                               28/28
  验证中      : python-backports-ssl_match_hostname-3.5.0.1-1.el7.noarch                                                      1/28
  验证中      : python-markupsafe-0.11-10.el7.x86_64                                                                          2/28
  验证中      : openpgm-5.2.122-2.el7.x86_64                                                                                  3/28
  验证中      : python2-msgpack-0.5.6-5.el7.x86_64                                                                            4/28
  验证中      : systemd-libs-219-62.el7_6.7.x86_64                                                                            5/28
  验证中      : python2-crypto-2.6.1-16.el7.x86_64                                                                            6/28
  验证中      : systemd-219-62.el7_6.7.x86_64                                                                                 7/28
  验证中      : python-jinja2-2.7.2-3.el7_6.noarch                                                                            8/28
  验证中      : zeromq-4.1.4-6.el7.x86_64                                                                                     9/28
  验证中      : python-six-1.9.0-2.el7.noarch                                                                                10/28
  验证中      : python-urllib3-1.10.2-5.el7.noarch                                                                           11/28
  验证中      : m2crypto-0.21.1-17.el7.x86_64                                                                                12/28
  验证中      : libsodium-1.0.18-1.el7.x86_64                                                                                13/28
  验证中      : python-babel-0.9.6-8.el7.noarch                                                                              14/28
  验证中      : salt-minion-2015.5.10-2.el7.noarch                                                                           15/28
  验证中      : libtommath-0.42.0-6.el7.x86_64                                                                               16/28
  验证中      : python-backports-1.0-8.el7.x86_64                                                                            17/28
  验证中      : python2-zmq-14.7.0-8.el7.x86_64                                                                              18/28
  验证中      : salt-2015.5.10-2.el7.noarch                                                                                  19/28
  验证中      : python-requests-2.6.0-1.el7_1.noarch                                                                         20/28
  验证中      : systemd-sysv-219-62.el7_6.7.x86_64                                                                           21/28
  验证中      : python-ipaddress-1.0.16-2.el7.noarch                                                                         22/28
  验证中      : systemd-python-219-62.el7_6.7.x86_64                                                                         23/28
  验证中      : PyYAML-3.10-11.el7.x86_64                                                                                    24/28
  验证中      : libtomcrypt-1.17-26.el7.x86_64                                                                               25/28
  验证中      : systemd-sysv-219-62.el7.x86_64                                                                               26/28
  验证中      : systemd-libs-219-62.el7.x86_64                                                                               27/28
  验证中      : systemd-219-62.el7.x86_64                                                                                    28/28

已安装:
  salt-minion.noarch 0:2015.5.10-2.el7

作为依赖被安装:
  PyYAML.x86_64 0:3.10-11.el7                                                libsodium.x86_64 0:1.0.18-1.el7
  libtomcrypt.x86_64 0:1.17-26.el7                                           libtommath.x86_64 0:0.42.0-6.el7
  m2crypto.x86_64 0:0.21.1-17.el7                                            openpgm.x86_64 0:5.2.122-2.el7
  python-babel.noarch 0:0.9.6-8.el7                                          python-backports.x86_64 0:1.0-8.el7
  python-backports-ssl_match_hostname.noarch 0:3.5.0.1-1.el7                 python-ipaddress.noarch 0:1.0.16-2.el7
  python-jinja2.noarch 0:2.7.2-3.el7_6                                       python-markupsafe.x86_64 0:0.11-10.el7
  python-requests.noarch 0:2.6.0-1.el7_1                                     python-six.noarch 0:1.9.0-2.el7
  python-urllib3.noarch 0:1.10.2-5.el7                                       python2-crypto.x86_64 0:2.6.1-16.el7
  python2-msgpack.x86_64 0:0.5.6-5.el7                                       python2-zmq.x86_64 0:14.7.0-8.el7
  salt.noarch 0:2015.5.10-2.el7                                              systemd-python.x86_64 0:219-62.el7_6.7
  zeromq.x86_64 0:4.1.4-6.el7

作为依赖被升级:
  systemd.x86_64 0:219-62.el7_6.7         systemd-libs.x86_64 0:219-62.el7_6.7         systemd-sysv.x86_64 0:219-62.el7_6.7

完毕!

3.配置salt相关服务

修改三台服务器上的hosts文件,添加三台服务器的ip地址和hostname

[root@linux-001 ~]# vim /etc/hosts

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.174.128  linux-001
192.168.174.129  linux-02
192.168.174.130  linux-03

修改三台服务器的minion,下图修改的是001机器,002机器和003机器也一样。

[root@linux-001 ~]# vim /etc/salt/minion
master: linux-001   //添加一行master的信息
[root@linux-001 ~]# systemctl start salt-minion

启动salt-master,可以查看到salt启动的端口是4505和4506。

[root@linux-001 ~]# systemctl start salt-master
[root@linux-001 ~]# ps axu |grep salt
root      22042  0.0  1.8 234880 15660 ?        Ss   17:11   0:00 /usr/bin/python /usr/bin/salt-minion
root      22053  0.1  3.1 533480 26508 ?        Sl   17:11   0:00 /usr/bin/python /usr/bin/salt-minion
root      22957  2.7  3.2 320924 27436 ?        Ss   17:16   0:00 /usr/bin/python /usr/bin/salt-master
root      22964  1.2  3.1 408956 26020 ?        Sl   17:16   0:00 /usr/bin/python /usr/bin/salt-master
root      22965  0.0  2.7 402852 23092 ?        Sl   17:16   0:00 /usr/bin/python /usr/bin/salt-master
root      22966  0.0  2.8 402852 23700 ?        Sl   17:16   0:00 /usr/bin/python /usr/bin/salt-master
root      22968  0.1  2.7 320924 22468 ?        S    17:16   0:00 /usr/bin/python /usr/bin/salt-master
root      22975  4.4  3.1 655004 26244 ?        Rl   17:16   0:00 /usr/bin/python /usr/bin/salt-master
root      22976  5.5  3.1 656036 26248 ?        Sl   17:16   0:00 /usr/bin/python /usr/bin/salt-master
root      22977  4.3  3.1 655008 26252 ?        Sl   17:16   0:00 /usr/bin/python /usr/bin/salt-master
root      22978  4.5  3.1 655008 26252 ?        Sl   17:16   0:00 /usr/bin/python /usr/bin/salt-master
root      22979  4.4  3.1 655008 26256 ?        Rl   17:16   0:00 /usr/bin/python /usr/bin/salt-master
root      22991 38.0  2.7 566708 23000 ?        Rl   17:16   0:03 /usr/bin/python /usr/bin/salt-master
root      23155  0.0  0.1 112724   988 pts/0    R+   17:16   0:00 grep --color=auto salt
root      23156  0.0  2.8 655008 23672 ?        R    17:16   0:00 /usr/bin/python /usr/bin/salt-master
[root@linux-001 ~]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 192.168.174.128:27017   0.0.0.0:*               LISTEN      9911/mongod
tcp        0      0 127.0.0.1:27017         0.0.0.0:*               LISTEN      9911/mongod
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      1/systemd
tcp        0      0 0.0.0.0:20048           0.0.0.0:*               LISTEN      6864/rpc.mountd
tcp        0      0 0.0.0.0:49681           0.0.0.0:*               LISTEN      6847/rpc.statd
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      6828/sshd
tcp        0      0 0.0.0.0:4505            0.0.0.0:*               LISTEN      22965/python
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      7238/master
tcp        0      0 0.0.0.0:4506            0.0.0.0:*               LISTEN      22991/python
tcp        0      0 0.0.0.0:2049            0.0.0.0:*               LISTEN      -
tcp        0      0 0.0.0.0:36388           0.0.0.0:*               LISTEN      -
tcp6       0      0 :::3306                 :::*                    LISTEN      7162/mysqld
tcp6       0      0 :::111                  :::*                    LISTEN      1/systemd
tcp6       0      0 :::20048                :::*                    LISTEN      6864/rpc.mountd
tcp6       0      0 :::22                   :::*                    LISTEN      6828/sshd
tcp6       0      0 :::36887                :::*                    LISTEN      -
tcp6       0      0 ::1:25                  :::*                    LISTEN      7238/master
tcp6       0      0 :::2049                 :::*                    LISTEN      -
tcp6       0      0 :::51393                :::*                    LISTEN      6847/rpc.statd

4.salt的配置认证

master端和minion端通信需要建立一个安全通道,传输过程需要加密,所以得配置认证,也是通过密钥对来加密解密的
minion在第一次启动时会在/etc/salt/pki/minion/下生成minion.pem和minion.pub,其中.pub为公钥,它会把公钥传输给master
master第一次启动时也会在/etc/salt/pki/master下生成密钥对,当master接收到minion传过来的公钥后,通过salt-key工具接受这个公钥,一旦接受后就会在/etc/salt/pki/master/minions/目录里存放刚刚接受的公钥,同时客户端也会接受master传过去的公钥,把它放在/etc/salt/pki/minion目录下,并命名为minion_master.pub
以上过程需要借助salt-key工具来实现
执行如下命令 salt-key -a aming-01// -a后面跟主机名,可以认证指定主机
salt-key -a aming-02
-a 后面跟主机名,认证指定主机
-A 认证所有主机
-r 跟主机名,拒绝指定主机
-R 拒绝所有主机
-d 跟主机名,删除指定主机认证
-D 删除全部主机认证
-y 省略掉交互,相当于直接按了y

[root@linux-01 ~]# salt-key -L
Accepted Keys:
Denied Keys:
Unaccepted Keys:
linux-01
linux-02
linux-03
Rejected Keys:
[root@linux-01 ~]# vim /etc/hosts
[root@linux-01 ~]#
[root@linux-01 ~]# salt-key -A
The following keys are going to be accepted:
Unaccepted Keys:
linux-01
linux-02
linux-03
Proceed? [n/Y] Y
Key for minion linux-01 accepted.
Key for minion linux-02 accepted.
Key for minion linux-03 accepted.
[root@linux-01 ~]# ls /etc/salt/pki/master/minions
linux-001  linux-02  linux-03

5.salt执行远程命令

在这里插入图片描述

[root@linux-01 ~]# salt '*' test.ping
linux-02:
    True
linux-03:
    True
linux-01:
    True


[root@linux-01 ~]# salt '*' cmd.run  'ls'
linux-02:
    111
    111.log
    Ctrl-C
    anaconda-ks.cfg
    grant
    mongodb-org-4.0.10-1.el7.x86_64.rpm
    mongodb-org-mongos-4.0.10-1.el7.x86_64.rpm
    mongodb-org-server-4.0.10-1.el7.x86_64.rpm
    mongodb-org-shell-4.0.10-1.el7.x86_64.rpm
    mongodb-org-tools-4.0.10-1.el7.x86_64.rpm
    mongodb-rpm-package
    rpmbuild
    show
    vim-enhanced-7.4.160-5.el7.x86_64.rpm
linux-01:
    1.txt
    111
    222
    add_user.sh
    anaconda-ks.cfg
    bb.log
    ceshi
    git-1.8.3.1-20.el7.x86_64.rpm
    ifcfg-ens33:0
    iptables.sh
    mongodb
    mongodb-org-4.0.10-1.el7.x86_64.rpm
    mongodb-org-shell-4.0.10-1.el7.x86_64.rpm
    my.tpt
    nload-0.7.4-4.el7.x86_64.rpm
    passwd
    process.txt
    subversion-1.7.14-14.el7.i686.rpm
    subversion-1.7.14-14.el7.x86_64.rpm
    test
    user-passwd.txt
    v2ray.sh
    xaa
    xihaji.log
    zabbix.sql
    zsh-5.0.2-31.el7.x86_64.rpm
linux-03:
    anaconda-ks.cfg
    bitnami-wordpress-5.1-0-linux-x64-installer.run
    initial-setup-ks.cfg
    mysql-community-release-el7-5.noarch.rpm
    new
    下载
    公共
    图片
    文档
    桌面
    模板
    视频
    音乐
[root@linux-01 ~]# salt '*' cmd.run  'hostname'
linux-02:
    linux-02
linux-03:
    linux-03
linux-01:
    linux-01

[root@linux-01 ~]# salt 'linux-01' cmd.run 'df -h'
linux-001:
    Filesystem      Size  Used Avail Use% Mounted on
    /dev/sda3        16G  9.0G  6.9G  57% /
    devtmpfs        396M     0  396M   0% /dev
    tmpfs           407M   16K  407M   1% /dev/shm
    tmpfs           407M   17M  390M   5% /run
    tmpfs           407M     0  407M   0% /sys/fs/cgroup
    tmpfs           407M   14M  394M   4% /tmp
    /dev/sda1       197M  115M   82M  59% /boot
    tmpfs            82M     0   82M   0% /run/user/0

6.grains

grains是在minion启动时收集到的一些信息,比如操作系统类型、网卡ip、内核版本、cpu架构等。
salt ‘aming-02’ grains.ls 列出所有的grains项目名字
salt ‘aming-02’ grains.items 列出所有grains项目以及值
grains的信息并不是动态的,并不会实时变更,它是在minion启动时收集到的。
我们可以根据grains收集到的一些信息,做配置管理工作。
grains支持自定义信息。

[root@linux-01 ~]# salt 'linux-02' grains.ls
linux-02:
    - SSDs
    - biosreleasedate
    - biosversion
    - cpu_flags
    - cpu_model
    - cpuarch
    - domain
    - fqdn
    - fqdn_ip4
    - fqdn_ip6
    - gpus
    - host
    - hwaddr_interfaces
    - id
    - init
    - ip4_interfaces
    - ip6_interfaces
    - ip_interfaces
    - ipv4
    - ipv6
    - kernel
    - kernelrelease
    - locale_info
    - localhost
    - lsb_distrib_id
    - machine_id
    - manufacturer
    - master
    - mdadm
    - mem_total
    - nodename
    - num_cpus
    - num_gpus
    - os
    - os_family
    - osarch
    - oscodename
    - osfinger
    - osfullname
    - osmajorrelease
    - osrelease
    - osrelease_info
    - path
    - productname
    - ps
    - pythonexecutable
    - pythonpath
    - pythonversion
    - saltpath
    - saltversion
    - saltversioninfo
    - selinux
    - serialnumber
    - server_id
    - shell
    - systemd
    - virtual
    - zmqversion
[root@linux-01 ~]# salt 'linux-02' grains.items
linux-02:
    ----------
    SSDs:
    biosreleasedate:
        04/13/2018
    biosversion:
        6.00
    cpu_flags:
        - fpu
        - vme
        - de
        - pse
        - tsc
        - msr
        - pae
        - mce
        - cx8
        - apic
        - sep
        - mtrr
        - pge
        - mca
        - cmov
        - pat
        - pse36
        - clflush
        - mmx
        - fxsr
        - sse
        - sse2
        - ss
        - syscall
        - nx
        - pdpe1gb
        - rdtscp
        - lm
        - constant_tsc
        - arch_perfmon
        - nopl
        - xtopology
        - tsc_reliable
        - nonstop_tsc
        - eagerfpu
        - pni
        - pclmulqdq
        - ssse3
        - fma
        - cx16
        - pcid
        - sse4_1
        - sse4_2
        - x2apic
        - movbe
        - popcnt
        - tsc_deadline_timer
        - aes
        - xsave
        - avx
        - f16c
        - rdrand
        - hypervisor
        - lahf_lm
        - abm
        - fsgsbase
        - tsc_adjust
        - bmi1
        - avx2
        - smep
        - bmi2
        - invpcid
        - xsaveopt
        - arat
    cpu_model:
        Intel(R) Core(TM) i5-4460  CPU @ 3.20GHz
    cpuarch:
        x86_64
    domain:
    fqdn:
        linux-02
    fqdn_ip4:
        - 192.168.174.129
    fqdn_ip6:
        - fe80::e3af:26e5:ac7b:b1f
        - fe80::86ff:d912:c144:4503
    gpus:
        |_
          ----------
          model:
              SVGA II Adapter
          vendor:
              unknown
    host:
        linux-02
    hwaddr_interfaces:
        ----------
        ens33:
            00:50:56:3a:cd:af
        ens37:
            00:0c:29:4b:77:f1
        lo:
            00:00:00:00:00:00
    id:
        linux-02
    init:
        systemd
    ip4_interfaces:
        ----------
        ens33:
            - 192.168.174.129
        ens37:
            - 192.168.100.100
        lo:
            - 127.0.0.1
    ip6_interfaces:
        ----------
        ens33:
            - fe80::86ff:d912:c144:4503
        ens37:
            - fe80::e3af:26e5:ac7b:b1f
        lo:
            - ::1
    ip_interfaces:
        ----------
        ens33:
            - 192.168.174.129
            - fe80::86ff:d912:c144:4503
        ens37:
            - 192.168.100.100
            - fe80::e3af:26e5:ac7b:b1f
        lo:
            - 127.0.0.1
            - ::1
    ipv4:
        - 127.0.0.1
        - 192.168.100.100
        - 192.168.174.129
    ipv6:
        - ::1
        - fe80::86ff:d912:c144:4503
        - fe80::e3af:26e5:ac7b:b1f
    kernel:
        Linux
    kernelrelease:
        3.10.0-957.el7.x86_64
    locale_info:
        ----------
        defaultencoding:
            UTF-8
        defaultlanguage:
            zh_CN
        detectedencoding:
            UTF-8
    localhost:
        linux-02
    lsb_distrib_id:
        CentOS Linux
    machine_id:
        42b310e318a04f10a706fc5677d2ba1b
    manufacturer:
        VMware, Inc.
    master:
        linux-01
    mdadm:
    mem_total:
        812
    nodename:
        linux-02
    num_cpus:
        1
    num_gpus:
        1
    os:
        CentOS
    os_family:
        RedHat
    osarch:
        x86_64
    oscodename:
        Core
    osfinger:
        CentOS Linux-7
    osfullname:
        CentOS Linux
    osmajorrelease:
        7
    osrelease:
        7.6.1810
    osrelease_info:
        - 7
        - 6
        - 1810
    path:
        /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
    productname:
        VMware Virtual Platform
    ps:
        ps -efH
    pythonexecutable:
        /usr/bin/python
    pythonpath:
        - /usr/bin
        - /usr/lib64/python27.zip
        - /usr/lib64/python2.7
        - /usr/lib64/python2.7/plat-linux2
        - /usr/lib64/python2.7/lib-tk
        - /usr/lib64/python2.7/lib-old
        - /usr/lib64/python2.7/lib-dynload
        - /usr/lib64/python2.7/site-packages
        - /usr/lib/python2.7/site-packages
    pythonversion:
        - 2
        - 7
        - 5
        - final
        - 0
    saltpath:
        /usr/lib/python2.7/site-packages/salt
    saltversion:
        2015.5.10
    saltversioninfo:
        - 2015
        - 5
        - 10
        - 0
    selinux:
        ----------
        enabled:
            False
        enforced:
            Disabled
    serialnumber:
        VMware-56 4d e3 03 ef 50 69 16-d9 ab 38 33 9b 4b 77 e7
    server_id:
        2079933721
    shell:
        /bin/sh
    systemd:
        ----------
        features:
            +PAM +AUDIT +SELINUX +IMA -APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ +LZ4 -SECCOMP +BLKID +ELFUTILS +KMOD +IDN
        version:
            219
    virtual:
        VMware
    zmqversion:
        4.1.4

自定义grains,编辑minion上的机器,从master上获取grains的值,如果获取不到值,需要把minion上重新启动salt-minion,或者killall掉salt-minion,然后启动salt-minion。

[root@linux-02 ~]# vim /etc/salt/grains

role: nginx
env: test
 
[root@linux-02 ~]# systemctl restart salt-minion   

可以在master上查看到已经获取到了值。

[root@linux-01 ~]#  salt '*' grains.item role env
linux-03:
    ----------
    env:
    role:
linux-02:
    ----------
    env:
        test
    role:
        nginx
linux-01:
    ----------
    env:
    role:
[root@linux-01 ~]#  salt -G role:nginx cmd.run 'hostname'
linux-02:
    linux-02

7.pillar

pillar和grains不一样,是在master上定义的,并且是针对minion定义的一些信息。像一些比较重要的数据(密码)可以存在pillar里,还可以定义变量等。
配置自定义pillar vim /etc/salt/master 找到如下配置://去掉前面的警号
pillar_roots:
base: #此行前面有两个空格
- /srv/pillar #此行前面有4个空格
mkdir /srv/pillar
vim /srv/pillar/test.sls
//内容如下 conf: /etc/123.conf

[root@linux-01 ~]#  vim /etc/salt/master

pillar_roots:   #可以认为这是一个项目
  base:          #base表示项目的key
    - /srv/pillar    # 这一行代表是值
	
[root@linux-01 ~]# killall salt-master
[root@linux-01 ~]# systemctl start salt-master
[root@linux-01 ~]# mkdir /srv/pillar
[root@linux-01 ~]# cd !$
cd /srv/pillar

[root@linux-01 pillar]# vi test.sys

conf: /etc/123.conf

[root@linux-01 pillar]# vim top.sys

base:
  'linux-02'
    - test

[root@linux-01 pillar]#  salt '*' saltutil.refresh_pillar   //在此处不需要重启服务,当更改完pillar配置文件后,我们可以通过刷新pillar配置来获取新的pillar状态

linux-02:
    True
linux-03:
    True
linux-001:
    True
[root@linux-01 pillar]# salt  '*' pillar.item conf
linux-02:
    /etc/123.conf
linux-03:
    ----------
linux-001:
    ----------
[root@linux-01 pillar]#

8.salt安装httpd服务

修改master的配置文件,把file_roots配置打开,如下

[root@linux-01 ~]# cat /etc/salt/master  |grep -Ev '^#|^$'
file_roots:
  base:
    - /srv/salt
pillar_roots:
  base:
    - /srv/salt/pillar
     
[root@linux-01 ~]# cd /srv/salt
[root@linux-01 salt]# vim top.sls

base:
  '*':                 #前面有2个空格,*代表在所有的客户端执行httpd模块
    - httpd            #前面有4个空格
     
[root@linux-01 salt]# vim httpd.sls
#这个就是httpd模块的内容
httpd-service:           #说明:httpd-service是id的名字,自定义的。
  pkg.installed:         # pkg.installed 为包安装函数
    - names:             #names是要安装的包的名字。这里如果只有一个服务,那么就可以写成 –name: httpd 不用再换一行了。
      - httpd
      - httpd-devel
  service.running:       #service.running也是一个函数,来保证指定的服务启动,enable表示开机启动。
    - name: httpd
    - enable: True
     
[root@linux-01 salt]#  systemctl restart salt-master

[root@linux-01 salt]#  
[root@linux-01 salt]# salt 'linux-01' state.highstate  
linux-01:
----------
          ID: httpd-service
    Function: pkg.installed
        Name: httpd
      Result: True
     Comment: Package httpd is already installed.
     Started: 17:58:56.814905
    Duration: 4862.334 ms
     Changes:
----------
          ID: httpd-service
    Function: pkg.installed
        Name: httpd-devel
      Result: True
     Comment: The following packages were installed/updated: httpd-devel
     Started: 17:59:01.677410
    Duration: 74855.449 ms
     Changes:
              ----------
              apr-devel:
                  ----------
                  new:
                      1.4.8-5.el7
                  old:
              apr-util-devel:
                  ----------
                  new:
                      1.5.2-6.el7
                  old:
              cyrus-sasl:
                  ----------
                  new:
                      2.1.26-23.el7
                  old:
              cyrus-sasl-devel:
                  ----------
                  new:
                      2.1.26-23.el7
                  old:
              expat-devel:
                  ----------
                  new:
                      2.1.0-11.el7
                  old:
              httpd-devel:
                  ----------
                  new:
                      2.4.6-93.el7.centos
                  old:
              libdb-devel:
                  ----------
                  new:
                      5.3.21-25.el7
                  old:
              openldap-devel:
                  ----------
                  new:
                      2.4.44-21.el7_6
                  old:
----------
          ID: httpd-service
    Function: service.running
        Name: httpd
      Result: True
     Comment: Service httpd has been enabled, and is running
     Started: 18:00:16.635596
    Duration: 1856.958 ms
     Changes:
              ----------
              httpd:
                  True

Summary
------------
Succeeded: 3 (changed=2)
Failed:    0
------------
Total states run:     3


# 检查一下httpd服务是否启动,看看端口是否存在
[root@linux-01 salt]# ps -ef |grep httpd
root     12778     1  0 18:00 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache   12780 12778  0 18:00 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache   12782 12778  0 18:00 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache   12783 12778  0 18:00 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache   12784 12778  0 18:00 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache   12785 12778  0 18:00 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
root     12878  9088  0 18:03 pts/0    00:00:00 grep --color=auto httpd
[root@linux-01 salt]# netstat -lntp |grep httpd
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      12778/httpd
[root@localhost salt]#

9.salt管理文件目录

9.1 salt管理文件


[root@linux-01 salt]# vi top.sls

base:
  '*':
#    - httpd
    - ceshi
[root@linux-01 salt]# vi ceshi.sls

file_ceshi:
  file.managed:
    - name: /tmp/linux.txt
    - source: salt://1.txt
    - user: root
    - group: root
    - mode: 600


[root@linux-01 salt]# salt 'linux-02'  state.highstate
linux-02:
----------
          ID: file_test
    Function: file.managed
        Name: /tmp/linux.txt
      Result: True
     Comment: File /tmp/linux.txt updated
     Started: 18:24:02.050767
    Duration: 15.572 ms
     Changes:
              ----------
              diff:
                  New file

Summary
------------
Succeeded: 1 (changed=1)
Failed:    0
------------
Total states run:     1

在这里插入图片描述在这里插入图片描述

9.2 salt管理目录

说明:这里有一个问题,如果source对应的目录下有空目录的话,客户端上不会创建该目录

[root@linux-01 salt]# vim top.sls

base:
  '*':
#    - httpd
    - ceshi_dir


[root@linux-01 salt]# vim ceshi_dir.sls

ceshi_dir:
  file.recurse:
    - name: /tmp/ceshi_dir
    - source: salt://123/
    - user: root
    - file_mode: 640
    - dir_mode: 750
    - mkdir: True
    - clean: True  #加上它之后,源删除文件或目录,目标也会跟着删除,否则不会删除
     
[root@linux-01 salt]# salt 'linux-02'  state.highstate
linux-02:
----------
          ID: ceshi_dir
    Function: file.recurse
        Name: /tmp/ceshi_dir
      Result: True
     Comment: Recursively updated /tmp/ceshi_dir
     Started: 18:36:09.869619
    Duration: 1085.54 ms
     Changes:
              ----------
              /tmp/ceshi_dir:
                  ----------
                  mode:
                      0750
              /tmp/ceshi_dir/2.txt:
                  ----------
                  diff:
                      New file
                  mode:
                      0640

Summary
------------
Succeeded: 1 (changed=1)
Failed:    0
------------
Total states run:     1

在这里插入图片描述

10.salt管理远程命令

[root@linux-01 test]# pwd
/srv/salt/test
[root@linux-01 test]# vi top.sls

base:
  '*':
   - shell_test

[root@linux-01 test]# vim shell_test.sls

shell_test:
  cmd.script:
    - source: salt://test/1.sh
    - user: root

[root@linux-01 test]# vi 1.sh

#!/bin/bash
touch /tmp/test.txt
cat /etc/passwd > /tmp/test.txt


[root@linux-01 test]# salt 'linux-01' state.highstate
linux-01:
----------
          ID: shell_test
    Function: cmd.script
      Result: True
     Comment: Command 'shell_test' run
     Started: 23:23:56.940758
    Duration: 316.797 ms
     Changes:
              ----------
              pid:
                  19880
              retcode:
                  0
              stderr:
              stdout:

Summary
------------
Succeeded: 1 (changed=1)
Failed:    0
------------
Total states run:     1

在这里插入图片描述

11.salt管理任务计划

11.1 salt创建一个crontab

[root@linux-01 test]# vim top.sls

base:
  '*':
   - cron_test

[root@linux-01 test]# vim cron_test.sls

cron_test:
  cron.present:
    - name: /bin/touch /tmp/111.txt
    - user: root
    - minute: '*'
    - hour: 20
    - daymonth: '*'
    - month: '*'
    - dayweek: '*

[root@linux-01 test]# salt 'linux-01' state.highstate
linux-01:
----------
          ID: cron_test
    Function: cron.present
        Name: /bin/touch /tmp/111.txt
      Result: True
     Comment: Cron /bin/touch /tmp/111.txt added to root's crontab
     Started: 23:32:51.244319
    Duration: 36.301 ms
     Changes:
              ----------
              root:
                  /bin/touch /tmp/111.txt

Summary
------------
Succeeded: 1 (changed=1)
Failed:    0
------------
Total states run:     1

在这里插入图片描述
以上的结果中,前两行的#号行是不可以删除的,否则会出现crontab出现重复的定时任务(如下图所示)。
在这里插入图片描述

11.1 salt删除一个crontab

[root@linux-01 test]# vim top.sls

base:
  '*':
   - cron_delete_test

[root@linux-01 test]# vim cron_delete_test.sls

cron_delete_test:
 cron.absent:
   - name: /bin/touch /tmp/111.txt

[root@linux-01 test]# salt 'linux-01' state.highstate
linux-01:
----------
          ID: cron_delete_test
    Function: cron.absent
        Name: /bin/touch /tmp/111.txt
      Result: True
     Comment: Cron /bin/touch /tmp/111.txt removed from root's crontab
     Started: 23:42:08.306320
    Duration: 29.779 ms
     Changes:
              ----------
              root:
                  /bin/touch /tmp/111.txt

Summary
------------
Succeeded: 1 (changed=1)
Failed:    0
------------
Total states run:     1

在这里插入图片描述

注意:我们不能随意改动crontab,否则就没法删除或者修改这个cron了。

12.salt其它可能会用到的命令

12.1 cp.get_file 拷贝master上的文件到客户端

此处我设置的master配置文件file_roots的路径为:/srv/salt/test

[root@linux-01 test]# cp /etc/salt/master  1.txt
[root@linux-01 test]# ll
总用量 52
-rw-r--r--. 1 root root    64 5月  20 23:14 1.sh
-rw-r-----. 1 root root 29548 5月  20 23:46 1.txt
-rw-r--r--. 1 root root    68 5月  20 23:42 cron_delete_test.sls
-rw-r--r--. 1 root root   170 5月  20 23:32 cron_test.sls
-rw-r--r--. 1 root root    69 5月  20 23:23 shell_test.sls
-rw-r--r--. 1 root root    35 5月  20 23:40 top.sls
[root@linux-01 test]# salt 'linux-01' cp.get_file salt://1.txt /tmp/123.txt
linux-01:
    /tmp/123.txt
[root@linux-01 test]# ll /tmp/123.txt
-rw-r--r--. 1 root root 29548 5月  20 23:47 /tmp/123.txt

12.2 cp.get_dir 拷贝目录

[root@linux-01 test]# cp -r  /root/Python-3.6.1    ./
[root@linux-01 test]# ll -d Python-3.6.1/
drwxr-xr-x. 18 root root 4096 5月  20 23:50 Python-3.6.1/

[root@linux-01 test]# salt '*' cp.get_dir salt://Python-3.6.1   /tmp/
……过程省略……

[root@linux-01 test]# ll -d /tmp/Python-3.6.1/
drwxr-xr-x. 18 root root 4096 5月  20 23:53 /tmp/Python-3.6.1/

注意:如上所示,在拷贝目录的时候不要在目标目录也就是/tmp/后面再写一个Python-3.6.1 ,否则拷贝后的目录会在/tmp/Python-3.6.1/下面。

12.3 manage.up 显示存活的minion

[root@linux-01 test]# salt-run manage.up
- linux-01
- linux-02
[root@linux-01 test]#

12.4 cmd.script 执行master上的shell脚本

可以使用此方法在master机器上给目标机器批量执行脚本
在这里插入图片描述

13. salt-ssh使用

  • salt-ssh不需要对客户端做认证,客户端也不用安装salt-minion,它类似于expect
  • 在master上安装 yum install -y salt-ssh

13.1 安装salt-ssh

[root@linux-01 test]# yum install -y salt-ssh
已加载插件:fastestmirror, product-id, search-disabled-repos, subscription-manager

This system is not registered with an entitlement server. You can use subscription-manager to register.

Loading mirror speeds from cached hostfile
epel/x86_64/metalink                                                                                                                                                                                               | 8.3 kB  00:00:00
 * base: mirrors.aliyun.com
 * epel: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
base                                                                                                                                                                                                               | 3.6 kB  00:00:00
epel                                                                                                                                                                                                               | 4.7 kB  00:00:00
extras                                                                                                                                                                                                             | 2.9 kB  00:00:00
updates                                                                                                                                                                                                            | 2.9 kB  00:00:00
(1/2): epel/x86_64/updateinfo                                                                                                                                                                                      | 1.0 MB  00:00:05
(2/2): epel/x86_64/primary_db                                                                                                                                                                                      | 6.8 MB  00:00:00
正在解决依赖关系
--> 正在检查事务
---> 软件包 salt-ssh.noarch.0.2015.5.10-2.el7 将被 安装
--> 解决依赖关系完成

依赖关系解决

==========================================================================================================================================================================================================================================
 Package                                                 架构                                                  版本                                                             源                                                   大小
==========================================================================================================================================================================================================================================
正在安装:
 salt-ssh                                                noarch                                                2015.5.10-2.el7                                                  epel                                                 15 k

事务概要
==========================================================================================================================================================================================================================================
安装  1 软件包

总下载量:15 k
安装大小:3.1 k
Downloading packages:
salt-ssh-2015.5.10-2.el7.noarch.rpm                                                                                                                                                                                |  15 kB  00:00:05
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安装    : salt-ssh-2015.5.10-2.el7.noarch                                                                                                                                                                                       1/1
  验证中      : salt-ssh-2015.5.10-2.el7.noarch                                                                                                                                                                                       1/1

已安装:
  salt-ssh.noarch 0:2015.5.10-2.el7

完毕!

13.2 修改salt-ssh的配置文件

修改salt-ssh的配置文件/etc/salt/roster

[root@linux-01 test]# vim /etc/salt/roster

# Sample salt-ssh config file
#web1:
#  host: 192.168.42.1 # The IP addr or DNS hostname
#  user: fred         # Remote executions will be executed as user fred
#  passwd: foobarbaz  # The password to use for login, if omitted, keys are used
#  sudo: True         # Whether to sudo to root, not enabled by default
#web2:
#  host: 192.168.42.2
linux-01:
  host: 192.168.1.232
  user: root
  passwd: 123qwe
linux-02:
  host: 192.168.1.233
  user: root
  passwd: 123qwe

13.3 通过salt-ssh远程执行命令

  • salt-ssh --key-deploy ‘*’ -r ‘w’ //第一次执行的时候会自动把本机的公钥放到对方机器上,然后就可以把roster里面的密码去掉咯;

  • 出现以下图中的错误,我们是不是需要在第一次ssh登录服务器的时候,需要输入一个yes,这样就可以解决我们的问题咯。

在这里插入图片描述

[root@linux-01 test]# ssh 192.168.1.233
The authenticity of host '192.168.1.233 (192.168.1.233)' can't be established.
ECDSA key fingerprint is SHA256:qkOCQ1F0B7lBYD5h+P14aeopz1uhsvUABYq55dgU1FM.
ECDSA key fingerprint is MD5:d8:10:97:52:db:5c:7b:0a:c7:0e:46:1b:cb:be:68:7a.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.1.233' (ECDSA) to the list of known hosts.
root@192.168.1.233's password:

[root@linux-01 test]# salt-ssh  --key-deploy  '*' -r 'w'
linux-02:
    ----------
    retcode:
        0
    stderr:
    stdout:
        root@192.168.1.233's password:
         00:20:06 up 82 days,  7:09,  3 users,  load average: 0.00, 0.01, 0.05
        USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
        root     tty1                      282月20 82days  0.02s  0.02s -bash
        root     pts/0    192.168.1.6      23:08   26:30   0.01s  0.01s -bash
        root     pts/1    192.168.1.246    17:08    5:15m  0.21s  0.21s -bash
linux-01:
    ----------
    retcode:
        0
    stderr:
    stdout:
        root@192.168.1.232's password:
         00:20:06 up  6:54,  2 users,  load average: 0.01, 0.04, 0.05
        USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
        root     pts/0    192.168.1.246    18:10    5:42m  0.08s  0.08s -bash
        root     pts/1    192.168.1.6      23:08    6.00s  0.99s  0.00s /usr/bin/python /usr/bin/salt-ssh --key-deploy * -r w

我们现在先删除配置文件的密码,在执行一次命令测试下。

[root@linux-01 test]# vim /etc/salt/roster

# Sample salt-ssh config file
#web1:
#  host: 192.168.42.1 # The IP addr or DNS hostname
#  user: fred         # Remote executions will be executed as user fred
#  passwd: foobarbaz  # The password to use for login, if omitted, keys are used
#  sudo: True         # Whether to sudo to root, not enabled by default
#web2:
#  host: 192.168.42.2
linux-01:
  host: 192.168.1.232
  user: root
linux-02:
  host: 192.168.1.233
  user: root

发现在远程执行命令的时候,去掉密码会卡住不动,所以我们需要使用ssh-key生成一个公钥,把这个公钥推送给客户端。

[root@linux-01 test]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:hs4xeHWCsVd2NcU/WQmV8aoodPEFU/MFXv1g9bQTys8 root@linux-01
The key's randomart image is:
+---[RSA 2048]----+
|      .   o ++OO@|
|       + o ..++OX|
|      o + o  +o+O|
|     . + o o .o++|
|    . = S . . .E.|
|     + = . . .   |
|      o . . .    |
|         .       |
|                 |
+----[SHA256]-----+
[root@linux-01 test]# ssh-copy-id -i /root/.ssh/  root@linux-01
authorized_keys  id_rsa           id_rsa.pub       known_hosts
[root@linux-01 test]# ssh-copy-id -i /root/.ssh/id_rsa.pub   root@linux-01
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@linux-01's password:

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@linux-01'"
and check to make sure that only the key(s) you wanted were added.

[root@linux-01 test]# ssh linux-01
Last login: Thu May 21 00:41:45 2020 from 192.168.1.232
[root@linux-01 ~]# exit
登出
Connection to linux-01 closed.

[root@linux-01 test]# salt-ssh 'linux-01' -r 'w'
linux-01:
    ----------
    retcode:
        0
    stderr:
    stdout:
         00:46:20 up  7:21,  2 users,  load average: 0.25, 0.07, 0.06
        USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
        root     pts/0    192.168.1.246    18:10    6:08m  0.08s  0.08s -bash
        root     pts/1    192.168.1.6      23:08    4.00s  1.16s  0.00s /usr/bin/python /usr/bin/salt-ssh linux-01 -r w

同样的方法可以把公钥推送给linux-02

[root@linux-01 test]# ssh-copy-id -i /root/.ssh/id_rsa.pub   root@linux-02
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@linux-02's password:

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@linux-02'"
and check to make sure that only the key(s) you wanted were added.

[root@linux-01 test]# salt-ssh '*' -r 'date'
linux-02:
    ----------
    retcode:
        0
    stderr:
    stdout:
        2020年 05月 21日 星期四 00:47:27 CST
linux-01:
    ----------
    retcode:
        0
    stderr:
    stdout:
        2020年 05月 21日 星期四 00:47:27 CST

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值