Gather Facts failed due resolve hostname inventory_hostname

Hello Team,

I am trying to run a playbook to gather facts from a nexus switch but playbook run throws an error: ‘Failed to resolve hostname inventory_hostname’

However, in the debug message, I can see the inventory_hostname.

Here is my host file:

[all]

NEXUS3172P ansible_ssh_host=10.131.88.10 ansible_network_os=nxos username=xxxx password=xxxxx

Below is the section of the tasks where I am getting error:

  • name: ‘{{ msg_block }} Set CLI parameters’
    set_fact:
    cli:
    host: ‘{{ inventory_hostname }}’
    username: ‘{{ username }}’
    password: ‘{{ password }}’
    timeout: 21600
    no_log: true

  • name: ‘{{ msg_block }} Gather hardware info’
    block:

  • name: ‘{{ msg_block }} Gather hardware info’
    nxos_facts:
    gather_subset: hardware
    provider: ‘{{ cli }}’
    register: device_facts

[bkb1321@ansible-server playbooks]$ ansible-playbook image_copy_nexus.yaml -i ../hosts

Enter your finastra email: hh

PLAY [Copy image to network device] ***********************************************************************************************************************

TASK [debug] **********************************************************************************************************************************************

ok: [NEXUS3172P] => {

“inventory_hostname”: “NEXUS3172P”

}

TASK [NEXUS3172P Normalize variables] *********************************************************************************************************************

ok: [NEXUS3172P]

TASK [NEXUS3172P Debug normalized variables] **************************************************************************************************************

skipping: [NEXUS3172P]

TASK [NEXUS3172P image_copy_cisco_nexus.yaml] *************************************************************************************************************

included: /etc/ansible/playbooks/play_image_copy_nexus.yaml for NEXUS3172P

TASK [NEXUS3172PSet CLI parameters] ***********************************************************************************************************************

ok: [NEXUS3172P]

TASK [NEXUS3172P Gather hardware info] ********************************************************************************************************************

fatal: [NEXUS3172P]: FAILED! => {“ansible_facts”: {“discovered_interpreter_python”: “/usr/bin/python3”}, “changed”: false, “msg”: “ssh connection failed: ssh connect failed: Failed to resolve hostname inventory_hostname (Name or service not known)”}

TASK [set_fact] *******************************************************************************************************************************************

ok: [NEXUS3172P]

Any help is much appreciated.

Thank you.

Bikram

I believe the message is about DNS resolution, if the inventory_hostname is not DNS resolvable, try setting the ip or resolvable name in the ansilbe_host variable in that host’s scope

Also, does your nxos_facts task need to “delegate_to: localhost” and use an API to talk to the “inventory_hostname” target?

Walter

not if using nxos connection plugin, as it does similar thing in the
background w/o needing delegation.

Hi Brian,

Even I tried with the following in host file but the error is same:

[all]

10.131.88.10 ansible_ssh_host=10.131.88.10 ansible_network_os=nxos username=xxxx password=xxxxx

It seems ansible is treating ‘inventory_hostname’ as a hostname.

In fact it knows the inventory_hostname as you see from my playbook-run debug output.

Thanks

Bikram

by default, ansilbe will use inventory_hostname as the hostname, only
if ansilbe_host is present, will it use that.

In any case, ansible itself does not 'resolve' names, it passes it to
the connection (in this case ssh) which seems to complain that it is
not resolvable.

This is really weird as I can ssh to the host with IP and Hostname.
Even though I set ansible_host=10.131.88.10 I am still getting the same error.

Thanks
Bikram

In your first post you were setting ansible_ssh_host=10.131.88.10, not ansible_host.

Both are ansible_host and ansible_ssh_host are valid. See
https://docs.ansible.com/ansible/latest/collections/ansible/builtin/ssh_connection.html#parameter-remote_user

https://docs.ansible.com/ansible/latest/collections/ansible/builtin/ssh_connection.html#parameter-remote_user

Errata

https://docs.ansible.com/ansible/latest/collections/ansible/builtin/ssh_connection.html#parameter-host

Yeah that’s right. ansible_host and ansible_ssh_host are the same. I tried both options. Weird thing is complaining about dns resolution even after using ip address.

Thanks

*ansible_host* and *ansible_ssh_host* are the same when you use
*ansible.builtin.ssh* connection plugin. See
https://docs.ansible.com/ansible/latest/collections/ansible/builtin/ssh_connection.html#parameter-host

In your case, *ansible_network_os=nxos* indicates that you use
*ansible.netcommon.network_cli* connection plugin. This plugin
doesn't recognise *ansible.builtin.ssh*. See
https://docs.ansible.com/ansible/latest/collections/ansible/netcommon/network_cli_connection.html#parameter-host

Try to isolate your problem:

* Create the inventory

cat hosts

[test]
NEXUS3172P

[test:vars]
ansible_host=10.131.88.10
ansible_network_os=nxos
username=xxxx
password=xxxxx

* Create a simple playbook

cat pb.yml

- hosts: NEXUS3172P
  gather_facts: false
  tasks:
    - nxos_facts:
        gather_subset: hardware

* Debug the connection (-vvvv)

Thanks a lot Vladimir. I really appreciate your help.

With the following combination it works:
ansible_host and connection=ansible.netcommon.network_cli

Previous setting was:

ansible_host and connection=local [which actually worked on ansible version 2.9.11 but my current version is 2.13.7]

-Bikram

sorry, got mislead by the error, its quite literally trying to resolve
`inventory_hostname` not it's value, but the string itself. This was a
bug in several connection plugins, but both core Ansible and the
plugins were patched to bypass it, try upgrading core, the collection
or both.