Skip to content
This repository was archived by the owner on Jun 24, 2021. It is now read-only.

Commit

Permalink
develop a help plugin (#207)
Browse files Browse the repository at this point in the history
  • Loading branch information
LinuxGit authored Jan 11, 2018
1 parent 1a05dbe commit f663a65
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
2 changes: 2 additions & 0 deletions ansible.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ log_path = log/ansible.log

deprecation_warnings = False

callback_whitelist = help

[ssh_connection]
## AWS key connection
# ssh_args = -i aws.key -C -o ControlMaster=auto -o ControlPersist=60s
Expand Down
72 changes: 72 additions & 0 deletions callback_plugins/help.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Make coding more python3-ish
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

DOCUMENTATION = '''
callback: help
type: notification
short_description: print help message
version_added: historical
description:
- This plugin will print help message when tasks fail.
'''

from ansible.plugins.callback import CallbackBase
from ansible import constants as C
import os
import logging

FAIL_LOGFILE = os.path.dirname(C.DEFAULT_LOG_PATH) + "/fail.log"

class CallbackModule(CallbackBase):
CALLBACK_VERSION = 2.0
CALLBACK_TYPE = 'notification'
CALLBACK_NAME = 'help'
CALLBACK_NEEDS_WHITELIST = True

def __init__(self):

super(CallbackModule, self).__init__()

if not os.path.exists(os.path.dirname(C.DEFAULT_LOG_PATH)):
os.makedirs(os.path.dirname(C.DEFAULT_LOG_PATH))

self.logger = logging.getLogger('fail')
self.logger.setLevel(logging.DEBUG)
self.handler = logging.FileHandler(FAIL_LOGFILE)
self.logger.addHandler(self.handler)

def print_help_message(self):
self._display.display("Ask for help:", color=C.COLOR_WARN)
self._display.display("Contact us: [email protected]", color=C.COLOR_HIGHLIGHT)
self._display.display("It seems that you encounter some problems. You can send an email to the above email address, attached with the tidb-ansible/inventory.ini and tidb-ansible/log/ansible.log files and the error message, or new issue on https://github.com/pingcap/docs/issues. We'll try our best to help you deploy a TiDB cluster. Thanks. :-)", color=C.COLOR_WARN)

def v2_runner_on_failed(self, result, ignore_errors=False):
if not ignore_errors:
self.print_help_message()
self.logger.error('[%s]: Ansible FAILED! => playbook: %s; %s; message: %s', result._host.name, self.playbook, result._task, self._dump_results(result._result))

def v2_runner_on_unreachable(self, result):
self.print_help_message()
self.logger.error('[%s]: Ansible UNREACHABLE! => playbook: %s; %s; message: %s', result._host.name, self.playbook, result._task, self._dump_results(result._result))

def v2_playbook_on_start(self, playbook):
self.playbook = playbook._file_name
open(FAIL_LOGFILE, 'w').close()

def v2_playbook_on_stats(self, stats):
if os.path.isfile(FAIL_LOGFILE):
count = -1
with open(FAIL_LOGFILE, 'r') as f:
for count, line in enumerate(f):
pass
count += 1

if count > 0:
self._display.banner("ERROR MESSAGE SUMMARY")
with open(FAIL_LOGFILE, 'r') as f:
for _, line in enumerate(f):
self._display.display(line, color=C.COLOR_ERROR)
self.print_help_message()
else:
self._display.display("Congrats! All goes well. :-)", color=C.COLOR_OK)

0 comments on commit f663a65

Please sign in to comment.