From 12bf9a8b6916e53ddb48a15ff12a3df09d748647 Mon Sep 17 00:00:00 2001 From: Michael Scherer Date: Mon, 12 Aug 2013 22:16:32 +0200 Subject: [PATCH] add a way for callback to disable itself The idea is that some plugin would not be called in some specific case, and the callback should decide by itself. Having a way to globally disable it is much cleaner than disabling every method one by one on the plugin side. My use case is for fedora-infrastructure that cannot be run from git checkout since it try to connect to the message bus, but another case would be to bootstrap infrastructure, or to run the code on a test servers without having all the callback infrastructure setup. --- lib/ansible/callback_plugins/noop.py | 7 ++++++- lib/ansible/callbacks.py | 4 ++++ plugins/callbacks/osx_say.py | 11 ++++++++++- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/lib/ansible/callback_plugins/noop.py b/lib/ansible/callback_plugins/noop.py index 54a2b254fc7..8af5a133e6c 100644 --- a/lib/ansible/callback_plugins/noop.py +++ b/lib/ansible/callback_plugins/noop.py @@ -21,11 +21,16 @@ class CallbackModule(object): """ this is an example ansible callback file that does nothing. You can drop other classes in the same directory to define your own handlers. Methods - you do not use can be omitted. + you do not use can be omitted. If self.disabled is set to True, the plugin + methods will not be called. example uses include: logging, emailing, storing info, etc """ + def __init__(self): + #if foo: + # self.disabled = True + pass def on_any(self, *args, **kwargs): pass diff --git a/lib/ansible/callbacks.py b/lib/ansible/callbacks.py index 7d7c7da5820..43a636cb0e0 100644 --- a/lib/ansible/callbacks.py +++ b/lib/ansible/callbacks.py @@ -144,6 +144,10 @@ def display(msg, color=None, stderr=False, screen_only=False, log_only=False, ru def call_callback_module(method_name, *args, **kwargs): for callback_plugin in callback_plugins: + # a plugin that set self.disabled to True will not be called + # see osx_say.py example for such a plugin + if getattr(callback_plugin, 'disabled', False): + continue methods = [ getattr(callback_plugin, method_name, None), getattr(callback_plugin, 'on_any', None) diff --git a/plugins/callbacks/osx_say.py b/plugins/callbacks/osx_say.py index 9f6763c100f..ec1f32bace9 100644 --- a/plugins/callbacks/osx_say.py +++ b/plugins/callbacks/osx_say.py @@ -17,19 +17,28 @@ # along with Ansible. If not, see . import subprocess +import os FAILED_VOICE="Zarvox" REGULAR_VOICE="Trinoids" HAPPY_VOICE="Cellos" LASER_VOICE="Princess" +SAY_CMD="/usr/bin/say" def say(msg, voice): - subprocess.call(["/usr/bin/say", msg, "--voice=%s" % (voice)]) + subprocess.call([SAY_CMD, msg, "--voice=%s" % (voice)]) class CallbackModule(object): """ makes Ansible much more exciting on OS X. """ + def __init__(self): + # plugin disable itself if say is not present + # ansible will not call any callback if disabled is set to True + if not os.path.exists(SAY_CMD): + self.disabled = True + print "%s does not exist, plugin %s disabled" % \ + (SAY_CMD, os.path.basename(__file__)) def on_any(self, *args, **kwargs): pass