use get_option instead of directly internal dict ()

* use get_option instead of directly internal dict

as get_option can populate it, while direct access cannot
also setup fallback values for derived plugins not using config system correctly
This commit is contained in:
Brian Coca 2018-07-13 15:54:52 -04:00 committed by GitHub
parent a97618b486
commit f1dcfe4f4c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -29,11 +29,13 @@ from ansible.utils.color import colorize, hostcolor
# plugin are not also added to ansible.constants, so authors of derivative # plugin are not also added to ansible.constants, so authors of derivative
# callback plugins will eventually need to add a reference to the common docs # callback plugins will eventually need to add a reference to the common docs
# fragment for the 'default' callback plugin # fragment for the 'default' callback plugin
DEFAULT_SHOW_CUSTOM_STATS = C.SHOW_CUSTOM_STATS
DEFAULT_DISPLAY_SKIPPED_HOSTS = C.DISPLAY_SKIPPED_HOSTS # these are used to provide backwards compat with old plugins that subclass from default
# These values reflect the defaults defined in the config fragment in the docs # but still don't use the new config system and/or fail to document the options
DEFAULT_DISPLAY_OK_HOSTS = True COMPAT_OPTIONS = (('display_skipped_hosts', C.DISPLAY_SKIPPED_HOSTS),
DEFAULT_DISPLAY_FAILED_STDERR = False ('display_ok_hosts', True),
('show_custom_stats', C.SHOW_CUSTOM_STATS),
('display_failed_stderr', False),)
class CallbackModule(CallbackBase): class CallbackModule(CallbackBase):
@ -54,6 +56,18 @@ class CallbackModule(CallbackBase):
self._last_task_name = None self._last_task_name = None
super(CallbackModule, self).__init__() super(CallbackModule, self).__init__()
def set_options(self, task_keys=None, var_options=None, direct=None):
super(CallbackModule, self).set_options(task_keys=task_keys, var_options=var_options, direct=direct)
# for backwards compat with plugins subclassing default, fallback to constants
for option, constant in COMPAT_OPTIONS:
try:
value = self.get_option(option)
except (AttributeError, KeyError):
value = constant
setattr(self, option, value)
def v2_runner_on_failed(self, result, ignore_errors=False): def v2_runner_on_failed(self, result, ignore_errors=False):
delegated_vars = result._result.get('_ansible_delegated_vars', None) delegated_vars = result._result.get('_ansible_delegated_vars', None)
@ -62,9 +76,7 @@ class CallbackModule(CallbackBase):
if self._last_task_banner != result._task._uuid: if self._last_task_banner != result._task._uuid:
self._print_task_banner(result._task) self._print_task_banner(result._task)
use_stderr = self._plugin_options.get('display_failed_stderr', DEFAULT_DISPLAY_FAILED_STDERR) self._handle_exception(result._result, use_stderr=self.display_failed_stderr)
self._handle_exception(result._result, use_stderr=use_stderr)
self._handle_warnings(result._result) self._handle_warnings(result._result)
if result._task.loop and 'results' in result._result: if result._task.loop and 'results' in result._result:
@ -74,51 +86,52 @@ class CallbackModule(CallbackBase):
if delegated_vars: if delegated_vars:
self._display.display("fatal: [%s -> %s]: FAILED! => %s" % (result._host.get_name(), delegated_vars['ansible_host'], self._display.display("fatal: [%s -> %s]: FAILED! => %s" % (result._host.get_name(), delegated_vars['ansible_host'],
self._dump_results(result._result)), self._dump_results(result._result)),
color=C.COLOR_ERROR, stderr=use_stderr) color=C.COLOR_ERROR, stderr=self.display_failed_stderr)
else: else:
self._display.display("fatal: [%s]: FAILED! => %s" % (result._host.get_name(), self._dump_results(result._result)), self._display.display("fatal: [%s]: FAILED! => %s" % (result._host.get_name(), self._dump_results(result._result)),
color=C.COLOR_ERROR, stderr=use_stderr) color=C.COLOR_ERROR, stderr=self.display_failed_stderr)
if ignore_errors: if ignore_errors:
self._display.display("...ignoring", color=C.COLOR_SKIP) self._display.display("...ignoring", color=C.COLOR_SKIP)
def v2_runner_on_ok(self, result): def v2_runner_on_ok(self, result):
if not self._plugin_options.get('display_ok_hosts', DEFAULT_DISPLAY_OK_HOSTS):
return
delegated_vars = result._result.get('_ansible_delegated_vars', None) if self.display_ok_hosts:
if self._last_task_banner != result._task._uuid: delegated_vars = result._result.get('_ansible_delegated_vars', None)
self._print_task_banner(result._task)
if isinstance(result._task, TaskInclude): if self._last_task_banner != result._task._uuid:
return self._print_task_banner(result._task)
elif result._result.get('changed', False):
if delegated_vars: if isinstance(result._task, TaskInclude):
msg = "changed: [%s -> %s]" % (result._host.get_name(), delegated_vars['ansible_host']) return
elif result._result.get('changed', False):
if delegated_vars:
msg = "changed: [%s -> %s]" % (result._host.get_name(), delegated_vars['ansible_host'])
else:
msg = "changed: [%s]" % result._host.get_name()
color = C.COLOR_CHANGED
else: else:
msg = "changed: [%s]" % result._host.get_name() if delegated_vars:
color = C.COLOR_CHANGED msg = "ok: [%s -> %s]" % (result._host.get_name(), delegated_vars['ansible_host'])
else: else:
if delegated_vars: msg = "ok: [%s]" % result._host.get_name()
msg = "ok: [%s -> %s]" % (result._host.get_name(), delegated_vars['ansible_host']) color = C.COLOR_OK
self._handle_warnings(result._result)
if result._task.loop and 'results' in result._result:
self._process_items(result)
else: else:
msg = "ok: [%s]" % result._host.get_name() self._clean_results(result._result, result._task.action)
color = C.COLOR_OK
self._handle_warnings(result._result) if (self._display.verbosity > 0 or '_ansible_verbose_always' in result._result) and '_ansible_verbose_override' not in result._result:
msg += " => %s" % (self._dump_results(result._result),)
if result._task.loop and 'results' in result._result: self._display.display(msg, color=color)
self._process_items(result)
else:
self._clean_results(result._result, result._task.action)
if (self._display.verbosity > 0 or '_ansible_verbose_always' in result._result) and '_ansible_verbose_override' not in result._result:
msg += " => %s" % (self._dump_results(result._result),)
self._display.display(msg, color=color)
def v2_runner_on_skipped(self, result): def v2_runner_on_skipped(self, result):
if self._plugin_options.get('display_skipped_hosts', DEFAULT_DISPLAY_SKIPPED_HOSTS):
if self.display_skipped_hosts:
self._clean_results(result._result, result._task.action) self._clean_results(result._result, result._task.action)
@ -158,8 +171,7 @@ class CallbackModule(CallbackBase):
self._last_task_name = task.get_name().strip() self._last_task_name = task.get_name().strip()
# Display the task banner immediately if we're not doing any filtering based on task result # Display the task banner immediately if we're not doing any filtering based on task result
if (self._plugin_options.get('display_skipped_hosts', DEFAULT_DISPLAY_SKIPPED_HOSTS) and if self.display_skipped_hosts and self.display_ok_hosts:
self._plugin_options.get('display_ok_hosts', DEFAULT_DISPLAY_OK_HOSTS)):
self._print_task_banner(task) self._print_task_banner(task)
def _print_task_banner(self, task): def _print_task_banner(self, task):
@ -222,30 +234,30 @@ class CallbackModule(CallbackBase):
self._display.display(diff) self._display.display(diff)
def v2_runner_item_on_ok(self, result): def v2_runner_item_on_ok(self, result):
if not self._plugin_options.get('display_ok_hosts', DEFAULT_DISPLAY_OK_HOSTS):
return
delegated_vars = result._result.get('_ansible_delegated_vars', None) if self.display_ok_hosts:
self._clean_results(result._result, result._task.action)
if isinstance(result._task, TaskInclude):
return
elif result._result.get('changed', False):
msg = 'changed'
color = C.COLOR_CHANGED
else:
msg = 'ok'
color = C.COLOR_OK
if delegated_vars: delegated_vars = result._result.get('_ansible_delegated_vars', None)
msg += ": [%s -> %s]" % (result._host.get_name(), delegated_vars['ansible_host']) self._clean_results(result._result, result._task.action)
else: if isinstance(result._task, TaskInclude):
msg += ": [%s]" % result._host.get_name() return
elif result._result.get('changed', False):
msg = 'changed'
color = C.COLOR_CHANGED
else:
msg = 'ok'
color = C.COLOR_OK
msg += " => (item=%s)" % (self._get_item_label(result._result),) if delegated_vars:
msg += ": [%s -> %s]" % (result._host.get_name(), delegated_vars['ansible_host'])
else:
msg += ": [%s]" % result._host.get_name()
if (self._display.verbosity > 0 or '_ansible_verbose_always' in result._result) and '_ansible_verbose_override' not in result._result: msg += " => (item=%s)" % (self._get_item_label(result._result),)
msg += " => %s" % self._dump_results(result._result)
self._display.display(msg, color=color) if (self._display.verbosity > 0 or '_ansible_verbose_always' in result._result) and '_ansible_verbose_override' not in result._result:
msg += " => %s" % self._dump_results(result._result)
self._display.display(msg, color=color)
def v2_runner_item_on_failed(self, result): def v2_runner_item_on_failed(self, result):
@ -263,7 +275,7 @@ class CallbackModule(CallbackBase):
self._display.display(msg + " (item=%s) => %s" % (self._get_item_label(result._result), self._dump_results(result._result)), color=C.COLOR_ERROR) self._display.display(msg + " (item=%s) => %s" % (self._get_item_label(result._result), self._dump_results(result._result)), color=C.COLOR_ERROR)
def v2_runner_item_on_skipped(self, result): def v2_runner_item_on_skipped(self, result):
if self._plugin_options.get('display_skipped_hosts', DEFAULT_DISPLAY_SKIPPED_HOSTS): if self.display_skipped_hosts:
self._clean_results(result._result, result._task.action) self._clean_results(result._result, result._task.action)
msg = "skipping: [%s] => (item=%s) " % (result._host.get_name(), self._get_item_label(result._result)) msg = "skipping: [%s] => (item=%s) " % (result._host.get_name(), self._get_item_label(result._result))
if (self._display.verbosity > 0 or '_ansible_verbose_always' in result._result) and '_ansible_verbose_override' not in result._result: if (self._display.verbosity > 0 or '_ansible_verbose_always' in result._result) and '_ansible_verbose_override' not in result._result:
@ -303,8 +315,8 @@ class CallbackModule(CallbackBase):
self._display.display("", screen_only=True) self._display.display("", screen_only=True)
# print custom stats # print custom stats if required
if self._plugin_options.get('show_custom_stats', DEFAULT_SHOW_CUSTOM_STATS) and stats.custom: if stats.custom and self.show_custom_stats:
self._display.banner("CUSTOM STATS: ") self._display.banner("CUSTOM STATS: ")
# per host # per host
# TODO: come up with 'pretty format' # TODO: come up with 'pretty format'