Ansible ping test :

Issue:

I was in the middle of setting up Ansible on my hosts and as first step tried the ansible ping command and it failed , not a good start :-0(

[vagrant@anmaster ~]$ ansible all -m ping -i inventory.txt
192.168.56.112 | UNREACHABLE! => {
    "changed": false, 
    "msg": "Failed to connect to the host via ssh: Permission denied (publickey,gssapi-keyex,gssapi-with-mic).\r\n", 
    "unreachable": true
}

Let's try the ping and see if the ping works :

[vagrant@anmaster ~]$ ping 192.168.56.112
PING 192.168.56.112 (192.168.56.112) 56(84) bytes of data.
64 bytes from 192.168.56.112: icmp_seq=1 ttl=64 time=0.313 ms
64 bytes from 192.168.56.112: icmp_seq=2 ttl=64 time=0.495 ms
64 bytes from 192.168.56.112: icmp_seq=3 ttl=64 time=0.313 ms
^C
--- 192.168.56.112 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2004ms
rtt min/avg/max/mdev = 0.313/0.373/0.495/0.088 ms

The ping goes fine but the ansible ping has issues, 

Resolution

The Ansible connectivity is based on ssh and in order to resolve this error we need to perform the following :

Create a Public/Private Key for anmaster (Ansible Master Host)

[vagrant@anmaster .ssh]$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/vagrant/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/vagrant/.ssh/id_rsa.
Your public key has been saved in /home/vagrant/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:OqjJybYwACtW6CzJ7S5GaCQ97sKJmr7SdjJ1zDZ+tNQ vagrant@anmaster
The key's randomart image is:
+---[RSA 2048]----+
|                 |
|  .              |
|.o .             |
|*+=              |
|X* o o  S.       |
|*oo ..*.o E      |
|Bo.o.+o+ .       |
|+@O+. ..o        |
|B*X*   .         |
+----[SHA256]-----+

 

Copy the Public Key to Authorized keys onto the Remote Server

[vagrant@anmaster .ssh]$ ssh-copy-id 192.168.56.112
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/vagrant/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
vagrant@192.168.56.112's password: 

Number of key(s) added: 1
Now try logging into the machine, with:   "ssh '192.168.56.112'"
and check to make sure that only the key(s) you wanted were added.


After adding the ssh keys , you will notice that the passwordless communication works and then we test the ansible connectivity

[vagrant@anmaster ~]$ ansible all -m ping -i inventory.txt
192.168.56.112 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}

Happy Learning !!!

DB Alchemist Academy

*****************************************************************************************************************************************************************

Folks:

Please feel free to join our YouTube Channel (DB Alchemist Academy)  for fortnightly videos on the following Topics

• Oracle / MySql Database

• Oracle Cloud

• Exadata

• AWS Cloud

• Docker

• DevOps

• Free/Discounted Udemy Coupons

Please Subscribe and remember to press the Notification Button.

Happy Learning !!!!!

*****************************************************************************************************************************************************************

Share you comments

{{ errorMessage }}