Last active
September 1, 2016 18:21
-
-
Save stick/12df9e84bf8da08e348af363f96c66a5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# (C) 2012, Michael DeHaan, <[email protected]> | |
# 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 pprint import pprint | |
import subprocess | |
import os | |
from ansible.plugins.callback import CallbackBase | |
DEFAULT_SOUND="Submarine" | |
FAILED_SOUND="Glass" | |
NOTIFY_CMD="/Users/ckm/bin/tmux-terminal-notifier" | |
class CallbackModule(CallbackBase): | |
""" | |
makes Ansible much more exciting on OS X. | |
""" | |
CALLBACK_VERSION = 2.0 | |
CALLBACK_TYPE = 'notification' | |
CALLBACK_NAME = 'notify' | |
CALLBACK_NEEDS_WHITELIST = False | |
def __init__(self): | |
super(CallbackModule, self).__init__() | |
# plugin disable itself if notify is not present | |
# ansible will not call any callback if disabled is set to True | |
if not os.path.exists(NOTIFY_CMD): | |
self.disabled = True | |
self._display.warning("%s does not exist, plugin %s disabled" % (NOTIFY_CMD, os.path.basename(__file__)) ) | |
def notify(self, msg, sound): | |
title = "Ansible" | |
subtitle = "Playbook: %s" % self.playbook_name | |
subprocess.call([NOTIFY_CMD, "-message", msg, "-title", title, "-sound", sound, "-subtitle", subtitle]) | |
def v2_playbook_on_start(self, playbook): | |
self.playbook_name = os.path.basename(playbook._file_name) | |
def runner_on_failed(self, host, res, ignore_errors=False): | |
self.notify("Failure on host %s" % host, FAILED_SOUND) | |
def runner_on_unreachable(self, host, res): | |
self.notify("Unreachable host %s" % host, FAILED_SOUND) | |
def runner_on_async_failed(self, host, res, jid): | |
self.notify("Failure on host %s" % host, FAILED_SOUND) | |
def playbook_on_stats(self, stats): | |
self.notify("Playbook complete", DEFAULT_SOUND) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# (C) 2012, Michael DeHaan, <[email protected]> | |
# 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/>. | |
import subprocess | |
import os | |
DEFAULT_SOUND="Submarine" | |
FAILED_SOUND="Glass" | |
NOTIFY_CMD="/Users/ckm/bin/tmux-terminal-notifier" | |
def notify(msg, sound): | |
title="Ansible Playbook" | |
process = subprocess.Popen([NOTIFY_CMD, "-sound", sound, "-title", title], shell=False, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
print process.communicate(msg)[0] | |
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(NOTIFY_CMD): | |
self.disabled = True | |
print "%s does not exist, plugin %s disabled" % \ | |
(NOTIFY_CMD, os.path.basename(__file__)) | |
def on_any(self, *args, **kwargs): | |
pass | |
def runner_on_failed(self, host, ignore_errors=False): | |
notify("Failure on host %s" % host, FAILED_SOUND) | |
def runner_on_unreachable(self, host, res): | |
notify("Failure on host %s" % host, FAILED_SOUND) | |
def runner_on_async_failed(self, host, res, jid): | |
notify("Failure on host %s" % host, FAILED_SOUND) | |
def playbook_on_stats(self, stats): | |
print "this is self: %s" % self | |
notify("Play complete", DEFAULT_SOUND) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The new one actually works fairly well and is probably a little cleaner.