6

How can I make ansible show only errors inside of some playbook, or even when directly invoked?

I tried suppressing std output but that apparently didn't help, because execution errors seem to be put into standard output instead of error output on Linux.

ansible all -a 'some_command_I_want_to_know_if_it_crashes' 1> /dev/null

I see only errors from Python (exceptions etc.) but not errors from playbook (the red text).

techraf
  • 64,883
  • 27
  • 193
  • 198
Petr
  • 13,747
  • 20
  • 89
  • 144

2 Answers2

3

Use the official sample callback plugin called actionable.py.

Put it in the callback_plugins directory and enable stdout-callbacks in ansible.cfg:

[defaults]
stdout_callback = actionable

Just by enabling it you will get much less information in th output, but you can further modify the plugin code to suit your needs.

For example to disable messages on successful tasks completely (regardless if status is ok or changed) change:

def v2_runner_on_ok(self, result):
    if result._result.get('changed', False):
        self.display_task_banner()
        self.super_ref.v2_runner_on_ok(result)

to

def v2_runner_on_ok(self, result):
    pass

As Konstantin Suvorov noted, the above ansible.cfg configuration method works for ansible-playbook.

For ansible output you can save the actionable.py as ./callback_plugins/minimal.py to achieve the same results.

techraf
  • 64,883
  • 27
  • 193
  • 198
  • 3
    It is worth noting, that `stdout_callback` affects only `ansible-playbook`, but not `ansible`. See [this answer](http://stackoverflow.com/a/38667680/2795592) for details. – Konstantin Suvorov Oct 25 '16 at 17:31
  • @KonstantinSuvorov Correct me if I'm wrong, but it seems like the `minimal.py` filename is predefined for `ansible` calls, but content-wise you can substitute it with any other sample plugin code, right? – techraf Oct 25 '16 at 23:37
  • Yes, that's correct. Also there is `oneline.py` if you use `-o` option. But you can change it's content for your needs. – Konstantin Suvorov Oct 26 '16 at 05:40
  • ok now an extra question, is it possible to run only /some/ `ansible-playbook` runs with this output? Or enable it on-demand – Petr Oct 27 '16 at 09:14
  • You can have a separate `ansible.cfg` (with different plugins enabled) in each project. Or you can define a path to the config file in an environment variable `ANSIBLE_CONFIG` and precede the `ansible-playbook` command. – techraf Oct 27 '16 at 09:16
0

You could run the command with actionable callback

ANSIBLE_STDOUT_CALLBACK=actionable ansible all -a 'some_command_I_want_to_know_if_it_crashes'
hamletmun
  • 51
  • 3