You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ansible连接Cisco交换机失败:无法解析inventory_hostname

Ansible连接Cisco交换机报错:无法解析hostname inventory_hostname

执行Playbook连接Cisco交换机时,仅收到如下错误:

ssh connect failed: Failed to resolve hostname inventory_hostname

环境信息(已脱敏):

  • 1.1.1.1 = Ansible控制主机
  • 2.2.2.2 = DNS服务器
  • 3.3.3.3 = 目标Cisco交换机
  • domain.local = Ansible主机自动追加的DNS搜索域
  • 使用个人主目录下安装的Ansible 2.14版本

直接SSH连接验证

直接通过SSH可以正常连接目标交换机:

$ ssh ansible@sw1
Password: 
Login successful.
sw1#

清单配置详情

执行ansible-inventory -i hosts.yml --host sw1输出如下:

{
    "ansible_become": true,
    "ansible_become_method": "enable",
    "ansible_connection": "ansible.netcommon.network_cli",
    "ansible_fqdn": "sw1.domain.local",
    "ansible_host": "3.3.3.3",
    "ansible_hostname": "sw1",
    "ansible_network_os": "cisco.ios.ios",
    "ansible_password": "**************",
    "ansible_user": "ansible"
}

测试Playbook(其他Playbook结果一致)

---
# Playbook for sw1

- name: Show Version
  hosts: sw1
  gather_facts: no
  tasks:
    - name: print inventory_hostname
      ansible.builtin.debug:
        var: inventory_hostname
    - name: print ansible_hostname
      ansible.builtin.debug:
        var: ansible_hostname
    - name: print ansible_fqdn
      ansible.builtin.debug:
        var: ansible_fqdn
    - name: print ansible_ssh_host
      ansible.builtin.debug:
        var: ansible_ssh_host

    - name: Print output
      debug:
        msg: "{{ansible_facts}}"

    - name: Gather all legacy facts
      cisco.ios.ios_facts:
        gather_subset: all

    - name: Print output
      debug:
        msg: "{{ansible_facts}}"

Playbook执行输出

ansible-playbook -i hosts.yml show_facts_sw1.yml

PLAY [Show Facts] *************************

TASK [print inventory_hostname] ***********
ok: [sw1] => {
    "inventory_hostname": "sw1"
}

TASK [print ansible_hostname] *************
ok: [sw1] => {
    "ansible_hostname": "sw1"
}

TASK [print ansible_fqdn] *****************
ok: [sw1] => {
    "ansible_fqdn": "sw1.domain.local"
}

TASK [print ansible_ssh_host] *************
ok: [sw1] => {
    "ansible_ssh_host": "VARIABLE IS NOT DEFINED!"
}

TASK [Print output] **********************
ok: [sw1] => {
    "msg": {}
}

TASK [Gather all legacy facts] **********
fatal: [sw1]: FAILED! => {"changed": false, "msg": "ssh connection failed: ssh connect        failed: Failed to resolve hostname inventory_hostname (Temporary failure in name resolution)"}

PLAY RECAP *******************
sw1          : ok=5    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

注:曾在清单中设置ansible_ssh_host变量为IP地址,结果相同


tcpdump抓包结果

抓DNS请求(tcpdump port 53)输出如下:

10:56:56.409051 IP 1.1.1.1.34980 > 2.2.2.2.53: 41061+ A? inventory_hostname.domain.local. (43)
10:56:56.409076 IP 1.1.1.1.34980 > 2.2.2.2.53: 40545+ AAAA? inventory_hostname.domain.local. (43)
10:56:56.409867 IP 2.2.2.2.53 > 1.1.1.1.34980: 41061 NXDomain* 0/1/0 (92)
10:56:56.409906 IP 2.2.2.2.53 > 1.1.1.1.34980: 40545 NXDomain* 0/1/0 (92)

注:tcpdump中显示的inventory_hostname为原始字符串


版本信息

ansible --version
ansible [core 2.14.1]
  config file = /home/user/network/ansible.cfg
  configured module search path = ['/home/user/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/user/.local/lib/python3.9/site-packages/ansible
  ansible collection location = /home/user/.ansible/collections:/usr/share/ansible/collections
  executable location = /home/user/.local/bin/ansible
  python version = 3.9.2 (default, Feb 28 2021, 17:03:44) [GCC 10.2.1 20210110] (/usr/bin/python3)
  jinja version = 3.1.2
  libyaml = True

已尝试的解决措施

  • 将所有主机名变量改为IP地址后再改回,无效果
  • 在组和主机变量中设置ansible_host_dns_name: false关闭DNS解析,无效果
  • 在线查找解决方案,仅找到Ansible特殊变量的相关说明

请问问题出在哪里?已尝试解决多日,无法确定是自身配置错误还是Ansible存在问题。确认不是DNS问题,所有主机名都能正常解析到正确IP地址。


内容的提问来源于stack exchange,提问作者belarion

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 01:40:35