Error message “Failed to connect to the host via ssh” while running ping command on host machines using Ansible

+1 vote

I tried to connect to machines and ran a ping command on nodes using ansible. I have been getting the the below message.

command: ansible -i inventory all -m ping

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 had stated the private key file and the remote user name in ansible.cfg file.

Apr 30, 2018 in Ansible by Atul
• 10,240 points
Are you doing this on a Linux distro?
Yess, both host and server on Ubuntu
Are you able to ssh into your host from the server?
No I'm not! Do I have to first create an ssh connection?
Yes obviously, create an ssh connection first

3 answers to this question.

+1 vote

Just read the below error message:

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

Ansible can't be able to resolve the centosinstance hostname.

Modify your inventory like this:

centosinstance ansible_ssh_host=
centosinstance1 ansible_ssh_host=

Change the ip addresses with the ip addresses of your machines and try one more time.

Hope that help it will resolve your problem.

answered May 1, 2018 by shubham
• 6,910 points
+1 vote
Check if you've added hostnames in the inventory file.
answered Apr 23, 2019 by Keshav
+1 vote
I hadn't created the ssh connection between my hosts and the servers. I created an ssh key on my server, copied that to my hosts and the error disappeared.
answered Apr 23, 2019 by Lavanya

Related Questions In Ansible

+15 votes
2 answers

Git management technique when there are multiple customers and need multiple customization?

Consider this - In 'extended' Git-Flow, (Git-Multi-Flow, ...READ MORE

answered Mar 26, 2018 in DevOps & Agile by DragonLord999
• 8,450 points
+2 votes
1 answer
0 votes
1 answer
0 votes
1 answer

How do I execute a shell script and then use the result in ansible

This can work out for you: - name: ...READ MORE

answered Apr 11, 2018 in Ansible by shubham
• 6,910 points