Callback plugin with Ansible 2.0

480 views
Skip to first unread message

Sam Sen

unread,
Aug 26, 2016, 3:38:07 PM8/26/16
to Ansible Development
I inherited a callback plugin that was woirking with Ansible 1.9. We upgraded to 2.1 and it no longer works. 

Looks like it's trying to access the playbook object but it's not getting passed into "v2_playbook_on_stats." My Python is rusty so I'm not sure how I can go about accessing that data. I'm tryijng to access the "extra-vars" variable but it's giving me the following error:

 [WARNING]: Failure using method (v2_playbook_on_stats) in callback plugin
(</home/deployment/scripts/playbooks-2.0/callback/log_to_mongo.CallbackModule object at 0x2b8cf90>): 'NoneType' object has no
attribute 'extra_vars'


class CallbackModule(object):
    """
    An ansible callback module for saving Ansible output to a database log
    """
    def __init__(self):
        self.playbook = None
        self.playbook_name = None
        self.play = None
        self.task = None

    CALLBACK_VERSION = 2.0
    CALLBACK_TYPE = 'notification'
    CALLBACK_NAME = 'log_to_mongo'
    CALLBACK_NEEDS_WHITELIST = False

def v2_playbook_on_stats(self, stats):
        global jira_ticket
        global deployed_modules
        global ocd_job_id
        global environment_id

        deployed_modules = []
        jira_ticket = self.playbook.extra_vars['jira_ticket']
        ocd_job_id = self.playbook.extra_vars['ocd_job_id']
        environment_id = self.playbook.extra_vars['environment_id']

Raja

unread,
Aug 26, 2016, 10:14:53 PM8/26/16
to Sam Sen, Ansible Development
I think the mechanism changed in Ansible 2. You might have to get your play object in v2_playbook_on_play_start and then use that variable in on_stats . Something like

class CallbackModule(object):
      def __init__(self):
            super(CallbackModule,self).__init__()
            self.play = None

       def v2_playbook_on_play_start(self, play):
            self.play = play

      .....
     
      def v2_playbook_on_stats(self, stats):
            extra_vars = self.play._variable_manager.extra_vars
            jira_ticket = extra_vars['jira_ticket']

--
You received this message because you are subscribed to the Google Groups "Ansible Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-devel+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Raja
rajasaur at gmail.com

Sam Sen

unread,
Aug 26, 2016, 10:21:01 PM8/26/16
to Ansible Development
THANK YOU!!!!!!!!

Now how the heck do I track down all these changes? I like v2 but it broke a lot of things.

Jason DeTiberus

unread,
Aug 27, 2016, 3:22:57 AM8/27/16
to Sam Sen, Ansible Development

--

Sam Sen

unread,
Aug 29, 2016, 10:26:18 AM8/29/16
to Ansible Development, samna...@gmail.com
Not really. This is highly frustrated, but I'm speaking for myself.

So for example, I can't figure out where certain methods are. Other folks are also expressing the same feelings.

Case in point:

To get extra vars I can use the following line in v2.

extra_vars = self.play._variable_manager.extra_vars
jira_ticket = extra_vars['jira_ticket']

Old method:

jira_ticket = self.playbook.extra_vars['jira_ticket']

Now I'm trying to chase down where the var cache are located.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-deve...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages