Ansible ssh-key密钥认证配置
对于被管理服务器做免密码登录设置
1.在管理服务器生成ssh-key密钥
#ssh-keygen //生成秘钥
root@hsz:/etc/ansible# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
c5:5a:29:c4:d6:8d:f7:2c:46:f8:a3:c1:d8:e9:5b:06 root@hsz
The key's randomart image is:
+--[ RSA 2048]----+
| ... + |
| .o.+.+ |
| ..+== o |
| .=E = o |
| S. = o |
| o o |
| + |
| . |
| |
+-----------------+
#回车回车就行
2.将秘钥拷贝到被管理服务器上
#ssh-copy-id -i ~/.ssh/id_rsa.pub -p 22 root@192.168.0.50
#一般这样的命令就行,即被管理服务器的ssh端口默认
#ssh-copy-id 192.168.0.50
# 那么接下来就可以进行远程操作了(将被管理服务器ip加到清单的情况下)
比如ping:
[root@hsz ~]# ansible 192.168.0.50 -m ping 192.168.0.50 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "ping": "pong" }