SaltStack之salt-ssh

本文详细介绍了如何使用SaltStack的salt-ssh模块在无需安装salt-minion的情况下远程管理主机。salt-ssh依赖SSH协议,需要Python支持,并且可以通过配置文件或密钥对实现身份验证。内容包括salt-ssh的安装、配置、远程管理方式,以及使用示例,如安装Python3和管理服务状态。此外,还展示了通过编写yaml文件自动化安装salt-minion的流程。
摘要由CSDN通过智能技术生成

SaltStack之salt-ssh

salt-ssh介绍

salt-ssh可以让我们不需要在受控机上安装salt-minion客户端也能够实现管理操作。

salt-ssh的特点

  • 远程系统需要Python支持,除非使用-r选项发送原始ssh命令
  • salt-ssh是一个软件包,需安装之后才能使用,命令本身也是salt-ssh
  • salt-ssh不会取代标准的Salt通信系统,它只是提供了一个基于SSH的替代方案,不需要ZeroMQ和agent

请注意,由于所有与Salt SSH的通信都是通过SSH执行的,因此它比使用ZeroMQ的标准Salt慢得多

salt-ssh远程管理的方式

salt-ssh有两种方式实现远程管理,一种是在配置文件中记录所有客户端的信息,诸如 IP 地址、端口号、用户名、密码以及是否支持sudo等;另一种是使用密钥实现远程管理,不需要输入密码。

salt-ssh管理

在 master 上安装 salt-ssh

[root@master ~]# yum -y install salt-ssh

通过使用用户名密码的SSH实现远程管理

修改配置文件,添加受控机信息

[root@master ~]# vim /etc/salt/roster
vm1:
  host: 192.168.207.131
  user: root
  port: 22
  passwd: 789123

vm2:
  host: 192.168.207.137
  user: root
  port: 22
  passwd: 789123

vm3:
  host: 192.168.207.136
  user: root
  port: 22
  passwd: 789123


测试连通性

[root@master ~]# vim ~/.ssh/config
StrictHostKeyChecking no

[root@master ~]# salt-ssh '*' test.ping
vm1:
    True

从上面的信息可以看出,第一次访问时需要输入 yes/no ,但是 saltstack 是不支持交互式操作的,所以为了解决这个问题,我们需要对其进行设置,让系统不进行主机验证。

[root@master ~]# vim ~/.ssh/config
StrictHostKeyChecking no
[root@master ~]# salt-ssh '*' test.ping
vm2:
    True
vm1:
    True
vm3:
    True

把所有的被控主机安装python3

[root@master ~]# salt-ssh '*' -r 'yum install -y python3'
vm2:
    ----------
    retcode:
        0
    stderr:
    stdout:
        
        root@192.168.207.137's password: 
        Last metadata expiration check: 1 day, 22:14:24 ago on Tue 06 Jul 2021 10:13:55 PM CST.
        Package python36-3.6.8-37.module_el8.5.0+771+e5d9a225.x86_64 is already installed.
        Dependencies resolved.
        Nothing to do.
        Complete!
vm3:
    ----------
    retcode:
        0
    stderr:
    stdout:
        
        root@192.168.207.136's password: 
        Last metadata expiration check: 2:05:56 ago on Thu 08 Jul 2021 06:22:22 PM CST.
        Dependencies resolved.
        ================================================================================
         Package            Arch   Version                              Repo       Size
        ================================================================================
        Installing:
         python36           x86_64 3.6.8-37.module_el8.5.0+771+e5d9a225 appstream  19 k
        Installing dependencies:
         python3-pip        noarch 9.0.3-19.el8                         appstream  20 k
         python3-setuptools noarch 39.2.0-6.el8                         baseos    163 k
        Enabling module streams:
         python36                  3.6                                                 
        
        Transaction Summary
        ================================================================================
        Install  3 Packages
        
        Total download size: 201 k
        Installed size: 466 k
        Downloading Packages:
        (1/3): python3-pip-9.0.3-19.el8.noarch.rpm      169 kB/s |  20 kB     00:00    
        (2/3): python36-3.6.8-37.module_el8.5.0+771+e5d 154 kB/s |  19 kB     00:00    
        (3/3): python3-setuptools-39.2.0-6.el8.noarch.r 746 kB/s | 163 kB     00:00    
        --------------------------------------------------------------------------------
        Total                                           158 kB/s | 201 kB     00:01     
        Running transaction check
        Transaction check succeeded.
        Running transaction test
        Transaction test succeeded.
        Running transaction
          Preparing        :                                                        1/1 
          Installing       : python3-setuptools-39.2.0-6.el8.noarch                 1/3 
          Installing       : python36-3.6.8-37.module_el8.5.0+771+e5d9a225.x86_64   2/3 
          Running scriptlet: python36-3.6.8-37.module_el8.5.0+771+e5d9a225.x86_64   2/3 
          Installing       : python3-pip-9.0.3-19.el8.noarch                        3/3 
          Running scriptlet: python3-pip-9.0.3-19.el8.noarch                        3/3 
          Verifying        : python3-pip-9.0.3-19.el8.noarch                        1/3 
          Verifying        : python36-3.6.8-37.module_el8.5.0+771+e5d9a225.x86_64   2/3 
          Verifying        : python3-setuptools-39.2.0-6.el8.noarch                 3/3 
        
        Installed:
          python3-pip-9.0.3-19.el8.noarch                                               
          python3-setuptools-39.2.0-6.el8.noarch                                        
          python36-3.6.8-37.module_el8.5.0+771+e5d9a225.x86_64                          
        
        Complete!
vm1:
    ----------
    retcode:
        0
    stderr:
    stdout:
        
        root@192.168.207.131's password: 
        Last metadata expiration check: 0:41:00 ago on Thu 08 Jul 2021 07:47:21 PM CST.
        Package python36-3.6.8-37.module_el8.5.0+771+e5d9a225.x86_64 is already installed.
        Dependencies resolved.
        Nothing to do.
        Complete!

编写ymal文件

[root@master ~]# mkdir -p /srv/salt/base/{repo,files}
[root@master ~]# \cp /etc/yum.repos.d/salt-latest.repo /srv/salt/base/repo/salt-latest.repo
[root@master ~]# cp /etc/salt/minion /srv/salt/base/files/

[root@master ~]# vim /srv/salt/base/repo.sls
salt-repo:
  file.managed:
    - name: /etc/yum.repos.d/salt-latest.repo
    - source: salt://repo/salt-latest.repo
    - user: root
    - group: root
    - mode: 644

[root@master ~]# vim /srv/salt/base/minion.sls
salt-minion-install:
  pkg.installed:
    - name: salt-minion

salt-minion-conf:
  file.managed:
    - name: /etc/salt/minion
    - source: salt://files/minion
    - user: root
    - group: root
    - mode: 644
    - template: jinja
    - default:
      ID: {{ grains['ipv4'] [1] }}
    - require:
      - pkg: salt-minion-install

salt-minion-service:
  service.running:
    - name: salt-minion
    - enable: True
    - start: True
    - watch:
       - file: /etc/salt/minion

开始自动安装

[root@master ~]# salt-ssh '*' state.sls repo
vm3:
----------
          ID: salt-repo
    Function: file.managed
        Name: /etc/yum.repos.d/salt-latest.repo
      Result: True
     Comment: File /etc/yum.repos.d/salt-latest.repo updated
     Started: 21:03:45.218592
    Duration: 28.625 ms
     Changes:   
              ----------
              diff:
                  New file
              mode:
                  0644

Summary for vm3
------------
Succeeded: 1 (changed=1)
Failed:    0
------------
Total states run:     1
Total run time:  28.625 ms
vm2:
----------
          ID: salt-repo
    Function: file.managed
        Name: /etc/yum.repos.d/salt-latest.repo
      Result: True
     Comment: File /etc/yum.repos.d/salt-latest.repo updated
     Started: 21:03:46.348977
    Duration: 25.983 ms
     Changes:   
              ----------
              diff:
                  New file
              mode:
                  0644

Summary for vm2
------------
Succeeded: 1 (changed=1)
Failed:    0
------------
Total states run:     1
Total run time:  25.983 ms
vm1:
----------
          ID: salt-repo
    Function: file.managed
        Name: /etc/yum.repos.d/salt-latest.repo
      Result: True
     Comment: File /etc/yum.repos.d/salt-latest.repo is in the correct state
     Started: 21:03:48.462771
    Duration: 24.432 ms
     Changes:   

Summary for vm1
------------
Succeeded: 1
Failed:    0
------------
Total states run:     1
Total run time:  24.432 ms

[root@master ~]# salt-ssh '*' state.sls minion
vm2:
----------
          ID: salt-minion-install
    Function: pkg.installed
        Name: salt-minion
      Result: True
     Comment: All specified packages are already installed
     Started: 21:05:12.268538
    Duration: 693.595 ms
     Changes:   
----------
          ID: salt-minion-conf
    Function: file.managed
        Name: /etc/salt/minion
      Result: True
     Comment: File /etc/salt/minion updated
     Started: 21:05:12.964761
    Duration: 68.4 ms
     Changes:   
              ----------
              diff:
                  --- 
                  +++ 
                  @@ -14,14 +14,7 @@
                   # Set the location of the salt master server. If the master server cannot be
                   # resolved, then the minion will fail to start.
                   #master: salt
                  -
                   master: 192.168.207.131
                  -
                  -mysql.host: '192.168.207.136'
                  -mysql.user: 'salt'
                  -mysql.pass: 'salt'
                  -mysql.db: 'salt'
                  -mysql.port: 3306
                   
                   # Set http proxy information for the minion when doing requests
                   #proxy_host:
              mode:
                  0644
----------
          ID: salt-minion-service
    Function: service.running
        Name: salt-minion
      Result: True
     Comment: Service restarted
     Started: 21:05:13.072602
    Duration: 793.615 ms
     Changes:   
              ----------
              salt-minion:
                  True

Summary for vm2
------------
Succeeded: 3 (changed=2)
Failed:    0
------------
Total states run:     3
Total run time:   1.556 s
vm1:
----------
          ID: salt-minion-install
    Function: pkg.installed
        Name: salt-minion
      Result: True
     Comment: All specified packages are already installed
     Started: 21:05:14.592060
    Duration: 814.719 ms
     Changes:   
----------
          ID: salt-minion-conf
    Function: file.managed
        Name: /etc/salt/minion
      Result: True
     Comment: File /etc/salt/minion updated
     Started: 21:05:15.409407
    Duration: 46.531 ms
     Changes:   
              ----------
              mode:
                  0644
----------
          ID: salt-minion-service
    Function: service.running
        Name: salt-minion
      Result: True
     Comment: Service salt-minion has been enabled, and is in the desired state
     Started: 21:05:15.456752
    Duration: 180.639 ms
     Changes:   
              ----------
              salt-minion:
                  True

Summary for vm1
------------
Succeeded: 3 (changed=2)
Failed:    0
------------
Total states run:     3
Total run time:   1.042 s
vm3:
----------
          ID: salt-minion-install
    Function: pkg.installed
        Name: salt-minion
      Result: True
     Comment: The following packages were installed/updated: salt-minion
     Started: 21:05:12.369995
    Duration: 34323.132 ms
     Changes:   
              ----------
              gpg-pubkey.(none):
                  ----------
                  new:
                      8483c65d-5ccc5b19,de57bfbe-53a9be98
                  old:
                      8483c65d-5ccc5b19
              libsodium:
                  ----------
                  new:
                      1.0.18-2.el8
                  old:
              libunwind:
                  ----------
                  new:
                      1.3.1-3.el8
                  old:
              openpgm:
                  ----------
                  new:
                      5.2.122-21.el8
                  old:
              python3-babel:
                  ----------
                  new:
                      2.5.1-6.el8
                  old:
              python3-chardet:
                  ----------
                  new:
                      3.0.4-7.el8
                  old:
              python3-contextvars:
                  ----------
                  new:
                      2.4-1.el8
                  old:
              python3-distro:
                  ----------
                  new:
                      1.4.0-2.module_el8.5.0+761+faacb0fb
                  old:
              python3-idna:
                  ----------
                  new:
                      2.5-5.el8
                  old:
              python3-immutables:
                  ----------
                  new:
                      0.15-2.el8
                  old:
              python3-jinja2:
                  ----------
                  new:
                      2.10.1-3.el8
                  old:
              python3-m2crypto:
                  ----------
                  new:
                      0.35.2-5.el8
                  old:
              python3-markupsafe:
                  ----------
                  new:
                      0.23-19.el8
                  old:
              python3-msgpack:
                  ----------
                  new:
                      0.6.2-1.el8
                  old:
              python3-psutil:
                  ----------
                  new:
                      5.4.3-10.el8
                  old:
              python3-pycurl:
                  ----------
                  new:
                      7.43.0.2-4.el8
                  old:
              python3-pysocks:
                  ----------
                  new:
                      1.6.8-3.el8
                  old:
              python3-pytz:
                  ----------
                  new:
                      2017.2-9.el8
                  old:
              python3-pyyaml:
                  ----------
                  new:
                      3.12-12.el8
                  old:
              python3-requests:
                  ----------
                  new:
                      2.20.0-2.1.el8_1
                  old:
              python3-urllib3:
                  ----------
                  new:
                      1.24.2-5.el8
                  old:
              python3-zmq:
                  ----------
                  new:
                      19.0.0-1.el8
                  old:
              salt:
                  ----------
                  new:
                      3003.1-1.el8
                  old:
              salt-minion:
                  ----------
                  new:
                      3003.1-1.el8
                  old:
              yum-utils:
                  ----------
                  new:
                      4.0.18-4.el8
                  old:
              zeromq:
                  ----------
                  new:
                      4.3.4-2.el8
                  old:
----------
          ID: salt-minion-conf
    Function: file.managed
        Name: /etc/salt/minion
      Result: True
     Comment: File /etc/salt/minion updated
     Started: 21:05:46.695672
    Duration: 63.668 ms
     Changes:   
              ----------
              diff:
                  --- 
                  +++ 
                  @@ -14,6 +14,7 @@
                   # Set the location of the salt master server. If the master server cannot be
                   # resolved, then the minion will fail to start.
                   #master: salt
                  +master: 192.168.207.131
                   
                   # Set http proxy information for the minion when doing requests
                   #proxy_host:
              mode:
                  0644
----------
          ID: salt-minion-service
    Function: service.running
        Name: salt-minion
      Result: True
     Comment: Service salt-minion has been enabled, and is running
     Started: 21:05:46.767414
    Duration: 443.277 ms
     Changes:   
              ----------
              salt-minion:
                  True

Summary for vm3
------------
Succeeded: 3 (changed=3)
Failed:    0
------------
Total states run:     3
Total run time:  34.830 s

成功

[root@master ~]# salt-ssh '*' cmd.run 'systemctl restart salt-minion'
vm3:
vm2:
vm1:
[root@master ~]# salt-key -yA
[root@master ~]# salt-key -L
Accepted Keys:
master
minion
mysql
Denied Keys:
Unaccepted Keys:
Rejected Keys:

[root@master ~]# salt-ssh '*' state.sls minion
vm2:
----------
          ID: salt-minion-install
    Function: pkg.installed
        Name: salt-minion
      Result: True
     Comment: All specified packages are already installed
     Started: 21:07:48.294851
    Duration: 607.257 ms
     Changes:   
----------
          ID: salt-minion-conf
    Function: file.managed
        Name: /etc/salt/minion
      Result: True
     Comment: File /etc/salt/minion is in the correct state
     Started: 21:07:48.904992
    Duration: 50.208 ms
     Changes:   
----------
          ID: salt-minion-service
    Function: service.running
        Name: salt-minion
      Result: True
     Comment: The service salt-minion is already running
     Started: 21:07:48.956176
    Duration: 46.309 ms
     Changes:   

Summary for vm2
------------
Succeeded: 3
Failed:    0
------------
Total states run:     3
Total run time: 703.774 ms
vm3:
----------
          ID: salt-minion-install
    Function: pkg.installed
        Name: salt-minion
      Result: True
     Comment: All specified packages are already installed
     Started: 21:07:51.060503
    Duration: 646.68 ms
     Changes:   
----------
          ID: salt-minion-conf
    Function: file.managed
        Name: /etc/salt/minion
      Result: True
     Comment: File /etc/salt/minion is in the correct state
     Started: 21:07:51.710438
    Duration: 48.137 ms
     Changes:   
----------
          ID: salt-minion-service
    Function: service.running
        Name: salt-minion
      Result: True
     Comment: The service salt-minion is already running
     Started: 21:07:51.759509
    Duration: 38.529 ms
     Changes:   

Summary for vm3
------------
Succeeded: 3
Failed:    0
------------
Total states run:     3
Total run time: 733.346 ms
vm1:
----------
          ID: salt-minion-install
    Function: pkg.installed
        Name: salt-minion
      Result: True
     Comment: All specified packages are already installed
     Started: 21:07:51.142167
    Duration: 867.488 ms
     Changes:   
----------
          ID: salt-minion-conf
    Function: file.managed
        Name: /etc/salt/minion
      Result: True
     Comment: File /etc/salt/minion is in the correct state
     Started: 21:07:52.012309
    Duration: 46.204 ms
     Changes:   
----------
          ID: salt-minion-service
    Function: service.running
        Name: salt-minion
      Result: True
     Comment: The service salt-minion is already running
     Started: 21:07:52.059343
    Duration: 37.25 ms
     Changes:   

Summary for vm1
------------
Succeeded: 3
Failed:    0
------------
Total states run:     3
Total run time: 950.942 ms
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值