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@wyt1 ~]# yum -y install salt-ssh
2.1 通过使用用户名密码的SSH实现远程管理
修改配置文件,添加受控机信息
[root@wyt1 ~]# vim /etc/salt/roster
wyt2:
host: 192.168.232.129
user: root
passwd: 123456
测试连通性
[root@wyt1 ~]# salt-ssh '*' test.ping
wyt2:
----------
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.232.129 (192.168.232.129)' can't be established.
ECDSA key fingerprint is SHA256:lsDQSzU3iqXVSwdfiNTSwfpS7PfVunpJoCDNIIqw444.
ECDSA key fingerprint is MD5:c8:f4:64:57:48:d4:9c:96:ae:cc:fa:7d:f9:11:b3:da.
Are you sure you want to continue connecting (yes/no)?
第一次访问时需要输入 yes/no ,但是 saltstack 是不支持交互式操作的,所以为了解决这个问题,我们需要对其进行设置,让系统不进行主机验证。
[root@wyt1 ~]# vim ~/.ssh/config
StrictHostKeyChecking no
[root@wyt1 ~]# salt-ssh '*' test.ping
wyt2:
True
2.2 通过salt-ssh初始化系统安装salt-minion
在master上安装salt-ssh
[root@wyt1 ~]# yum -y install salt-ssh
修改配置文件,添加受控机信息
[root@wyt1 ~]# vim /etc/salt/roster
wyt2:
host: 192.168.232.129
user: root
passwd: 123456
测试连通性
[root@wyt1 ~]# salt-ssh '*' test.ping
wyt2:
True
创建salt源和文件的存放目录,并拷贝文件
[root@wyt1 ~]# yum -y install salt-master
[root@wyt1 ~]# vim /etc/salt/master
file_roots:
base:
- /srv/salt/base
[root@wyt1 ~]# systemctl start master
[root@wyt1 ~]# mkdir -p /srv/salt/base/{repo,files}
[root@wyt1 base]# cp /etc/yum.repos.d/salt-3000.repo /srv/salt/base/repo/salt-3000.repo
[root@wyt1 base]# cp /etc/salt/minion /srv/salt/base/files/
[root@wyt1 base]# vim files/minion
master: {{ IP }}
配置sls状态文件
[root@wyt1 ~]# vim /srv/salt/base/repo.sls
salt-repo:
file.managed:
- name: /etc/yum.repos.d/salt-3000.repo
- source: salt://repo/salt-3000.repo
- user: root
- group: root
- mode: 644
[root@wyt1 ~]# 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:
IP:192.168.232.128
- require:
- pkg: salt-minion-install
salt-minion-service:
service.running:
- name: salt-minion
- enable: True
- start: True
- watch:
- file: /etc/salt/minion
[root@wyt1 ~]# vim /srv/salt/base/top.sls //高级状态文件
base:
'wyt2':
- repo
- minion
执行高级状态文件
[root@wyt1 base]# salt-ssh 'wyt2' state.highstate
//在minion端查看状态
[root@wyt2 ~]# systemctl status salt-minion
● salt-minion.service - The Salt Minion
Loaded: loaded (/usr/lib/systemd/system/salt-minion.service; enabled; vendor preset: disabled)
Active: active (running) since Tue 2020-08-25 10:23:24 EDT; 1min 18s ago
Docs: man:salt-minion(1)
file:///usr/share/doc/salt/html/contents.html
https://docs.saltstack.com/en/latest/contents.html
Main PID: 129738 (salt-minion)
CGroup: /system.slice/salt-minion.service
├─ 35316 /usr/bin/java -Djava.util.logging.config.file=...
├─129738 /usr/bin/python /usr/bin/salt-minion
├─129742 /usr/bin/python /usr/bin/salt-minion
└─129745 /usr/bin/python /usr/bin/salt-minion
Aug 25 10:23:24 wyt2 systemd[1]: Stopped The Salt Minion.
Aug 25 10:23:24 wyt2 systemd[1]: Starting The Salt Minion...
Aug 25 10:23:24 wyt2 systemd[1]: Started The Salt Minion.
Aug 25 10:23:24 wyt2 salt-minion[129738]: /usr/lib/python2.7/site-...
Hint: Some lines were ellipsized, use -l to show in full.
[root@wyt2 ~]# grep '192.168.232.128' /etc/salt/minion
master: 192.168.232.128
在master上接受指定minion的新证书
[root@wyt1 base]# salt-key -L
Accepted Keys:
Denied Keys:
Unaccepted Keys:
wyt2
Rejected Keys:
[root@wyt1 base]# salt-key -ya 'wyt2'
The following keys are going to be accepted:
Unaccepted Keys:
wyt2
Key for minion wyt2 accepted.
[root@wyt1 base]# salt-key -L
Accepted Keys:
wyt2
Denied Keys:
Unaccepted Keys:
Rejected Keys:
使用salt命令测试连通性
[root@wyt1 ~]# salt 'wyt2' test.ping
wyt2:
True