SaltStack之salt-ssh

Posted 卑微小胡

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SaltStack之salt-ssh相关的知识,希望对你有一定的参考价值。

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
....此处省略N行
mysql:
  host: 192.168.96.134
  user: root
  passwd: 123456

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

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

测试连通性

[root@master ~]# salt-ssh '*' -r 'dnf -y install python3'
mysql:
    ----------
    retcode:
        None
    stderr:
    stdout:
        
        root@192.168.96.134's password: 
        Last metadata expiration check: 2:11:04 ago on Thu 08 Jul 2021 09:41:45 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-setuptools-39.2.0-6.el8.noarch.r 998 kB/s | 163 kB     00:00    
        (2/3): python3-pip-9.0.3-19.el8.noarch.rpm      107 kB/s |  20 kB     00:00    
        (3/3): python36-3.6.8-37.module_el8.5.0+771+e5d 104 kB/s |  19 kB     00:00    
        --------------------------------------------------------------------------------
        Total                                            49 kB/s | 201 kB     00:04     
        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!
[root@master ~]# salt-ssh '*' test.ping
mysql:
    True

通过salt-ssh初始化系统安装salt-minion

执行状态命令,初始化系统,安装salt-minion

[root@master ~]# mkdir -p /srv/salt/base/repo,files
[root@master ~]# cp /etc/yum.repos.d/salt.repo /srv/salt/base/repo/salt.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.repo
    - source: salt://repo/salt.repo
    - user: root
    - group: root
    - mode: 0644
[root@master ~]# vim /srv/salt/base/minion.sls
minion_install:
  pkg.installed:
    - name: salt-minion

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: minion_install

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

[root@master ~]# salt-ssh '*' state.sls repo
mysql:
----------
          ID: salt_repo
    Function: file.managed
        Name: /etc/yum.repos.d/salt.repo
      Result: True
     Comment: File /etc/yum.repos.d/salt.repo updated
     Started: 00:06:09.512910
    Duration: 41.975 ms
     Changes:   
              ----------
              diff:
                  New file
              mode:
                  0644

Summary for mysql
------------
Succeeded: 1 (changed=1)
Failed:    0
------------
Total states run:     1
Total run time:  41.975 ms
[root@master ~]# salt-ssh '*' state.sls minion
mysql:
----------
          ID: minion_install
    Function: pkg.installed
        Name: salt-minion
      Result: True
     Comment: The following packages were installed/updated: salt-minion
     Started: 00:06:39.755567
    Duration: 89302.759 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: minion_conf
    Function: file.managed
        Name: /etc/salt/minion
      Result: True
     Comment: File /etc/salt/minion updated
     Started: 00:08:09.066091
    Duration: 99.455 ms
     Changes:   
              ----------
              diff:
                  --- 
                  +++ 
                  @@ -14,7 +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.96.129
                   # Set http proxy information for the minion when doing requests
                   #proxy_host:
                   #proxy_port:
              mode:
                  0644
----------
          ID: minion_service
    Function: service.running
        Name: salt-minion
      Result: True
     Comment: Service salt-minion has been enabled, and is running
     Started: 00:08:09.191625
    Duration: 912.338 ms
     Changes:   
              ----------
              salt-minion:
                  True

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

测试

[root@master base]# salt-key -L
Accepted Keys:
minion1
Denied Keys:
Unaccepted Keys:
master
mysql
Rejected Keys:
[root@master base]# salt-key -ya mysql
The following keys are going to be accepted:
Unaccepted Keys:
mysql
Key for minion mysql accepted.
[root@master base]# salt '*' test.ping
mysql:
    True
minion1:
    True

以上是关于SaltStack之salt-ssh的主要内容,如果未能解决你的问题,请参考以下文章

SaltStack之salt-ssh

SaltStack之salt-ssh

SaltStack之salt-ssh

saltstack之salt-ssh的细枝末节

SaltStack的salt-ssh使用及LAMP状态设计部署

SaltStack salt-ssh 用法