Gather Facts failed due resolve hostname inventory_hostname

364 views
Skip to first unread message

Bikram

unread,
Jun 1, 2023, 2:43:38 PM6/1/23
to ansible...@googlegroups.com
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 }}'
      timeout21600
  no_logtrue

name'{{ msg_block }} Gather hardware info'
  block:
    - name'{{ msg_block }} Gather hardware info'
      nxos_facts:
        gather_subsethardware
provider: '{{ cli }}'
      registerdevice_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

Brian Coca

unread,
Jun 1, 2023, 2:48:12 PM6/1/23
to ansible...@googlegroups.com
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

----------
Brian Coca

Rowe, Walter P. (Fed)

unread,
Jun 1, 2023, 2:57:14 PM6/1/23
to ansible...@googlegroups.com
Also, does your nxos_facts task need to "delegate_to: localhost" and use an API to talk to the "inventory_hostname" target?

Walter
--
Walter Rowe, Division Chief
Infrastructure Services, OISM
Mobile: 202.355.4123

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CACVha7do3KXQL1LBs8uL%2BXOyFMMMhkE2WiTFVQtS3ZqVpBSJew%40mail.gmail.com.

Brian Coca

unread,
Jun 1, 2023, 3:01:15 PM6/1/23
to ansible...@googlegroups.com
not if using nxos connection plugin, as it does similar thing in the
background w/o needing delegation.

On Thu, Jun 1, 2023 at 2:57 PM 'Rowe, Walter P. (Fed)' via Ansible
Project <ansible...@googlegroups.com> wrote:
>
> Also, does your nxos_facts task need to "delegate_to: localhost" and use an API to talk to the "inventory_hostname" target?


--
----------
Brian Coca

Bikram

unread,
Jun 1, 2023, 3:11:35 PM6/1/23
to ansible...@googlegroups.com
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



--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.

Brian Coca

unread,
Jun 1, 2023, 3:41:34 PM6/1/23
to ansible...@googlegroups.com
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.


--
----------
Brian Coca

Bikram

unread,
Jun 1, 2023, 3:57:21 PM6/1/23
to ansible...@googlegroups.com
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

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.

Todd Lewis

unread,
Jun 1, 2023, 4:49:52 PM6/1/23
to ansible...@googlegroups.com, uto...@gmail.com
In your first post you were setting ansible_ssh_host=10.131.88.10, not ansible_host.


On 6/1/23 3:56 PM, Bikram wrote:
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.


-- 
Todd

Vladimir Botka

unread,
Jun 1, 2023, 5:47:29 PM6/1/23
to Todd Lewis, ansible...@googlegroups.com
On Thu, 1 Jun 2023 16:49:39 -0400
Todd Lewis <uto...@gmail.com> wrote:

> 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


--
Vladimir Botka

Vladimir Botka

unread,
Jun 1, 2023, 5:49:42 PM6/1/23
to Todd Lewis, ansible...@googlegroups.com

Bikram

unread,
Jun 1, 2023, 6:13:00 PM6/1/23
to ansible...@googlegroups.com, Todd Lewis
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

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.

Vladimir Botka

unread,
Jun 1, 2023, 6:39:17 PM6/1/23
to Bikram, ansible...@googlegroups.com
*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

--
Vladimir Botka

Vladimir Botka

unread,
Jun 1, 2023, 7:01:30 PM6/1/23
to Bikram, ansible...@googlegroups.com
Try to isolate your problem:

* Create the inventory

shell> cat hosts
[test]
NEXUS3172P

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

* Create a simple playbook

shell> cat pb.yml
- hosts: NEXUS3172P
gather_facts: false
tasks:
- nxos_facts:
gather_subset: hardware

* Debug the connection (-vvvv)

shell> ansible-playbook -i hosts -vvvv pb.yml


--
Vladimir Botka

Bikram

unread,
Jun 1, 2023, 7:10:52 PM6/1/23
to Vladimir Botka, ansible...@googlegroups.com
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

Brian Coca

unread,
Jun 2, 2023, 11:43:50 AM6/2/23
to ansible...@googlegroups.com
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.

--
----------
Brian Coca

Reply all
Reply to author
Forward
0 new messages