通过终端的 SSH 成功,但是通过 Ansible 在同一台机器上建立相同的 SSH 连接失败?

通过终端的 SSH 成功,但是通过 Ansible 在同一台机器上建立相同的 SSH 连接失败?

有人能解释一下为什么会发生这种情况吗?

我以 basicuser 用户身份登录到机器 A(客户端)。当我尝试在机器 B(服务器机器,以 remoteuser 身份)上运行 ansible playbook 时。

  • 1)我的私钥/公钥绝对匹配,正如在客户端 A 上运行的“ssh user@serverB”(作为 basicuser)所证明的那样 - ansible 和 SSH 都指向同一个私钥
  • 2)我的 ~/.ssh/known_hosts 没有过时
  • 3)我的 ~/.ssh 和 ~/.ssh/* 权限正确(分别由 basicuser、chmod 700 拥有)
  • 4)我的 hosts.ini 文件包含所需的“ansible_ssh_user=remoteuser ansible_ssh_private_key_file=~/.ssh/id_rsa”(并且格式正确)

我知道 id_rsa 与 /home/remoteuser/.ssh 中机器 B 的“authorized_keys”文件中的唯一条目匹配。

以下是客户端 A 通过终端进行的 SSH:

basicuser@serverA:~$ ssh remoteuser@serverB
Enter passphrase for key '/home/basicuser/.ssh/id_rsa':

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law.
Last login: Wed Jul 16 23:38:41 2014 from xxx.xxx.xxx.xxx
remoteuser@serverB:~$

以下是我运行 ansible playbook 时发生的情况:http://pastebin.com/6wXgp3dd

我把它放在 pastebin 上,因为它的输出量很大。然而,结尾处的这一部分让我感到很奇怪:

debug2: key: /home/basicuser/.ssh/id_rsa ((nil))
debug1: Authentications that can continue: publickey
debug3: start over, passed a different list publickey
debug3: preferred gssapi-with-mic,gssapi-keyex,hostbased,publickey
debug3: authmethod_lookup publickey
debug3: remaining preferred: ,gssapi-keyex,hostbased,publickey
debug3: authmethod_is_enabled publickey
debug1: Next authentication method: publickey
debug1: Trying private key: /home/basicuser/.ssh/id_rsa
debug1: key_parse_private_pem: PEM_read_PrivateKey failed
debug1: read PEM private key done: type <unknown>
debug1: read PEM private key done: type RSA
debug3: sign_and_send_pubkey: RSA ed:bb:6d:4e:e3:7a:54:0a:19:f6:ee:1c:d0:f2:1b:3f
debug2: we sent a publickey packet, wait for reply
debug1: Authentications that can continue: publickey
debug2: we did not send a packet, disable method
debug1: No more authentication methods to try.
Permission denied (publickey).

我已经花了大约三天时间解决这个问题。为什么终端 SSH 可以工作,而 ansible 却失败了?

我在 /etc/ssh/ssh_config 中没有任何覆盖 - 我需要吗?

答案1

经过多次调整和喝咖啡后,我终于明白自己做错了什么。我的 hosts.ini 和部署文件中的“ansible_ssh_user”参数重复了,但私钥参数没有重复。也就是说,我的 ssh 密钥上没有密码!我不得不输入这个短语太多次了。

感谢大家的见解

相关内容