# ansible log\_plays.py callback plugin not working with ansible 2.0.1 as expected

**URL:** <https://forum.ansible.com/t/ansible-log-plays-py-callback-plugin-not-working-with-ansible-2-0-1-as-expected/20438>\
**Category:** Ansible Project\
**Tags:** ubuntu\
**Created:** [February 3, 2016, 1:23pm UTC](https://forum.ansible.com/t/ansible-log-plays-py-callback-plugin-not-working-with-ansible-2-0-1-as-expected/20438 "2016-02-03T13:23:05Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ankit\_Kulkarni](https://avatars.discourse-cdn.com/v4/letter/a/c68b51/32.png) [@Ankit\_Kulkarni](https://forum.ansible.com/u/Ankit_Kulkarni)\
**Post date:** [February 3, 2016, 1:23pm UTC](https://forum.ansible.com/t/ansible-log-plays-py-callback-plugin-not-working-with-ansible-2-0-1-as-expected/20438/1 "2016-02-03T13:23:05Z")

</div>

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](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)`

---

<div class="post-metadata">

**Author:** ![tinyzais](https://avatars.discourse-cdn.com/v4/letter/t/7c8e57/32.png) [@tinyzais](https://forum.ansible.com/u/tinyzais)\
**Post date:** [February 3, 2016, 1:48pm UTC](https://forum.ansible.com/t/ansible-log-plays-py-callback-plugin-not-working-with-ansible-2-0-1-as-expected/20438/2 "2016-02-03T13:48:38Z")

</div>

I believe you should use ‘v2\_’ versions of functions in 2.0

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [February 3, 2016, 2:11pm UTC](https://forum.ansible.com/t/ansible-log-plays-py-callback-plugin-not-working-with-ansible-2-0-1-as-expected/20438/3 "2016-02-03T14:11:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Ankit\_Kulkarni](https://avatars.discourse-cdn.com/v4/letter/a/c68b51/32.png) [@Ankit\_Kulkarni](https://forum.ansible.com/u/Ankit_Kulkarni)\
**Post date:** [February 3, 2016, 2:35pm UTC](https://forum.ansible.com/t/ansible-log-plays-py-callback-plugin-not-working-with-ansible-2-0-1-as-expected/20438/4 "2016-02-03T14:35:31Z")

</div>

> 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 .

---

<div class="post-metadata">

**Author:** ![Ankit\_Kulkarni](https://avatars.discourse-cdn.com/v4/letter/a/c68b51/32.png) [@Ankit\_Kulkarni](https://forum.ansible.com/u/Ankit_Kulkarni)\
**Post date:** [February 8, 2016, 10:20am UTC](https://forum.ansible.com/t/ansible-log-plays-py-callback-plugin-not-working-with-ansible-2-0-1-as-expected/20438/5 "2016-02-08T10:20:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [February 8, 2016, 2:58pm UTC](https://forum.ansible.com/t/ansible-log-plays-py-callback-plugin-not-working-with-ansible-2-0-1-as-expected/20438/6 "2016-02-08T14:58:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [February 8, 2016, 3:18pm UTC](https://forum.ansible.com/t/ansible-log-plays-py-callback-plugin-not-working-with-ansible-2-0-1-as-expected/20438/7 "2016-02-08T15:18:04Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![Ankit\_Kulkarni](https://avatars.discourse-cdn.com/v4/letter/a/c68b51/32.png) [@Ankit\_Kulkarni](https://forum.ansible.com/u/Ankit_Kulkarni)\
**Post date:** [February 9, 2016, 4:29am UTC](https://forum.ansible.com/t/ansible-log-plays-py-callback-plugin-not-working-with-ansible-2-0-1-as-expected/20438/8 "2016-02-09T04:29:13Z")

</div>

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 .

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [February 9, 2016, 6:26am UTC](https://forum.ansible.com/t/ansible-log-plays-py-callback-plugin-not-working-with-ansible-2-0-1-as-expected/20438/9 "2016-02-09T06:26:02Z")

</div>

​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.
