Skip to content

Instantly share code, notes, and snippets.

@stick
Created April 30, 2020 14:39
Show Gist options
  • Save stick/ff4290760bdf8260d8c5d68d0083563c to your computer and use it in GitHub Desktop.
Save stick/ff4290760bdf8260d8c5d68d0083563c to your computer and use it in GitHub Desktop.
old osx notify callback plugin for ansible
# (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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment