Ansible免密登陆配置

1:把远程服务器的公钥来获取到本地

ssh-keyscan ip1 ip2 ip3 ip4 >> /root/.ssh/known_hosts

完成后,cat /root/.ssh/known_hosts查看拉取结果

2:使用ssh-key产生公钥和私钥

ssh-keygen -t rsa
一路按enter

3:添加服务IP到 ansible 的 hosts 

cat >> /etc/ansible/hosts << EOF
ip1
ip2
ip3
ip4
EOF

4: 添加免密登录(我的ROOT密码都一样,使用 -k 参数, 然后输入 root 密码)

相同密码主机部署公钥
ansible all -m authorized_key -a "user=root state=present key=\"{{ lookup('file', '/root/.ssh/id_rsa.pub') }} \"" -k

 不同密码主机部署公钥

  ansible all -m authorized_key -a "user=root exclusive=true manage_dir=true key='$(</root/.ssh/authorized_keys)'" -k -v

5: 验证免密登陆

ansible all -m ping

 

posted @ 2020-07-08 16:13  淡若风云  阅读(456)  评论(0)    收藏  举报