Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make verbose output for specific task or module in Ansible

I am using AWS ec2 module and I would like to log what userdata is sent to AWS with every command, but I don't want verbose output from all other useless tasks.

Is there any way I can enable verbosity of the ec2 module?

like image 398
user3214546 Avatar asked Jan 04 '23 00:01

user3214546


1 Answers

I agree with @techraf that there is no out of the box way to do this.
But Ansible is easily tuneable with plugins!

Drop this code as <playbook_dir>/callback_plugins/verbose_tasks.py:

from ansible.plugins.callback import CallbackBase
import json

try:
    from __main__ import display
except ImportError:
    display = None

class CallbackModule(CallbackBase):

    def v2_runner_on_any(self, result, ignore_errors=False):
        if (result._task.action in ['file','stat']):
            print '####### DEBUG ########'
            print json.dumps(result._result,indent=4)
            print '####### DEBUG ########'

    v2_runner_on_ok = v2_runner_on_any
    v2_runner_on_failed = v2_runner_on_any

You can tune what modules' results you want to print by changing ['file','stat'] list.
If you need only ec2, replace it with ['ec2'].

like image 106
Konstantin Suvorov Avatar answered May 16 '23 09:05

Konstantin Suvorov