因为之后要用ansible,所以,机器之间要做做个互信,写个shell脚本节省人力。下面两个都一样,用哪个都行。
/root目录下运行ssh-key命令,一路回车
ssh-key
SHELL脚本1:
#!/bin/bash
#熊の力量 编写,实现基于KEY验证的脚本方式2,添加互信。
IPLIST="
"
rpm -qa sshpass &> /dev/null || yum install -y sshpass
[ -f /ssh/.ssh/id_rsa] || ssh-keygen -f /root/.ssh/id_rsa -P ''
export SSHPASS=密码
for IP in $IPLIST;do
sshpass -e ssh-copy-id -o StrictHostKeyChecking=no $IP
done
SHELL脚本2:
#!/bin/bash
#熊の力量 编写,实现基于KEY验证的脚本,添加互信。
rpm -qa sshpass &> /dev/null || yum install -y sshpass
[ -f /ssh/.ssh/id_rsa] || ssh-keygen -f /root/.ssh/id_rsa -P ''
export SSHPASS=密码
while read IP;do
sshpass -e ssh-copy-id -o StrictHostKeyChecking=no $IP
done < hosts.list
ansible脚本:
[root@test ansible]# pwd
/etc/ansible
[root@test ansible]# cat playbook/authorized_key.yaml
- name: 互信
ignore_errors: false
gather_facts: false
hosts: rhel8 #/etc/ansible/hosts内写的标签
tasks:
- name: authorized_key
authorized_key:
user: root #需要做互信的用户
state: present #新增公钥内容到服务器用户家目录的.ssh目录的authorized_keys文件
#没有则创建authorized_keys文件state: (1) present 添加 (2) absent 删除
key: "{{ lookup('file', '/root/.ssh/id_rsa.pub') }}"
ansible脚本执行:
ansible-playbook -i hosts playbook/authorized_key.yaml -k
使用-k参数或者如下图文件中写ansible_ssh_pass密码
[root@localhost ~]# cd /etc/ansible/
[root@localhost ansible]# vim hosts
[db]
192.168.10.11 ansible_ssh_user='root' ansible_ssh_pass='123456'
192.168.10.12 ansible_ssh_user='root' ansible_ssh_pass='123456'