【问题标题】:Ansible: "Failed to connect to the host via ssh" errorAnsible:“无法通过 ssh 连接到主机”错误
【发布时间】:2016-05-24 22:41:43
【问题描述】:

我正在尝试第一次设置 Ansible,以连接到 Raspberry Pi。按照official 'getting started' 的步骤,我制作了一个库存文件:

192.168.1.206

.. 但 ping 失败如下:

$ ansible all -m ping -vvv
No config file found; using defaults
<192.168.1.206> ESTABLISH SSH CONNECTION FOR USER: pi
<192.168.1.206> SSH: EXEC ssh -C -q -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=pi -o ConnectTimeout=10 -o ControlPath=/Users/username/.ansible/cp/ansible-ssh-%h-%p-%r 192.168.1.206 '/bin/sh -c '"'"'( umask 22 && mkdir -p "` echo $HOME/.ansible/tmp/ansible-tmp-1464128959.67-131325759126042 `" && echo "` echo $HOME/.ansible/tmp/ansible-tmp-1464128959.67-131325759126042 `" )'"'"''
192.168.1.206 | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh.",
    "unreachable": true
}

这看起来与this question 相同,但添加密码/用户位对我没有任何影响,不需要 ping,而且无论如何不在官方示例中。无论如何,我更愿意将 Ansible 配置为使用特定的公钥/私钥对(根据 ssh -i ~/.ssh/keyfile 方法..)

感谢您的帮助。


哦,是的,树莓派在那个地址有售:

$ ping 192.168.1.206
PING 192.168.1.206 (192.168.1.206): 56 data bytes
64 bytes from 192.168.1.206: icmp_seq=0 ttl=64 time=83.822 ms

【问题讨论】:

  • 您是否测试过无需密码或使用您在相应变量中提供的密码即可通过 ssh 访问它?
  • 是的 ssh 可以正常工作,无论是使用密码还是 -i ~/.ssh/keyfile 方法。
  • 那么你就知道 ssh 有效,而 ssh -C -q -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=pi -o ConnectTimeout=10 -o ControlPath=/Users/username/.ansible/cp/ansible-ssh-%h-%p-%r 无效。是时候进行二分搜索了,虽然我注意到控制路径用户与 ssh 命令中的用户不匹配。
  • 你的意思是pi 不匹配username?如果是这样,当我在 /etc/ansible/hosts 中指定 username 时,我会得到 &lt;192.168.1.206&gt; ESTABLISH SSH CONNECTION FOR USER: username,但后面会出现一条相同的错误消息。
  • 我所说的“二分搜索”是否不清楚?您需要使用 Ansible 的 ssh 命令,验证它不能按原样工作,然后开始尝试一次删除一个参数,看看这里的肇事者是什么。我知道这实际上不是二分搜索,但我只是在尝试一些幽默。

标签: ssh ansible


【解决方案1】:

尽管它的名字可以暗示,Ansible ping 模块不会发出 ICMP ping。

它会尝试连接到主机并确保安装了兼容版本的 Python(如 documentation 中所述)。

ping - 尝试连接到主机,验证可用的 python 并在成功时返回 pong。

如果您想使用特定的私钥,可以在您的库存文件中指定ansible_ssh_private_key_file

[all]
192.168.1.206 ansible_ssh_private_key_file=/home/example/.ssh/keyfile

【讨论】:

    【解决方案2】:

    它对我有用。

    10.23.4.5 ansible_ssh_pass='password' ansible_user='root'

    【讨论】:

      【解决方案3】:

      你也可以通过在调试模式下执行ssh来排查问题,并在运行时比较结果:

      ssh -v pi@192.168.1.206
      

      与:

      ansible all -m ping -vvvv
      

      【讨论】:

        猜你喜欢
        • 2023-03-20
        • 2017-08-08
        • 2017-02-08
        • 2018-07-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-27
        • 1970-01-01
        相关资源
        最近更新 更多