【问题标题】:Ansible 'Failed to connect to the host via ssh'Ansible '无法通过 ssh 连接到主机'
【发布时间】:2017-08-08 09:57:30
【问题描述】:

我知道这已经被问了很多,但我已经尝试了我在网络上看到的所有解决方案,比如 20 个线程,但没有任何一个对我有用。

我在使用 ansible 2.1.1.0 的 Ubuntu 16.10 Linux 本地计算机上尝试 ping (ansible all -m ping) 我在 Ubuntu AWS EC2 实例上的 Ansible 子节点。

我的 /etc/ansible/hosts 有 EC2 的公共 IP:

web1 ansible_host=52.91.x.y

我可以通过 SSH 连接到 EC2 实例:

ssh -i "mypemfile.pem" ubuntu@52.91.x.y

所以我知道安全组/网络/等。在实例上很好。

但是当我尝试 ping 子节点时:

ansible all -m ping

我明白了:

Failed to connect to the host via ssh

当我通过 -vvvv 显示完整命令时:

ssh -C -vvv -o ControlMaster=auto -o ControlPersist=60s -o 'IdentityFile="/home/myuser/.ssh/id_rsa.pub"' -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/home/myuser/.ansible/cp/ansible-ssh-%h-%p-%r 52.91.x.y

然后运行 ​​Ansible 正在运行的命令,在详细输出的末尾,它显示:

debug1: Offering RSA public key: /home/myuser/.ssh/id_rsa.pub
debug3: send_pubkey_test
debug3: send packet: type 50
debug2: we sent a publickey packet, wait for reply
debug3: receive packet: type 51
debug1: Authentications that can continue: publickey
debug1: Offering RSA public key: Downloads/nginx-code-challenge-2017-03-16.pem
...skip some stuff
debug1: No more authentication methods to try.
Permission denied (publickey).

它从我的本地(Ansible 主节点)系统尝试了一堆不同的密钥,但没有成功。

我确保我的主节点的公钥在子节点的.ssh/authorized_keys 中。当我执行 ssh-copy-id 时,它会确认这一点,说密钥已被添加。

我尝试在 /etc/ansible/ansible.cfg 中指定我的私钥的路径,但这没有任何作用,而且我已经在调试输出中看到它正在尝试多个密钥。

我已经尝试添加-c paramiko 命令:

ansible all -m ping -vvvv -c paramiko

但我得到的只是一个新错误,Authentication failed

我什至在 ansible.cfg 中尝试过像 host_key_checking = False 这样疯狂的东西

帮助?

【问题讨论】:

    标签: ssh amazon-ec2 connection ansible


    【解决方案1】:

    您的手动 SSH 登录使用的是正确的身份文件(*.pem 文件)

    ssh -i "mypemfile.pem" ubuntu@52.91.x.y
    

    但是 Ansible 正在访问您的公钥文件并将其用作私有身份文件:

     'IdentityFile="/home/myuser/.ssh/id_rsa.pub"'
    

    IdentityFile 参数需要一个私钥文件。你会希望它也指向“mypemfile.pem”。

    建议

    将远程虚拟机添加到 ~/.ssh/config 中的 SSH 配置文件

      Host ec2
           HostName  something.compute.amazonaws.com 
           User ec2-user
           IdentityFile /home/me/.ssh/mypemfile.pem
    

    然后你的 Ansible 主机文件就变得简单多了:

     [EC2]
     dev ansible_ssh_host=ec2
    

    【讨论】:

    • 感谢您的回复;但是,它也在尝试那个键。 :-( 在输出中它有“debug1:提供 RSA 公钥:Downloads/nginx-code-challenge-2017-03-16.pem”,这是我的 AWS 实例的私钥
    • 我什至试过 --private-key="path_to_aws_key" 没有运气
    • 我会试试你的配置文件选项..等等
    • 拥有那个 .ssh/config 文件似乎没有任何区别
    【解决方案2】:

    解决方案最终变得非常简单。我在 /etc/ansible/hosts (ansible_ssh_user=ubuntu 部分)中缺少用户:

    [nginx]
    web1 ansible_ssh_host=52.91.x.y ansible_ssh_user=ubuntu
    

    【讨论】: