1
- name: traceroute   
  shell: traceroute {{ my_ip }}    
  register: result    
- debug: msg="{{ result.stdout_lines }}"

I have added this in my playbook to test traceroute but task is not stopped , it is not showing output. How to stop and display the output after certain time when we are using ping and traceroute.

I have used async and poll but same issue

β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83

2 Answers2

1

Per default ping doesn't end until you CTRL^C it.

But you can run it with the argument -c to give it a count of ping to send to the server:

$ ping -c5 google.com
PING google.com (216.58.201.238) 56(84) bytes of data.
64 bytes from google.com (216.58.201.238): icmp_seq=1 ttl=50 time=14.8 ms
64 bytes from google.com (216.58.201.238): icmp_seq=2 ttl=50 time=14.8 ms
64 bytes from google.com (216.58.201.238): icmp_seq=3 ttl=50 time=14.3 ms
64 bytes from google.com (216.58.201.238): icmp_seq=4 ttl=50 time=14.7 ms
64 bytes from google.com (216.58.201.238): icmp_seq=5 ttl=50 time=15.3 ms

--- google.com ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4020ms
rtt min/avg/max/mdev = 14.329/14.826/15.373/0.357 ms

Also see: https://linux.die.net/man/8/ping

Quite the same applies to traceroute that will try 30 hops per default.
It will end, but after quite a long amount of time.
Try to limit this with the option -m.

$ traceroute -m5 google.com
traceroute to google.com (216.58.201.238), 5 hops max, 60 byte packets
 1  x.x.x.x (x.x.x.x)  0.452 ms  0.427 ms  0.145 ms
 2  x.x.x.x (x.x.x.x)  1.169 ms  0.873 ms  1.641 ms
 3  * * *
 4  * * *
 5  * * *

Also see: https://linux.die.net/man/8/traceroute

β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
0

As previously answered, you need to limit the amount of packets, you can try these 2 tasks:

  - name: Execute ping
    shell: "ping -c 4 {{ my_ip }}"
    register: result

  - name: Show result
    debug:
      msg: "{{ result['stdout_lines'] }}"

Keep in mind the "my_ip" variable was taken from a different file. After these tasks finished, I got the following output:

    "msg": [
        "PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.",
        "64 bytes from 8.8.8.8: icmp_seq=1 ttl=54 time=3.86 ms",
        "64 bytes from 8.8.8.8: icmp_seq=2 ttl=54 time=3.35 ms",
        "64 bytes from 8.8.8.8: icmp_seq=3 ttl=54 time=3.22 ms",
        "64 bytes from 8.8.8.8: icmp_seq=4 ttl=54 time=3.54 ms",
        "",
        "--- 8.8.8.8 ping statistics ---",
        "4 packets transmitted, 4 received, 0% packet loss, time 3005ms",
        "rtt min/avg/max/mdev = 3.224/3.494/3.860/0.246 ms"
    ]

Hope it helps!

ErnestoQ
  • 97
  • 1
  • 5