python批量传公钥_使用Ansible批量传送ssh公钥,实现无密码登录

1、免输入yes,修改/etc/ssh/ssh_config配置文件StrictHostKeyChecking no

2、在ansible配置文件中写入需要批量传key的服务器[14.0]

14.1 ansible_ssh_port=22 ansible_ssh_host=10.6.14.1 ansible_ssh_pass='password'

14.2 ansible_ssh_port=22 ansible_ssh_host=10.6.14.2 ansible_ssh_pass='password'

14.3 ansible_ssh_port=22 ansible_ssh_host=10.6.14.3 ansible_ssh_pass='password'

14.4 ansible_ssh_port=22 ansible_ssh_host=10.6.14.4 ansible_ssh_pass='password'

14.5 ansible_ssh_port=22 ansible_ssh_host=10.6.14.5 ansible_ssh_pass='password'

14.6 ansible_ssh_port=22 ansible_ssh_host=10.6.14.6 ansible_ssh_pass='password'

3、ansible命令实现批量下发公钥

pubkey=`cat /root/.ssh/id_rsa.pub`

ansible 14.0 -m shell -a "cd /root/; umask 077; test -d .ssh || mkdir .ssh ; echo $pubkey >> .ssh/authorized_keys"

注意:实际的AuthorizedKeysFile位置应依照对端/etc/ssh/ssh_config配置文件中的值。比如esxi中就不一样AuthorizedKeysFile      /etc/ssh/keys-%u/authorized_keys

所以对端为esxi,则应该执行:ansible 14.0 -m shell -a "cd /root/; umask 077; test -d .ssh || mkdir .ssh ; echo $pubkey >> /etc/ssh/keys-root/authorized_keys"

4、下发完公钥后,ansible配置文件就能去除密码配置项了[14.0]

14.1 ansible_ssh_port=22 ansible_ssh_host=10.6.14.1

14.2 ansible_ssh_port=22 ansible_ssh_host=10.6.14.2

14.3 ansible_ssh_port=22 ansible_ssh_host=10.6.14.3

14.4 ansible_ssh_port=22 ansible_ssh_host=10.6.14.4

14.5 ansible_ssh_port=22 ansible_ssh_host=10.6.14.5

14.6 ansible_ssh_port=22 ansible_ssh_host=10.6.14.6

5、测试一下ansible 14.0 -m ping

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值