2015-07-13 19:42:47 +00:00
|
|
|
# (c) 2012-2014, Michael DeHaan <michael.dehaan@gmail.com>
|
|
|
|
#
|
|
|
|
# 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 ansible.plugins.callback import CallbackBase
|
2015-07-23 17:42:20 +00:00
|
|
|
from ansible import constants as C
|
2015-07-13 19:42:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
class CallbackModule(CallbackBase):
|
|
|
|
|
|
|
|
'''
|
|
|
|
This is the default callback interface, which simply prints messages
|
|
|
|
to stdout when new callback events are received.
|
|
|
|
'''
|
|
|
|
|
|
|
|
CALLBACK_VERSION = 2.0
|
|
|
|
CALLBACK_TYPE = 'stdout'
|
|
|
|
CALLBACK_NAME = 'oneline'
|
|
|
|
|
2015-07-23 17:42:20 +00:00
|
|
|
def _command_generic_msg(self, hostname, result, caption):
|
2017-04-26 22:09:36 +00:00
|
|
|
stdout = result.get('stdout','').replace('\n', '\\n').replace('\r', '\\r')
|
2015-07-23 17:42:20 +00:00
|
|
|
if 'stderr' in result and result['stderr']:
|
2017-04-26 22:09:36 +00:00
|
|
|
stderr = result.get('stderr','').replace('\n', '\\n').replace('\r', '\\r')
|
2016-12-07 16:00:11 +00:00
|
|
|
return "%s | %s | rc=%s | (stdout) %s (stderr) %s" % (hostname, caption, result.get('rc', -1), stdout, stderr)
|
2015-07-23 17:42:20 +00:00
|
|
|
else:
|
2016-12-07 16:00:11 +00:00
|
|
|
return "%s | %s | rc=%s | (stdout) %s" % (hostname, caption, result.get('rc', -1), stdout)
|
2015-07-23 17:42:20 +00:00
|
|
|
|
2015-07-13 19:42:47 +00:00
|
|
|
def v2_runner_on_failed(self, result, ignore_errors=False):
|
|
|
|
if 'exception' in result._result:
|
|
|
|
if self._display.verbosity < 3:
|
|
|
|
# extract just the actual error message from the exception text
|
|
|
|
error = result._result['exception'].strip().split('\n')[-1]
|
|
|
|
msg = "An exception occurred during task execution. To see the full traceback, use -vvv. The error was: %s" % error
|
|
|
|
else:
|
|
|
|
msg = "An exception occurred during task execution. The full traceback is:\n" + result._result['exception'].replace('\n','')
|
|
|
|
|
2016-12-07 16:00:11 +00:00
|
|
|
if result._task.action in C.MODULE_NO_JSON and 'module_stderr' not in result._result:
|
2015-12-29 20:41:00 +00:00
|
|
|
self._display.display(self._command_generic_msg(result._host.get_name(), result._result,'FAILED'), color=C.COLOR_ERROR)
|
2015-07-23 17:42:20 +00:00
|
|
|
else:
|
2015-12-29 20:41:00 +00:00
|
|
|
self._display.display(msg, color=C.COLOR_ERROR)
|
2015-07-13 19:42:47 +00:00
|
|
|
|
2017-03-23 01:50:28 +00:00
|
|
|
self._display.display("%s | FAILED! => %s" % (result._host.get_name(), self._dump_results(result._result, indent=0).replace('\n','')),
|
|
|
|
color=C.COLOR_ERROR)
|
2015-07-13 19:42:47 +00:00
|
|
|
|
|
|
|
def v2_runner_on_ok(self, result):
|
2015-07-23 17:42:20 +00:00
|
|
|
if result._task.action in C.MODULE_NO_JSON:
|
2015-12-29 20:41:00 +00:00
|
|
|
self._display.display(self._command_generic_msg(result._host.get_name(), result._result,'SUCCESS'), color=C.COLOR_OK)
|
2015-07-23 17:42:20 +00:00
|
|
|
else:
|
2017-03-23 01:50:28 +00:00
|
|
|
self._display.display("%s | SUCCESS => %s" % (result._host.get_name(), self._dump_results(result._result, indent=0).replace('\n','')),
|
|
|
|
color=C.COLOR_OK)
|
2015-07-13 19:42:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
def v2_runner_on_unreachable(self, result):
|
2016-09-14 15:32:30 +00:00
|
|
|
self._display.display("%s | UNREACHABLE!: %s" % (result._host.get_name(), result._result.get('msg','')), color=C.COLOR_UNREACHABLE)
|
2015-07-13 19:53:55 +00:00
|
|
|
|
|
|
|
def v2_runner_on_skipped(self, result):
|
2015-12-29 20:41:00 +00:00
|
|
|
self._display.display("%s | SKIPPED" % (result._host.get_name()), color=C.COLOR_SKIP)
|