ansible log_plays.py callback plugin not working with ansible 2.0.1 as expected

We are using ansible in our production and our log_plays callback plugin just broke after upgrading from 1.9.4 to 2.0.0.1 .

I need to put my custom logging using log_plays.py ( say store the results in a sqllitedb for later analysis ) . The same was working fine till ansible 1.9.4 . I am using ansible 2.0.0 on ubuntu 14.04 64 bit .

I used this log file https://github.com/ansible/ansible/blob/stable-2.0.0.1/lib/ansibale/plugins/callback/log_plays.py . Any function overrided under the class CallbackModule(CallbackBase): are not reflecting while the code outside class is working .

Example -

def runner_on_ok(self, host, res): self.log(host, 'ok-it-worked', res)

I believe you should use ‘v2_’ versions of functions in 2.0

Yes the internal ansible APIs have changed in v2

Have a look at the other plugins in your ansible installation.

For example

site-packages/ansible/plugins/callback/timer.py

You will notice that the plugins now subclass CallbackModule.

Another change in Ansible 2.0 is that you can just whitelist the plugins that you want to use in your ansible.cfg, rather than having to copy them to somewhere on your plugin path.

Hope this helps,

Jon

Yes the internal ansible APIs have changed in v2

Have a look at the other plugins in your ansible installation.

For example

site-packages/ansible/plugins/callback/timer.py

You will notice that the plugins now subclass CallbackModule.

Another change in Ansible 2.0 is that you can just whitelist the plugins that you want to use in your ansible.cfg, rather than having to copy them to somewhere on your plugin path.

Hope this helps,

Jon

I believe you should use ‘v2_’ versions of functions in 2.0

Thanks a lot guys . I used the ‘v2_’ versions of the functions however it still couldn’t work. I have the following below code . Is something wrong in it
`

class CallbackModule(CallbackBase):
“”"
logs playbook results, per host, in /var/log/ansible/hosts
“”"
CALLBACK_VERSION = 2.0
CALLBACK_TYPE = ‘notification’
CALLBACK_NAME = ‘log_plays’
CALLBACK_NEEDS_WHITELIST = True

TIME_FORMAT=“%b %d %Y %H:%M:%S”
MSG_FORMAT=“%(now)s - %(category)s - %(data)s\n\n”

def init(self):

super(CallbackModule, self).init()

if not os.path.exists(“/var/log/ansible/hosts”):
os.makedirs(“/var/log/ansible/hosts”)

def log(self, host, category, data):
if type(data) == dict:
if ‘_ansible_verbose_override’ in data:

avoid logging extraneous data

data = ‘omitted’
else:
data = data.copy()
invocation = data.pop(‘invocation’, None)
data = json.dumps(data)
if invocation is not None:
data = json.dumps(invocation) + " => %s " % data

path = os.path.join(“/var/log/ansible/hosts”, host)
now = time.strftime(self.TIME_FORMAT, time.localtime())
msg = to_bytes(self.MSG_FORMAT % dict(now=now, category=category, data=data))
with open(path, “ab”) as fd:
fd.write(msg)

def v2_runner_on_failed(self, host, res, ignore_errors=False):
self.log(host, ‘FAILED’, res)

def v2_runner_on_ok(self, host, res):
self.log(host, ‘OKkkkk’, res)

def v2_runner_on_skipped(self, host, item=None):
self.log(host, ‘SKIPPED’, ‘…’)

def v2_runner_on_unreachable(self, host, res):
self.log(host, ‘UNREACHABLE’, res)

def v2_runner_on_async_failed(self, host, res, jid):
self.log(host, ‘ASYNC_FAILED’, res)

def v2_playbook_on_import_for_host(self, host, imported_file):
self.log(host, ‘IMPORTED’, imported_file)

def v2_playbook_on_not_import_for_host(self, host, missing_file):
self.log(host, ‘NOTIMPORTED’, missing_file)

`

It still didn’t overrided .

Can anyone help with below . Its not yet solved for me .

Have you ‘whitelisted’ your plugin in your ansible.cfg?

callback_whitelist = timer, mail

callback_whitelist = log_plays

If so, you can throw in some print statements temporarily to debug it.

You might want to whitelist the context_demo plugin as that will show you what play and task objects are available to plugins.

Hope this helps.

Jon

running with -vvv you should see messages about the v2 callbacks being
loaded, whitlisting is ONLY needed for v2 plugins that request it (we
do this for the non default ones shipped with ansible).

Thanks a lot Brian . It solved my issue. I was missing callback whitelist in ansible.cfg

In ansible 1.9 didn’t required callback to whitelist. They used to run with default config.

Thanks a lot for the help .

​in 1.9 they required that you copy them to a callback_plugins dir by play or in role​, to avoid that in 2.0 we ship them in same directory as default ones but made them require whitlisting.