用ansible工具建立 试试通道互信,实现无密码访问,只需要以下几步,就可以轻松实现ssh互信通道的建立。
1)在本地机器执行
ansible dbs -e 'ansible_host_key_checking=False' -m command -a "ssh-keygen -t rsa -f ~/.ssh/id_rsa -N ''" -f 3 #自动生成本地known_hosts内容,远程机器则没有
其中: -f 3 ansible的并行度3
2)用ansible把所有机器的ssh的公钥copy到本地authorized_keys
ansible dbs -m command -a 'cat /home/oracle/.ssh/id_rsa.pub' 2>/dev/null -f 3|grep -v 'CHANGED' >> /home/oracle/.ssh/authorized_keys
其中: chmod 600 /home/oracle/.ssh/authorized_keys 赋权600
3)扫描个主机sshkey,追加到/home/oracle/.ssh/known_hosts
ssh-keyscan dbnode-01,dbnode-02,dbnode-03 >> /home/oracle/.ssh/known_hosts
【注】主机很多的情况下,可以用shell for循环脚本实现
4)用ansible把本地的authorized_keys,known_hosts分发到其他机器上
ansible dbs --limit '!dbnode-03' -m copy -a 'src=/home/oracle/.ssh/authorized_keys dest=/home/oracle/.ssh/ mode=preserve' -f 2
ansible dbs --limit '!dbnode-03' -m copy -a 'src=/home/oracle/.ssh/known_hosts dest=/home/oracle/.ssh/ mode=preserve' -f 2
其中:
--limit '!dbnode-03' 排除本地机器执行
mode=preserve 保留文件权限信息
【完】
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/9606353/viewspace-3015699/ ,如需转载,请注明出处,否则将追究法律责任。