salt-ssh

1. salt-ssh介绍

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

1.1 salt-ssh的特点

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

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

1.2 salt-ssh远程管理的方式

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

2. salt-ssh管理

在 master 上安装 salt-ssh

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

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

  • 修改配置文件,添加受控机信息
[root@master ~]# tail -4 /etc/salt/roster
node1:
  host: 192.168.237.137
  user: root
  passwd: 1
  • 当受控主机较多时可以使用脚本来修改配置文件
[root@master ~]# cat host.info
1 192.168.237.137
2 192.168.237.138

[root@master ~]# cat test.sh
#!/bin/bash

while read line;do
cat >> /etc/salt/roster <<EOF
node$(echo $line |awk '{print $1}'):
  host: $(echo $line |awk '{print $2}')
  user: root
  passwd: 1
EOF
done < host.info
  • 测试连通性
[root@master ~]# salt-ssh -r 'node1' "yum -y install python3"

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.237.137 (192.168.237.137)' can't be established.
		ECDSA key fingerprint is SHA256:PiMfugKE4H3qZKYvhqHI1qhbI4JckOO6JiZZNjcSQCw.
		Are you sure you want to continue connecting (yes/no/[fingerprint])?

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

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

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

2.2 免密登录的方式

生成密钥

[root@master ~]# ssh-keygen -t rsa
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:VGm7GLwkADgcgfvTJR9ONGDHPuiImvUeECoYS32faYM root@master
The key's randomart image is:
+---[RSA 3072]----+
|o++.oo.   ..     |
|+.....+  .o      |
|.+.. * o.. .     |
|++ .+ X.* .      |
|=o.+ E @S+ .     |
|o =.o + + .      |
|.o o.            |
|o   ..           |
|   ..            |
+----[SHA256]-----+
[root@master ~]# 
[root@master ~]# ls .ssh/
config  id_rsa  id_rsa.pub  known_hosts

[root@master ~]# ssh-copy-id root@192.168.237.137
/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@192.168.237.137's password: 

Number of key(s) added: 1

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

//删除roster中定义的密码
[root@master ~]# tail -3 /etc/salt/roster
node1:
  host: 192.168.237.137
  user: root

//测试
[root@master ~]# salt-ssh 'node1' test.ping
Permission denied for host node1, do you want to deploy the salt-ssh key? (password required):
[Y/n] y
Password for root@node1: 
node1:
    True

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

安装 salt-ssh

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

测试连通性

//配置yum源
[root@master ~]# cd /srv/salt/base/init/yum/
[root@master yum]# cat 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-{{ grains['osrelease'] }}.repo:
  file.managed:
    - source: salt://init/yum/files/epel-{{ grains['osrelease'] }}.repo
    - user: root
    - group: root
    - mode: '0644'

/etc/yum.repos.d/salt-{{ grains['osrelease'] }}.repo:
  file.managed:
    - source: salt://init/yum/files/salt-{{ grains['osrelease'] }}.repo
    - user: root
    - group: root
    - mode: '0644'
[root@master yum]# ls files/
centos-7.repo  centos-8.repo  epel-7.repo  epel-8.repo  salt-7.repo  salt-8.repo

安装salt-minion

[root@master salt-minion]# pwd
/srv/salt/base/init/salt-minion
[root@master salt-minion]# ls
files  main.sls
[root@master salt-minion]# cat main.sls 
include:
  - init.yum.main

salt-minion:
  pkg.installed

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

salt-minion.service:
  service.running:
    - enable: true
[root@master salt-minion]# ls files/
minion

//定义变量
[root@master base]# cat salt-minion 
master_ip: 192.168.237.167
[root@master base]# cat top.sls 
base:
  '*':
    - salt-minion

[root@master salt-minion]# salt-ssh 'node1' state.sls init.salt-minion.main
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值