文章目录
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 ~]# vim /etc/salt/roster
....此处省略N行
node1:
host: 192.168.91.134
user: root
passwd: amu123
[root@master ~]# salt-ssh 'node1‘ test.ping
node1:
----------
retcode:
10
stderr:
ERROR: Unable to locate appropriate python command
stdout:
ERROR: Python version error. Recommendation(s) follow:
- Install Python 3 on the target machine(s)
- You can use ssh_pre_flight or raw shell (-r) to install Python 3 // 需要安装python3
// 安装python3
[root@master ~]# salt-ssh -r 'node1' 'yum -y install python3'
[root@node1 ~]# which python3
/usr/bin/python3
// 安装完之后在测试
[root@master ~]# salt-ssh 'node1' test.ping
node1:
True
测试连通性
// 第一次ping的时候他会问你是否要继续连接
[root@master .ssh]# ls
known_hosts
[root@master .ssh]# ls
known_hosts
[root@master .ssh]# rm -f known_hosts
[root@master .ssh]# salt-ssh 'node1' test.ping
node1:
----------
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.91.134 (192.168.91.134)' 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 .ssh]# vim ~/.ssh/config
StrictHostKeyChecking no
[root@master .ssh]# salt-ssh '*' test.ping
node1:
True
2.2 通过salt-ssh初始化系统安装salt-minion
安装 salt-ssh
[root@master ~]# yum -y install salt-ssh
修改roster配置文件,添加受控主机
[root@master ~]# vim /etc/salt/roster
....此处省略N行
node1:
host: 192.168.91.134
user: root
passwd: amu123
测试连通性
[root@master ~]# salt-ssh 'node1' test.ping
node1:
True
执行状态命令,初始化系统,安装salt-minion
[root@master ~]# cd /srv/salt/base/init/
[root@master init]# ls
basepkgs history postfix sshd yum
chrony kernel salt-minion sudo
firewalld main.sls selinux timeout
// 准备工作
[root@master init]# cd salt-minion/
[root@master salt-minion]#
[root@master salt-minion]# ls
files main.sls
[root@master salt-minion]# vim 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
- require:
- pkg: salt-minion
salt-minion.service:
service.running:
- enable: true
- reload: true
- watch:
- file: /etc/salt/minion
// 开始安装
[root@node1 ~]# systemctl status salt-minion
Unit salt-minion.service could not be found.
[root@master ~]# salt-ssh 'node1' state.sls init.salt-minion.main
// 查看安装情况
[root@node1 yum.repos.d]# systemctl status salt-minion
● salt-minion.service - The Salt Minion
Loaded: loaded (/usr/lib/systemd/system/salt-minion.servi>
Active: active (running) since Mon 2021-11-29 17:19:08 CS>
Docs: man:salt-minion(1)
file:///usr/share/doc/salt/html/contents.html
https://docs.saltproject.io/en/latest/contents.ht>
Main PID: 77393 (salt-minion)
Tasks: 3 (limit: 4743)
Memory: 50.4M
CGroup: /system.slice/salt-minion.service
├─77393 /usr/bin/python3.6 /usr/bin/salt-minion
└─77429 /usr/bin/python3.6 /usr/bin/salt-minion
Nov 29 17:19:07 node1 systemd[1]: Starting The Salt Minion...
Nov 29 17:19:08 node1 systemd[1]: Started The Salt Minion.
// 查看key
[root@master ~]# salt-key -L
Accepted Keys:
Denied Keys:
Unaccepted Keys:
node1
Rejected Keys:
// 接受所有key
[root@master ~]# salt-key -yA
The following keys are going to be accepted:
Unaccepted Keys:
node1
Key for minion node1 accepted.
[root@master ~]# salt-key -L
Accepted Keys:
node1
Denied Keys:
Unaccepted Keys:
Rejected Keys:
// 进行ping通测试
[root@master ~]# salt 'node1' test.ping
node1:
True