ITPub博客

首页 > 自动化运维 > 大规模网络运维 > ansible建立ssh通道互信,实现无密码访问

ansible建立ssh通道互信,实现无密码访问

原创 大规模网络运维 作者:longer3281 时间:2024-05-16 19:45:30 0 删除 编辑

用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/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: 汉诺比塔算法
请登录后发表评论 登录
全部评论
数据库架构师,DB从业经历 DB2,Oracle,MySQL,PGSQL,MonogDB 等数据库,hadoop,hive,spark,flink,doris等大数据技术。

注册时间:2009-05-22

  • 博文量
    69
  • 访问量
    185389