zz_failure_summary.py 3.7 KB

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