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

1,249 views
Skip to first unread message

Ankit Kulkarni

unread,
Feb 3, 2016, 8:23:05 AM2/3/16
to Ansible Project
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 wanted to use the playbook_on_stats(self, stats) function . How can I use it  ? I can see the changes from the 1.9.4 log_plays.py file https://github.com/ansible/ansible/blob/stable-1.9/plugins/callbacks/log_plays.py  like now  not all functions are included but not getting why the functions are not overriding . 


tiny...@gmail.com

unread,
Feb 3, 2016, 8:48:38 AM2/3/16
to Ansible Project
I believe you should use 'v2_' versions of functions in 2.0

J Hawkesworth

unread,
Feb 3, 2016, 9:11:53 AM2/3/16
to Ansible Project
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

Ankit Kulkarni

unread,
Feb 3, 2016, 9:35:31 AM2/3/16
to Ansible Project

On Wednesday, February 3, 2016 at 7:41:53 PM UTC+5:30, J Hawkesworth wrote:
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

On Wednesday, 3 February 2016 13:48:38 UTC, tiny...@gmail.com wrote:
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 . 

Ankit Kulkarni

unread,
Feb 8, 2016, 5:20:33 AM2/8/16
to Ansible Project
Can anyone help with below . Its not yet solved for me . 

J Hawkesworth

unread,
Feb 8, 2016, 9:58:38 AM2/8/16
to Ansible Project
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

Brian Coca

unread,
Feb 8, 2016, 10:18:13 AM2/8/16
to Ansible Project
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).




--
Brian Coca

Ankit Kulkarni

unread,
Feb 8, 2016, 11:29:29 PM2/8/16
to ansible...@googlegroups.com

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 .

--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/uxJ1TNpZzTM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAJ5XC8kEGoHYzaF-KSSn%3DTyHxEaROv05c%3D%2BPaoS1sz7FGUBUxw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Brian Coca

unread,
Feb 9, 2016, 1:26:08 AM2/9/16
to ansible...@googlegroups.com
​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.

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.

To post to this group, send email to ansible...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages