condensed.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. # (c) 2012-2014, Michael DeHaan <michael.dehaan@gmail.com>
  2. # (c) 2017 Ansible Project
  3. # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
  4. # Make coding more python3-ish
  5. from __future__ import (absolute_import, division, print_function)
  6. __metaclass__ = type
  7. DOCUMENTATION = '''
  8. callback: condensed
  9. callback_type: stdout
  10. requirements:
  11. - set as main display callback
  12. short_description: Ansible screen output that ignores skipped status
  13. version_added: "2.0"
  14. deprecated:
  15. why: The 'default' callback plugin now supports this functionality
  16. removed_in: '2.11'
  17. alternative: "'default' callback plugin with 'display_skipped_hosts = no' option"
  18. extends_documentation_fragment:
  19. - default_callback
  20. description:
  21. - This callback does the same as the default except it does not output skipped host/task/item status
  22. '''
  23. from ansible.plugins.callback.default import CallbackModule as CallbackModule_default
  24. from ansible.playbook.task_include import TaskInclude
  25. from ansible.utils.display import Display
  26. from ansible import constants as C
  27. class CondensedDisplay(Display):
  28. def banner(self, msg, color=None, cows=True):
  29. '''
  30. Prints a header-looking line with cowsay or stars with length depending on terminal width (3 minimum)
  31. '''
  32. if self.b_cowsay and cows:
  33. try:
  34. self.banner_cowsay(msg)
  35. return
  36. except OSError:
  37. self.warning("somebody cleverly deleted cowsay or something during the PB run. heh.")
  38. msg = msg.strip()
  39. self.display(u"%s" % (msg), color=color)
  40. class CallbackModule(CallbackModule_default):
  41. '''
  42. This is the default callback interface, which simply prints messages
  43. to stdout when new callback events are received.
  44. '''
  45. CALLBACK_VERSION = 2.0
  46. CALLBACK_TYPE = 'stdout'
  47. CALLBACK_NAME = 'condensed'
  48. def __init__(self):
  49. super(CallbackModule, self).__init__()
  50. self._display = CondensedDisplay()
  51. def v2_playbook_on_include(self, included_file):
  52. basedir = included_file._task._loader.get_basedir()
  53. hosts = ", ".join([h.name for h in included_file._hosts])
  54. filepath = included_file._filename.replace(basedir, '').strip('/')
  55. msg = ' %s => %s' % (hosts, filepath)
  56. self._display.display('INCLUDE TASKS')
  57. self._display.display(msg, color=C.COLOR_SKIP)
  58. def v2_runner_on_ok(self, result):
  59. delegated_vars = result._result.get('_ansible_delegated_vars', None)
  60. if isinstance(result._task, TaskInclude):
  61. return
  62. elif result._result.get('changed', False):
  63. if self._last_task_banner != result._task._uuid:
  64. self._print_task_banner(result._task)
  65. if delegated_vars:
  66. msg = " %s -> %s" % (result._host.get_name(), delegated_vars['ansible_host'])
  67. else:
  68. msg = " %s" % result._host.get_name()
  69. color = C.COLOR_CHANGED
  70. else:
  71. if not self.display_ok_hosts:
  72. return
  73. if self._last_task_banner != result._task._uuid:
  74. self._print_task_banner(result._task)
  75. if delegated_vars:
  76. msg = " %s -> %s" % (result._host.get_name(), delegated_vars['ansible_host'])
  77. else:
  78. msg = " %s" % result._host.get_name()
  79. color = C.COLOR_OK
  80. self._handle_warnings(result._result)
  81. if result._task.loop and 'results' in result._result:
  82. self._process_items(result)
  83. else:
  84. self._clean_results(result._result, result._task.action)
  85. #if self._run_is_verbose(result):
  86. # msg += " => %s" % (self._dump_results(result._result),)
  87. self._display.display(msg, color=color)
  88. def v2_runner_item_on_ok(self, result):
  89. delegated_vars = result._result.get('_ansible_delegated_vars', None)
  90. self._clean_results(result._result, result._task.action)
  91. if isinstance(result._task, TaskInclude):
  92. return
  93. elif result._result.get('changed', False):
  94. if self._last_task_banner != result._task._uuid:
  95. self._print_task_banner(result._task)
  96. color = C.COLOR_CHANGED
  97. else:
  98. if not self.display_ok_hosts:
  99. return
  100. if self._last_task_banner != result._task._uuid:
  101. self._print_task_banner(result._task)
  102. color = C.COLOR_OK
  103. if delegated_vars:
  104. msg = " %s -> %s" % (result._host.get_name(), delegated_vars['ansible_host'])
  105. else:
  106. msg = " %s" % result._host.get_name()
  107. msg += " => (item=%s)" % (self._get_item_label(result._result),)
  108. #if self._run_is_verbose(result):
  109. # msg += " => %s" % self._dump_results(result._result)
  110. self._display.display(msg, color=color)
  111. def v2_runner_on_skipped(self, result):
  112. pass
  113. def v2_runner_item_on_skipped(self, result):
  114. pass