'Getting an error message "Failed to connect to the host via ssh" while trying to run ping command on host machines using Ansible

I am trying to connect to machines and run a ping command on nodes using ansible. I am getting the the below message.

command: ansible -i inventory all -m ping

Message:

centosinstance | UNREACHABLE! => {
    "changed": false, 
    "msg": "Failed to connect to the host via ssh: ssh: Could not resolve hostname centosinstance: Name or service not known\r\n", 
    "unreachable": true
}

I have mentioned the private key file and the remote user name in ansible.cfg file.

What would be the possible reason ?



Solution 1:[1]

Please read the error message:

ssh: Could not resolve hostname centosinstance: Name or service not known

Ansible can't be able to resolve the centosinstance hostname. You need to modify your inventory like this:

centosinstance ansible_ssh_host=192.168.1.10
centosinstance1 ansible_ssh_host=192.168.1.11

Please change the ip addresses with the ip addresses of your machines and try again. Hope that help you

Solution 2:[2]

I was also getting the same error However for me its just typo error like instead of ansible_host i typed ansible_hosts, i.e added an "s" to host.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Arbab Nazar
Solution 2 varun