Ansible print hostnames / variables in list / table

ansibleansible-playbook

Is there a way with Ansible to print out a simple table style output. I've got the values I need registered to a variable in the playbook I just want to print it out more cleanly.
Currently the output I have with stdout.lines looks like this:

ok: [sage] => {
    "voutput.stdout_lines": [
        "4.4.5"
    ]
}

ok: [example] => {
    "voutput.stdout_lines": [
        "4.7"
    ]
}

But it'd be nice if I could get it formatted more like this:

sage              4.7
example           4.4.5
somethingelse     1.2.3

Is there any way to do something along those lines directly within Ansible, or do I just have to take the output it gives and transform it with something like AWK?

Best Answer

You can use standard json output callback and fetch required data with jq.

hosts:

[test]
srv1 testvar=abc
srv2 testvar=zzzzzz
srv3 testvar=qqqq

playbook.yml:

- hosts: test
  gather_facts: no
  tasks:
    - debug: var=testvar

execution:

$ ANSIBLE_STDOUT_CALLBACK=json ansible-playbook playbook.yml | jq -r '.plays[0].tasks[0].hosts | to_entries[] | "\(.key), \(.value.testvar)"'
srv1, abc
srv2, zzzzzz
srv3, qqqq