[ansible-project] Print output in tabular column format.

1,837 views
Skip to first unread message

saravanan jothilingam

unread,
Nov 15, 2022, 10:09:15 PM11/15/22
to ansible...@googlegroups.com
Hi,
I need to get the ping status of all remote hosts and print its results in one neat tabular column format. Since I am a novice to ansible, I am looking for the appropriate playbook which does this. 

note - the below task doesnt give any clear output .
- name: check reachable hosts
  hosts: remote-hosts
  gather_facts: no
  tasks:
    - command: ping -c1 {{ inventory_hostname }}
      delegate_to: localhost
      register: ping_result

Thanks
saravanan

Dick Visser

unread,
Nov 15, 2022, 10:50:47 PM11/15/22
to ansible...@googlegroups.com
Hi
What is the reason you need to use ansible for this?

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAE7H9qoRWWw%3D1gbLETVztDvWrgC_hupW_X_7OxJWTAc70mRvSw%40mail.gmail.com.
--
Sent from Gmail Mobile

saravanan jothilingam

unread,
Nov 15, 2022, 11:11:24 PM11/15/22
to ansible...@googlegroups.com
We have around 300+ on-prem linux servers to manage its availability. We found ansible is one easy tool to manage it.

Rowe, Walter P. (Fed)

unread,
Nov 16, 2022, 7:13:18 AM11/16/22
to ansible...@googlegroups.com
The command module runs the ping. It won't display the output you register in ping_result. You need a debug task if you want to see the result. If you want formatted output you may want to look at using a jinja2 template.

Walter
--
Walter Rowe, Division Chief
Infrastructure Services, OISM
Mobile: 202.355.4123

saravanan jothilingam

unread,
Nov 16, 2022, 8:40:15 AM11/16/22
to ansible...@googlegroups.com
Thanks for this input.
Could you please share one sample playbook code which does that? Appreciate your help!

Todd Lewis

unread,
Nov 16, 2022, 9:24:04 AM11/16/22
to ansible...@googlegroups.com, uto...@gmail.com
$ cat pingout.yml
---
# pingout.yml
# Run as: $ ansible-playbook pingout.yml -i pingout-inventory
#         $ cat pingout-inventory
#         $ cat /tmp/ping-report.txt

- name: check reachable hosts
  hosts: remote_hosts
  gather_facts: no
  tasks:
    - name: Ping each host
      ansible.builtin.command: ping -c1 {{ item }}
      run_once: true
      loop: "{{ ansible_play_hosts_all }}"
      delegate_to: localhost
      register: ping_result
      failed_when: false

    - name: How did we do?
      ansible.builtin.debug:
        msg: "{{ ping_result.results[0] }}"
      run_once: true

    - name: Format it a bit
      # Note: the docs for 'copy' say don't do this, to use template instead,
      # and there are reasons, but this suffices for posting purposes.
      # The 'content:' below should be in your template.
      ansible.builtin.copy:
        dest: /tmp/ping-report.txt
        content: |
          --- PING REPORT ---
          {% for pr in ping_result.results %}
          {{ pr.stdout_lines | first }}
          {{ pr.stdout_lines | last }}
          
          {% endfor %}
      run_once: true
      delegate_to: localhost

$ cat pingout-inventory

[remote_hosts]
cloister
bluemoon
lappy
dewdrop

$ ansible-playbook pingout.yml -i pingout-inventory
PLAY [check reachable hosts] *****************************************************

TASK [Ping each host] ************************************************************
changed: [cloister -> localhost] => (item=cloister)
changed: [cloister -> localhost] => (item=bluemoon)
changed: [cloister -> localhost] => (item=lappy)
changed: [cloister -> localhost] => (item=dewdrop)

TASK [How did we do?] ************************************************************
ok: [cloister] => {
    "msg": {
        "ansible_loop_var": "item",
        "changed": true,
        "cmd": [
            "ping",
            "-c1",
            "cloister"
        ],
        "delta": "0:00:00.008754",
        "end": "2022-11-16 09:18:34.819681",
        "failed": false,
        "failed_when_result": false,
        "invocation": {
            "module_args": {
                "_raw_params": "ping -c1 cloister",
                "_uses_shell": false,
                "argv": null,
                "chdir": null,
                "creates": null,
                "executable": null,
                "removes": null,
                "stdin": null,
                "stdin_add_newline": true,
                "strip_empty_ends": true,
                "warn": false
            }
        },
        "item": "cloister",
        "msg": "",
        "rc": 0,
        "start": "2022-11-16 09:18:34.810927",
        "stderr": "",
        "stderr_lines": [],
        "stdout": "PING cloister (192.168.254.246) 56(84) bytes of data.\n64 bytes from cloister (192.168.254.246): icmp_seq=1 ttl=64 time=5.98 ms\n\n--- cloister ping statistics ---\n1 packets transmitted, 1 received, 0% packet loss, time 0ms\nrtt min/avg/max/mdev = 5.983/5.983/5.983/0.000 ms",
        "stdout_lines": [
            "PING cloister (192.168.254.246) 56(84) bytes of data.",
            "64 bytes from cloister (192.168.254.246): icmp_seq=1 ttl=64 time=5.98 ms",
            "",
            "--- cloister ping statistics ---",
            "1 packets transmitted, 1 received, 0% packet loss, time 0ms",
            "rtt min/avg/max/mdev = 5.983/5.983/5.983/0.000 ms"
        ]
    }
}

TASK [Format it a bit] ***********************************************************
changed: [cloister -> localhost]

PLAY RECAP ***********************************************************************
cloister                   : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

$ cat /tmp/ping-report.txt
--- PING REPORT ---
PING cloister (192.168.254.246) 56(84) bytes of data.
rtt min/avg/max/mdev = 5.983/5.983/5.983/0.000 ms

PING bluemoon (192.168.254.245) 56(84) bytes of data.
rtt min/avg/max/mdev = 5.455/5.455/5.455/0.000 ms

PING lappy (192.168.254.244) 56(84) bytes of data.
1 packets transmitted, 0 received, +1 errors, 100% packet loss, time 0ms

PING dewdrop (192.168.254.243) 56(84) bytes of data.
rtt min/avg/max/mdev = 286.843/286.843/286.843/0.000 ms

-- 
Todd

Mike Eggleston

unread,
Nov 16, 2022, 11:06:58 AM11/16/22
to ansible...@googlegroups.com
I used the delegate to localhost to create a CSV report of inventory details. HTH

Mike

On Nov 16, 2022, at 07:40, saravanan jothilingam <saravan....@gmail.com> wrote:


Reply all
Reply to author
Forward
0 new messages