salt-ssh

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 
[root@master ~]# cat /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
lamp:
  host: 192.168.240.70
  user: root
  passwd: 1

测试连通性

[root@master ~]# salt-ssh '*' test.ping
vm1:
    ----------
    retcode:
        254
    stderr:
    stdout:
        The host key needs to be accepted, to auto accept run salt-ssh with the -i flag:
        The authenticity of host '192.168.69.202 (192.168.69.202)' can't be established.
        ECDSA key fingerprint is SHA256:Nz8CAwwL3HRh/Lvqejqa+eiV3A09xGYYfG2A/W8wRPs.
        ECDSA key fingerprint is MD5:8c:b3:22:14:7a:8a:bc:34:f9:9d:3c:3a:07:8a:96:20.
        Are you sure you want to continue connecting (yes/no)?

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

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

[root@master ~]# salt-ssh lamp test.ping
lamp:
    True

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

安装 salt-ssh

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

修改roster配置文件,添加受控主机

[root@master ~]# vim /etc/salt/roster 
[root@master ~]# cat /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
lamp:
  host: 192.168.240.70
  user: root
  passwd: 1

测试连通性

[root@master ~]# salt-ssh lamp test.ping
lamp:
    True

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

[root@master init]# cat yum/main.sls 
{% if grains['os'] == 'RedHat' %}
/etc/yum.repos.d/centos-{{ grains['osrelease'] }}.repo
  file.managed:
    - source: salt://init/yum/files/centos-{{ grains['osrelease'] }}.repo
    - user: root
    - group: root
    - mode: '0644'
{% endif %}

/etc/yum.repos.d/epel.repo:
  file.managed:
    - source: salt://init/yum/files/epel.repo
    - user: root
    - group: root
    - mode: '0644'

/etc/yum.repos.d/salt.repo:
  file.managed:
    - source: salt://init/yum/files/salt.repo
    - user: root
    - group: root
    - mode: '0644'
[root@master init]# cat salt_minion/minion.sls 
include:
  - init.yum.main

salt-minion:
  pkg.installed:
    - pkg: salt-minion

/etc/salt/minion
  file.managed:
    - source: salt://init/salt_minion/files/minion.j2
    - user: root
    - user: root
    - mode: '0644'
    - template: jinja

salt-minion.service:
  service.running:
    - enable: true
    
[root@master salt_minion]# salt-ssh lamp state.sls init.salt_minion.minion
lamp:
----------
          ID: /etc/yum.repos.d/epel.repo
    Function: file.managed
      Result: True
     Comment: File /etc/yum.repos.d/epel.repo is in the correct state
     Started: 02:36:34.403639
    Duration: 29.538 ms
     Changes:   
----------
          ID: /etc/yum.repos.d/salt.repo
    Function: file.managed
      Result: True
     Comment: File /etc/yum.repos.d/salt.repo is in the correct state
     Started: 02:36:34.433416
    Duration: 5.386 ms
     Changes:   
----------
          ID: salt-minion
    Function: pkg.installed
      Result: True
     Comment: All specified packages are already installed
     Started: 02:36:35.236310
    Duration: 706.151 ms
     Changes:   
----------
          ID: /etc/salt/minion
    Function: file.managed
      Result: True
     Comment: File /etc/salt/minion updated
     Started: 02:36:35.942680
    Duration: 41.556 ms
     Changes:   
              ----------
              mode:
                  0644
----------
          ID: salt-minion.service
    Function: service.running
      Result: True
     Comment: The service salt-minion.service is already running
     Started: 02:36:35.985279
    Duration: 36.332 ms
     Changes:   

Summary for lamp
------------
Succeeded: 5 (changed=1)
Failed:    0
------------
Total states run:     5
Total run time: 818.963 ms
上一篇:Linux 下搭建jsp服务器(配置jsp开发环境)


下一篇:SQL Server 补位