summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorToshio Kuratomi <toshio@fedoraproject.org>2016-01-29 18:58:01 -0800
committerToshio Kuratomi <toshio@fedoraproject.org>2016-01-29 19:45:12 -0800
commit9e3932ffcad920adf2c9452e3739822c19bb2c2a (patch)
treebc775d9deafc990f73c5afaeab2ed54702bd5f23
parentfb57818ea3860902750215ab2876c2195a48192a (diff)
downloadansible-callback-v2-attribute-replacement.tar.gz
Some attributes of callbacks aren't in v2. Port plugins to the v2 way to do thatcallback-v2-attribute-replacement
Update porting guide with info on callback porting
-rw-r--r--docsite/rst/porting_guide_2.0.rst28
-rw-r--r--lib/ansible/plugins/callback/context_demo.py16
-rw-r--r--lib/ansible/plugins/callback/hipchat.py5
3 files changed, 47 insertions, 2 deletions
diff --git a/docsite/rst/porting_guide_2.0.rst b/docsite/rst/porting_guide_2.0.rst
index a26763fc14..489b74a287 100644
--- a/docsite/rst/porting_guide_2.0.rst
+++ b/docsite/rst/porting_guide_2.0.rst
@@ -168,7 +168,33 @@ Action plugins
Callback plugins
----------------
-* callback plugins
+Although Ansible 2.0 provides a new callback API the old one continues to work
+for most callback plugins. However, if your callback plugin makes use of
+:attr:`self.playbook`, :attr:`self.play`, or :attr:`self.task` then you will
+have to store the values for these yourself as ansible no longer automatically
+populates the callback with them. Here's a short snippet that shows you how::
+
+ from ansible.plugins.callback import CallbackBase
+
+ class CallbackModule(CallbackBase):
+ def __init__(self):
+ self.playbook = None
+ self.play = None
+ self.task = None
+
+ def v2_playbook_on_start(self, playbook):
+ self.playbook = playbook
+
+ def v2_playbook_on_play_start(self, play):
+ self.play = play
+
+ def v2_playbook_on_task_start(self, task, is_conditional):
+ self.task = task
+
+ def v2_on_any(self, *args, **kwargs):
+ self._display.display('%s: %s: %s' % (self.playbook.name,
+ self.play.name, self.task))
+
Connection plugins
------------------
diff --git a/lib/ansible/plugins/callback/context_demo.py b/lib/ansible/plugins/callback/context_demo.py
index ec4454c45a..f01f849406 100644
--- a/lib/ansible/plugins/callback/context_demo.py
+++ b/lib/ansible/plugins/callback/context_demo.py
@@ -31,8 +31,18 @@ class CallbackModule(CallbackBase):
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))
@@ -41,3 +51,9 @@ class CallbackModule(CallbackBase):
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
diff --git a/lib/ansible/plugins/callback/hipchat.py b/lib/ansible/plugins/callback/hipchat.py
index b31140128b..602827aeac 100644
--- a/lib/ansible/plugins/callback/hipchat.py
+++ b/lib/ansible/plugins/callback/hipchat.py
@@ -73,6 +73,7 @@ class CallbackModule(CallbackBase):
self.printed_playbook = False
self.playbook_name = None
+ self.play = None
def send_msg(self, msg, msg_format='text', color='yellow', notify=False):
"""Method for sending a message to HipChat"""
@@ -93,9 +94,11 @@ class CallbackModule(CallbackBase):
self.display.warning('Could not submit message to hipchat')
- def playbook_on_play_start(self, name):
+ def v2_playbook_on_play_start(self, play):
"""Display Playbook and play start messages"""
+ self.play = play
+ name = play.name
# This block sends information about a playbook when it starts
# The playbook object is not immediately available at
# playbook_on_start so we grab it via the play