06-16-2020 12:50 AM - edited 06-16-2020 12:53 AM
Hello there,
this is my very first post here in the Devnet Community. Hope you are all well..
I am just getting my feet wet with ansible, and keep running into all sorts of issues.
Some of them I could fix myself but thisone really gets me..
I have a ansible 2.9.9 running on Ubuntu 16.04.6 LTS
My host file is rather simple:
[all:vars]
# these defaults can be overridden for any group in the [group:vars] section
ansible_connection=local
ansible_network_os=ios
[hosts]
TEST-HOST ansible_host=10.10.10.4
If i now try to ping this host group it seems to work:
$ ansible hosts -m ping
TEST-HOST | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
So I thought - sweet this is working.. But than i wanted to test further and changed the IP of the test host to somehtign that will NOT respond. i.e. 5.5.5.5
[all:vars]
# these defaults can be overridden for any group in the [group:vars] section
ansible_connection=local
ansible_network_os=ios
[hosts]
TEST-HOST ansible_host=5.5.5.5
If I try to ping 5.5.5.5 from the Ansible server it fails
ping 5.5.5.5
PING 5.5.5.5 (5.5.5.5) 56(84) bytes of data.
^C
--- 5.5.5.5 ping statistics ---
5 packets transmitted, 0 received, 100% packet loss, time 4031ms
But using the ansible ping module this still returns pong :(
$ ansible hosts -m ping
TEST-HOST | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
I tried messing with the connection types, i.e. setting it to local. But that doesnt help.
If I remove the connection type ping doesnt work at all - even againt the correct IP address..
Can you guys push me in the right direction?
Thanks a lot and greetings from Munich
Kibo
06-16-2020 11:32 PM
Hi
Regarding ansible_connection see all the connection types - https://docs.ansible.com/ansible/latest/plugins/connection.html
ansible_connection=local with run the ping on the localhost.
ansible_connection=ssh will target the host in the inventory.
[all:vars] # these defaults can be overridden for any group in the [group:vars] section ansible_connection=ssh ansible_network_os=ios [hosts] TEST-HOST ansible_host=10.10.10.4
I don't have 10.10.10.4 on my network
neteng@neteng-dev:~$ ansible -i hosts all -m ping TEST-HOST | UNREACHABLE! => { "changed": false, "msg": "Failed to connect to the host via ssh: ssh: connect to host 10.10.10.4 port 22: No route to host", "unreachable": true } neteng@neteng-dev:~$
Changed ansible_connection from ssh to local
neteng@neteng-dev:~$ ansible -i hosts all -m ping TEST-HOST | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "ping": "pong" } neteng@neteng-dev:~$
Hope this helps
06-18-2020 01:36 AM
Thanks a lot for your feedback. That did already help me understand this a lot better..
I still run into problems though...
I tried changing the connection to ssh before - but when I did so, I got this SSH error message when trying to use the ping module. $ ansible hosts -m ping
TEST-HOST | UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: ssh: connect to host 10.10.10.4 port 22: Connection timed out",
"unreachable": true
}
When I try to use a net_ping I get this error
ansible hosts -m net_ping
TEST-HOST | FAILED! => {
"changed": false,
"msg": "Connection type ssh is not valid for this module"
}
Thanks a lot again
Kibo
06-20-2020 04:41 AM - edited 06-20-2020 04:44 AM
Hi
Example hosts file with connection local. Test-Host is a reachable Cisco router, Ubuntu is a reachable VM and NoHost doesn't exist on the network. Both Ubuntu and Cisco have ssh enabled and able to ssh with a valid user from cli or putty.
[all:vars] # these defaults can be overridden for any group in the [group:vars] section ansible_connection=local ansible_network_os=ios [hosts] TEST-HOST ansible_host=192.168.106.142 Ubuntu ansible_host=192.168.106.152 NoHost ansible_host=192.168.200.200
Result - all hosts ping:pong. Even NoHost because the connection is local.
neteng@neteng-dev:~$ ansible -i hosts all -m ping TEST-HOST | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "ping": "pong" } NoHost | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "ping": "pong" } Ubuntu | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "ping": "pong" }
Result with connection ssh. All come up Unreachable ... because Ansible is using the currently logged in user "neteng" to connect that is not a valid user configured on the devices.
neteng@neteng-dev:~$ ansible -i hosts all -m ping Ubuntu | UNREACHABLE! => { "changed": false, "msg": "Failed to connect to the host via ssh: neteng@192.168.106.152: Permission denied (publickey,password).", "unreachable": true } TEST-HOST | UNREACHABLE! => { "changed": false, "msg": "Failed to connect to the host via ssh: Warning: Permanently added '192.168.106.142' (RSA) to the list of known hosts.\r\nneteng@192.168.106.142: Permission denied (publickey,keyboard-interactive,password).", "unreachable": true } NoHost | UNREACHABLE! => { "changed": false, "msg": "Failed to connect to the host via ssh: ssh: connect to host 192.168.200.200 port 22: Connection timed out", "unreachable": true }
hosts file with username and password. I have different users .. if the same user has access to both devices, username and password can be given only once under [all:vars]
[all:vars]
ansible_connection=ssh
[hosts] TEST-HOST ansible_host=192.168.106.142 ansible_user=admin ansible_ssh_pass=cisco Ubuntu ansible_host=192.168.106.152 ansible_user=neteng ansible_ssh_pass=neteng NoHost ansible_host=192.168.200.200
Result - Ubuntu is Success as it can ssh with a valid user. Cisco is now Failed vs Unreachable earlier. Cisco router dint recognise bash command - /bin/sh -c '/usr/bin/python
ansible -i hosts all -m ping Ubuntu | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "ping": "pong" } TEST-HOST | FAILED! => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "module_stderr": "Shared connection to 192.168.106.142 closed.\r\n", "module_stdout": "\r\nLine has invalid autocommand \"/bin/sh -c '/usr/bin/python '\"'\"'Line has invalid autocommand \"/bin/sh -c '\"'\"'\"'\"'\"'\"'\"'\"'( umask 77 && mkdir -p \"` echo Line has invalid autocommand \"/bin/sh -c '\"'\"'\"'\"'\"'\"'\"'\"'\"'\"'\"'\"'\"'\"'\"'\"'\"'\"'\"'\"'\"'\"'\"'\"'\"'\"'echo ~admin && sleep 0'\"'\"'\"'\"'\"'\"'\"'\"'\"", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", "rc": 0 } NoHost | UNREACHABLE! => { "changed": false, "msg": "Failed to connect to the host via ssh: ssh: connect to host 192.168.200.200 port 22: Connection timed out", "unreachable": true }
with connection - ssh and using net_ping. All failed because ssh is not valid for net_ping module we need to use local
ansible -i hosts all -m net_ping NoHost | FAILED! => { "changed": false, "msg": "Connection type ssh is not valid for this module" } TEST-HOST | FAILED! => { "changed": false, "msg": "Connection type ssh is not valid for this module" } Ubuntu | FAILED! => { "changed": false, "msg": "Connection type ssh is not valid for this module" }
with connection - local
Ubuntu | FAILED! => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "msg": "missing required arguments: dest" } TEST-HOST | FAILED! => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "msg": "missing required arguments: dest"
net_ping module for network devices requires an additional parameter dest=192.168.106.142.
For multiple hosts we can give dest like this
net_ping: dest: 10.10.10.10
more info - https://docs.ansible.com/ansible/latest/modules/net_ping_module.html
For my one host, I can pass an additional argument to the module from cli using -a dest=192.168.106.142
ansible -i hosts TEST-HOST -m net_ping -a dest=192.168.106.142
TEST-HOST | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "commands": [ "ping 192.168.106.142" ], "packet_loss": "0%", "packets_rx": 5, "packets_tx": 5, "rtt": { "avg": 1, "max": 1, "min": 1 } }
Hope this helps
Please dont forget to hit the Helpful button for any helpful posts.
06-22-2020 01:40 AM
Thanks @omz for your detailed feedback.
Is it just me - Or should this ping module be much more simple.
There should be an easy way to just ping all devince in the hosts file and see if they are up...
Anyway - I am happy to see it's not just me... Your results are similar to my findings/experience.
Bottom line - this is all very confusing - being new to the world of ansible :)
Thanks a bunch.
Kibo
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide