Monday, April 18, 2022

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

Issue

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

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



Answered By - Arbab Nazar
Answer Checked By - Marilyn (WPSolving Volunteer)