The ansible ad-hoc command can use the ping module to ping another system. Here is how you would use the ansible command to ping one managed nodes.
AVOID TROUBLE
In this example, the hostname of the managed node must be followed by a single comma.
ansible all -i server1.example.com, --module-name ping
And here is how you could ping two or more other systems.
ansible all -i server1.example.com,server2.example.com,server3.example.com --module-name ping
Let's say you have the following managed nodes defined in the default hosts file or your own inventory file.
windows:
hosts:
server1.example.com:
server2.example.com:
linux:
hosts:
server3.example.com:
server4.example.com:
Here is how you would use the ansible command to ping all of the managed nodes.
ansible all -i inventory.yml --module-name ping
If all of the hosts are reachable, something like this should be returned.
server1.example.org | SUCCESS => {
"changed": false,
"ping": "pong"
}
server2.example.org | SUCCESS => {
"changed": false,
"ping": "pong"
}
server3.example.org | SUCCESS => {
"changed": false,
"ping": "pong"
}
server4.example.org | SUCCESS => {
"changed": false,
"ping": "pong"
}
Let's say the following is returned.
server1.example.com | UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: ssh: Could not resolve hostname server1.example.com: Name or service not known",
"unreachable": true
}
This suggests that the managed nodes hostname (server1.example.com) cannot be resolved to an IP address, which you can confirm using the nslookup command.
~]# nslookup server1.example.com
Server: 192.168.0.6
Address: 192.168.0.6#53
** server can't find server1.example.com: NXDOMAIN
In this scenario, you would have to consult with the team in your organization that managed DNS (probably the networking team) to determine why DNS is unable to resolve the hostname to an IP address.
If you are just doing local testing, you can add an entry like this to the /etc/hosts file on your Ansible control node to resolve the hostname to an IP address.
10.17.4.56 server1.example.com
Did you find this article helpful?
If so, consider buying me a coffee over at