default_plus_summary.py 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # vim: expandtab:tabstop=4:shiftwidth=4
  2. '''
  3. Ansible callback plugin.
  4. '''
  5. from ansible.plugins.callback.default import CallbackModule as CallbackModule_default
  6. from ansible import constants as C
  7. from ansible.utils.color import stringc
  8. class CallbackModule(CallbackModule_default):
  9. '''
  10. This is like the default callback plugin, but also stores results and
  11. summarizes failures.
  12. '''
  13. CALLBACK_VERSION = 2.0
  14. CALLBACK_TYPE = 'stdout'
  15. CALLBACK_NAME = 'default_plus_summary'
  16. def __init__(self):
  17. super(CallbackModule, self).__init__()
  18. self.__failures = []
  19. def v2_runner_on_failed(self, result, ignore_errors=False):
  20. super(CallbackModule, self).v2_runner_on_failed(result, ignore_errors)
  21. self.__failures.append(dict(result=result, ignore_errors=ignore_errors))
  22. def v2_playbook_on_stats(self, stats):
  23. super(CallbackModule, self).v2_playbook_on_stats(stats)
  24. # TODO: update condition to consider a host var or env var to
  25. # enable/disable the summary, so that we can control the output from a
  26. # play.
  27. if self.__failures:
  28. self._print_failure_summary()
  29. def _print_failure_summary(self):
  30. '''Print a summary of failed tasks (including ignored failures).'''
  31. self._display.display(u'\nFailure summary:\n')
  32. # TODO: group failures by host or by task. If grouped by host, it is
  33. # easy to see all problems of a given host. If grouped by task, it is
  34. # easy to see what hosts needs the same fix.
  35. width = len(str(len(self.__failures)))
  36. initial_indent_format = u' {{:>{width}}}. '.format(width=width)
  37. initial_indent_len = len(initial_indent_format.format(0))
  38. subsequent_indent = u' ' * initial_indent_len
  39. subsequent_extra_indent = u' ' * (initial_indent_len + 10)
  40. for i, failure in enumerate(self.__failures, 1):
  41. lines = _format_failure(failure)
  42. self._display.display(u'\n{}{}'.format(initial_indent_format.format(i), lines[0]))
  43. for line in lines[1:]:
  44. line = line.replace(u'\n', u'\n' + subsequent_extra_indent)
  45. indented = u'{}{}'.format(subsequent_indent, line)
  46. self._display.display(indented)
  47. # Reason: disable pylint protected-access because we need to access _*
  48. # attributes of a task result to implement this method.
  49. # Status: permanently disabled unless Ansible's API changes.
  50. # pylint: disable=protected-access
  51. def _format_failure(failure):
  52. '''Return a list of pretty-formatted lines describing a failure, including
  53. relevant information about it. Line separators are not included.'''
  54. result = failure['result']
  55. host = result._host.get_name()
  56. play = _get_play(result._task)
  57. if play:
  58. play = play.get_name()
  59. task = result._task.get_name()
  60. msg = result._result.get('msg', u'???')
  61. rows = (
  62. (u'Host', host),
  63. (u'Play', play),
  64. (u'Task', task),
  65. (u'Message', stringc(msg, C.COLOR_ERROR)),
  66. )
  67. row_format = '{:10}{}'
  68. return [row_format.format(header + u':', body) for header, body in rows]
  69. # Reason: disable pylint protected-access because we need to access _*
  70. # attributes of obj to implement this function.
  71. # This is inspired by ansible.playbook.base.Base.dump_me.
  72. # Status: permanently disabled unless Ansible's API changes.
  73. # pylint: disable=protected-access
  74. def _get_play(obj):
  75. '''Given a task or block, recursively tries to find its parent play.'''
  76. if hasattr(obj, '_play'):
  77. return obj._play
  78. if getattr(obj, '_parent'):
  79. return _get_play(obj._parent)