salt-ssh管理
通过使用用户名密码的SSH实现远程管理
// 在 master 上安装 salt-ssh
[root@master ~]# yum -y install salt-ssh
[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
在配置文件里面写入以下内容:
minion:
host: 192.168.147.130
user: root
passwd: 123
// 安装yum源
[root@master salt]# salt-ssh 'minion' state.sls init.yum.main
// 写脚本取出ip
[root@master ~]# cat test.sh
#!/bin/bash
while read line;do
cat >> abc << EOF
minion$(echo $line | awk '{print $1}'):
host: $(echo $line | awk '{print $2}')
user: root
passwd: 1
EOF
done < host.info
免密登录的方式
[root@master .ssh]# 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:zpvPZDJqIS4zkLtQ+X+UWKzEm0aZj5Q9JHLWYQWrDq4 root@master
The key's randomart image is:
+---[RSA 3072]----+
| .++. |
| . +.o. |
| = O. |
| . O.= |
| .o .+.OSo |
|o. o.oOo+ |
|.o .oo.o= o |
|o +......O |
|..E+ .o.o.o |
+----[SHA256]-----+
[root@master .ssh]# ls
config id_rsa id_rsa.pub known_hosts
[root@master ~]# ssh-copy-id root@192.168.147.130
/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.147.130's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.147.130'"
and check to make sure that only the key(s) you wanted were added.
//配置文件的账户密码删除
[root@master .ssh]# vim /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
minion:
host: 192.168.147.130
通过salt-ssh初始化系统安装salt-minion
// 安装 salt-ssh
[root@master ~]# yum -y install salt-ssh
// 执行状态命令,初始化系统,安装salt-minion
/yum源
[root@master yum]# pwd
/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'
// 修改epel8的key
[root@master files]# vim epel-8.repo
......
enabled=1
gpgcheck=1
countme=1
gpgkey=https://mirrors.aliyun.com/epel/RPM-GPG-KEY-EPEL-8 #添加此行
#gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-8 #添加注释
.......此处省略
// 执行安装minion
[root@master salt-minion]# pwd
/srv/salt/base/init/salt-minion
[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
- require:
- pkg: salt-minion
salt-minion.service:
service.running:
- enable: true
- reload: true
- watch:
- file: /etc/salt/minion
[root@master files]# pwd
/srv/salt/base/init/salt-minion/files
[root@master files]# vim minion.j2
.......
#master: salt
master: {{ pillar['master_ip'] }} #定义成变量
......
//在pillar定义变量
[root@master base]# pwd
/srv/pillar/base
[root@master base]# cat salt-minion.sls
master_ip:192.168.147.129
[root@master base]# cat top.sls
base:
'*':
- salt-minion
//执行
[root@master files]# salt-ssh '*' state.sls init.salt-minion.main