3

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?

Jon Dison
  • 31
  • 1
  • 2

2 Answers2

4

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
Konstantin Suvorov
  • 3,996
  • 1
  • 12
  • 13
  • Thanks. That looks like a good start, but when I use the ANSIBLE_STDOUT_CALLBACK=json, then the variable registered during playbook execution isn't in the list of the output. Taking that off gets me back to where I have the typical Ansible output, but the variable does print its value. – Jon Dison Dec 29 '16 at 04:21
1

You can:

  • alter the log output of Ansible using Callback Plugins. It will require some Python coding. Unfortunately move of the example plugins seem to fulfill your requirements.

  • use a Jinja2 template and save the output to a file with either a template or a copy module with content parameter.

    First you'd need to combine the dictionaries into a list and iterate over items in the template.

techraf
  • 4,243
  • 8
  • 29
  • 44