from __future__ import (absolute_import, division, print_function) __metaclass__ = type DOCUMENTATION = ''' callback: log_task type: notification short_description: log task result status description: - This plugin will log task result status. ''' from ansible.plugins.callback import CallbackBase from ansible import constants as C import os import io import logging TASK_LOGFILE = os.path.dirname(C.DEFAULT_LOG_PATH) + "/task.log" FAILED_LOGFILE = os.path.dirname(C.DEFAULT_LOG_PATH) + "/task_failed.log" class CallbackModule(CallbackBase): CALLBACK_VERSION = 2.0 CALLBACK_TYPE = 'notification' CALLBACK_NAME = 'log_task' 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_task = logging.getLogger('task') self.logger_task.addHandler(logging.FileHandler(TASK_LOGFILE)) self.logger_failed = logging.getLogger('failed') self.logger_failed.addHandler(logging.FileHandler(FAILED_LOGFILE)) def v2_runner_on_failed(self, result, ignore_errors=False): if not ignore_errors: self.logger_failed.error('[%s]: FAILED! => %s', result._host.name, result._task) def v2_runner_on_unreachable(self, result): self.logger_failed.error('[%s]: UNREACHABLE! => %s', result._host.name, result._task) def v2_playbook_on_task_start(self, task, is_conditional): self.logger_task.info(task) def v2_playbook_on_start(self, playbook): open(TASK_LOGFILE, 'w').close() open(FAILED_LOGFILE, 'w').close()