# (C) 2012, Michael DeHaan, <michael.dehaan@gmail.com>

# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.

# Make coding more python3-ish
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

from ansible.plugins.callback import CallbackBase

class CallbackModule(CallbackBase):
    """
    This is a very trivial example of how any callback function can get at play and task objects.
    play will be 'None' for runner invocations, and task will be None for 'setup' invocations.
    """
    CALLBACK_VERSION = 2.0
    CALLBACK_TYPE = 'aggregate'
    CALLBACK_NAME = 'context_demo'
    CALLBACK_NEEDS_WHITELIST = True

    def __init__(self, *args, **kwargs):
        self.task = None
        self.play = None

    def v2_on_any(self, *args, **kwargs):
        i = 0
        if self.play:
            play_str = 'play: %s' % self.play.name
        if self.task:
            task_str = 'task: %s' % self.task
        self._display.display("--- %s %s ---" % (self.play_str, self.task_str))

        self._display.display("     --- ARGS ")
        for a in args:
            self._display.display('     %s: %s' % (i, a))
            i += 1

        self._display.display("      --- KWARGS ")
        for k in kwargs:
            self._display.display('     %s: %s' % (k, kwargs[k]))

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

    def v2_playbook_on_task_start(self, task, is_conditional):
        self.task = task

@@ -1303,74 +1303,44 @@
    s
-elf.task = None%0A        self.play = None%0A%0A    def v2_on_any(self, 
+uper(CallbackModule, self).__init__(
 *arg
@@ -1343,33 +1343,32 @@
 *args, **kwargs)
-:
 %0A        i = 0%0A 
@@ -1364,217 +1364,190 @@
     
-i = 0%0A        if self.play:%0A            play_str = 'play: %25s' %25 self.play.name%0A        if self.task:%0A            task_str = 'task: %25s' %25 self.task%0A        self._display.display(%22--- %25s %25s ---%22 %25 (self.play_str
+self.task = None%0A        self.play = None%0A%0A    def v2_on_any(self, *args, **kwargs):%0A        self._display.display(%22--- play: %7B%7D task: %7B%7D ---%22.format(getattr(self.play, 'name', None)
 , se
@@ -1557,12 +1557,8 @@
 task
-_str
 ))%0A%0A
@@ -1620,18 +1620,32 @@
  for
+ i,
  a in 
+enumerate(
 args
+)
 :%0A  
@@ -1703,27 +1703,8 @@
  a))
-%0A            i += 1
 %0A%0A  

Fix context_demo callback plugin. (#17235)