Logo
Section 1 / 9

连接失败

学习如何排查和解决连接失败错误。

导入

连接失败

学习如何排查和解决连接失败错误。

连接相关错误

连接失败错误是 Ansible 中最常见的问题之一,且非常令人困扰。
这类问题的根本原因可能是网络连接、防火墙、某些 SSH 配置参数,甚至是我们 Ansible 仓库中主机名拼写错误。

...

结果是出现致命的连接失败错误,我们通常会在使用 ansible-playbook 命令行工具执行时,在日志中看到“无法连接”或“操作超时”的消息,如下图所示。

...
连接失败错误

在处理虚拟机时,我们需要验证虚拟网卡的连接状态,并确认防火墙中是否启用了 SSH 服务。
手动尝试通过 SSH 连接到目标主机进行测试,对排查连接问题很有帮助。

示例:主机名拼写错误

现在我们来模拟并解决目标主机因主机名拼写错误导致的问题。
假设某主机名为 localhost,但被错误地拼写为 localehost(多了一个 e)。这是一个简单而常见的错误。
同时,错误地使用了 Ansible 的 ansible_connection 变量。
对于 localhost,目标应设置为 local 而非 ssh。ssh 参数使用 OpenSSH 连接,而 local 参数使用本地连接。

以下 Ansible 仓库展示了拼写错误的目标主机名:

1localehost ansible_connection=ssh

我们可以使用下面这个简单的 ping.yml Ansible playbook 来复现该错误。它使用 ansible.builtin.ping 模块测试与目标主机的连接。

1--- 2- name: Test connection 3 hosts: all 4 tasks: 5 - name: Test connection 6 ansible.builtin.ping:

我们可以使用包含在每个 Ansible 安装中的 ansible-playbook 命令执行 ping.yml Ansible playbook。执行 ping.yml playbook 的完整命令如下:

1ansible-playbook -i inventory_error ping.yml

之前 Ansible playbook 的执行输出如下:

ping.yml playbook 的执行结果

1PLAY [Test connection] *************************************************************** 2 3TASK [Gathering Facts] *************************************************************** 4fatal: [localehost]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to 5the host via ssh: ssh: Could not resolve hostname localehost: Name or service not known", 6"unreachable": true} 7 8PLAY RECAP *************************************************************************** 9localehost : ok=0 changed=0 unreachable=1 failed=0 skipped=0 rescued=0 ignored=0

我们的排查工作从使用每个 Ansible 安装都包含的 ansible-inventory 命令验证 Ansible 仓库 inventory_error 开始。完整的列表视图命令如下:

1ansible-inventory -i inventory_error --list

上述命令产生的输出如下:

inventory_error 仓库的列表视图

1{ 2 "_meta": { 3 "hostvars": { 4 "localehost": { 5 "ansible_connection": "ssh" 6 } 7 } 8 }, 9 "all": { 10 "children": [ 11 "ungrouped" 12 ] 13 }, 14 "ungrouped": { 15 "hosts": [ 16 "localehost" 17 ] 18 } 19}

我们可以将文件更正为正确的 localhost 主机名和 local 连接类型,如 inventory 文件所示。

1localhost ansible_connection=local

我们可以使用 ansible-inventory 命令验证 Ansible 仓库 inventory 文件:

1ansible-inventory -i inventory --list

inventory_error 仓库的列表视图

1{ 2 "_meta": { 3 "hostvars": { 4 "localhost": { 5 "ansible_connection": "local" 6 } 7 } 8 }, 9 "all": { 10 "children": [ 11 "ungrouped" 12 ] 13 }, 14 "ungrouped": { 15 "hosts": [ 16 "localhost" 17 ] 18 } 19}

我们得到了正确主机列表,使用了正确的 Ansible 仓库文件。

实验环境

让我们使用下方的实验环境,尝试本课介绍的命令。

  • 使用 inventory_error 仓库文件执行 ping.yml Ansible playbook。

    1ansible-playbook -i inventory_error ping.yml
  • 查看 inventory_error Ansible 仓库。

    1ansible-inventory -i inventory_error --list
  • 使用 inventory 仓库文件执行 ping.yml Ansible playbook。

    1ansible-playbook -i inventory ping.yml
  • 查看 inventory Ansible 仓库。

    1ansible-inventory -i inventory --list
  • 使用 inventory_error 仓库文件执行 ping.yml Ansible playbook。

    1ansible-playbook -i inventory_error ping.yml
  • 查看 inventory_error Ansible 仓库。

    1ansible-inventory -i inventory_error --list
  • 使用 inventory 仓库文件执行 ping.yml Ansible playbook。

    1ansible-playbook -i inventory ping.yml
  • 查看 inventory Ansible 仓库。

    1ansible-inventory -i inventory --list

No configuration found for ID: 8c10b6ae414838a27a344b8a4e0a455939cfc9ba5900a694208271cbcf1f5018

Ansible for Linux

缺少模块参数