PageRenderTime 57ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/ansible/plugins/callback/default.py

https://gitlab.com/0072016/Facebook-SDK-json-
Python | 258 lines | 190 code | 40 blank | 28 comment | 68 complexity | 47ea7bd2418cb43ca488068d6aa4ee3d MD5 | raw file
  1. # (c) 2012-2014, Michael DeHaan <michael.dehaan@gmail.com>
  2. #
  3. # This file is part of Ansible
  4. #
  5. # Ansible is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # Ansible is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
  17. # Make coding more python3-ish
  18. from __future__ import (absolute_import, division, print_function)
  19. __metaclass__ = type
  20. from ansible import constants as C
  21. from ansible.plugins.callback import CallbackBase
  22. from ansible.utils.color import colorize, hostcolor
  23. class CallbackModule(CallbackBase):
  24. '''
  25. This is the default callback interface, which simply prints messages
  26. to stdout when new callback events are received.
  27. '''
  28. CALLBACK_VERSION = 2.0
  29. CALLBACK_TYPE = 'stdout'
  30. CALLBACK_NAME = 'default'
  31. def v2_runner_on_failed(self, result, ignore_errors=False):
  32. delegated_vars = result._result.get('_ansible_delegated_vars', None)
  33. if 'exception' in result._result:
  34. if self._display.verbosity < 3:
  35. # extract just the actual error message from the exception text
  36. error = result._result['exception'].strip().split('\n')[-1]
  37. msg = "An exception occurred during task execution. To see the full traceback, use -vvv. The error was: %s" % error
  38. else:
  39. msg = "An exception occurred during task execution. The full traceback is:\n" + result._result['exception']
  40. self._display.display(msg, color=C.COLOR_ERROR)
  41. if result._task.loop and 'results' in result._result:
  42. self._process_items(result)
  43. else:
  44. if delegated_vars:
  45. self._display.display("fatal: [%s -> %s]: FAILED! => %s" % (result._host.get_name(), delegated_vars['ansible_host'], self._dump_results(result._result)), color=C.COLOR_ERROR)
  46. else:
  47. self._display.display("fatal: [%s]: FAILED! => %s" % (result._host.get_name(), self._dump_results(result._result)), color=C.COLOR_ERROR)
  48. if result._task.ignore_errors:
  49. self._display.display("...ignoring", color=C.COLOR_SKIP)
  50. def v2_runner_on_ok(self, result):
  51. self._clean_results(result._result, result._task.action)
  52. delegated_vars = result._result.get('_ansible_delegated_vars', None)
  53. if result._task.action == 'include':
  54. return
  55. elif result._result.get('changed', False):
  56. if delegated_vars:
  57. msg = "changed: [%s -> %s]" % (result._host.get_name(), delegated_vars['ansible_host'])
  58. else:
  59. msg = "changed: [%s]" % result._host.get_name()
  60. color = C.COLOR_CHANGED
  61. else:
  62. if delegated_vars:
  63. msg = "ok: [%s -> %s]" % (result._host.get_name(), delegated_vars['ansible_host'])
  64. else:
  65. msg = "ok: [%s]" % result._host.get_name()
  66. color = C.COLOR_OK
  67. if result._task.loop and 'results' in result._result:
  68. self._process_items(result)
  69. else:
  70. if (self._display.verbosity > 0 or '_ansible_verbose_always' in result._result) and not '_ansible_verbose_override' in result._result:
  71. msg += " => %s" % (self._dump_results(result._result),)
  72. self._display.display(msg, color=color)
  73. self._handle_warnings(result._result)
  74. def v2_runner_on_skipped(self, result):
  75. if C.DISPLAY_SKIPPED_HOSTS:
  76. if result._task.loop and 'results' in result._result:
  77. self._process_items(result)
  78. else:
  79. msg = "skipping: [%s]" % result._host.get_name()
  80. if (self._display.verbosity > 0 or '_ansible_verbose_always' in result._result) and not '_ansible_verbose_override' in result._result:
  81. msg += " => %s" % self._dump_results(result._result)
  82. self._display.display(msg, color=C.COLOR_SKIP)
  83. def v2_runner_on_unreachable(self, result):
  84. delegated_vars = result._result.get('_ansible_delegated_vars', None)
  85. if delegated_vars:
  86. self._display.display("fatal: [%s -> %s]: UNREACHABLE! => %s" % (result._host.get_name(), delegated_vars['ansible_host'], self._dump_results(result._result)), color=C.COLOR_UNREACHABLE)
  87. else:
  88. self._display.display("fatal: [%s]: UNREACHABLE! => %s" % (result._host.get_name(), self._dump_results(result._result)), color=C.COLOR_UNREACHABLE)
  89. def v2_playbook_on_no_hosts_matched(self):
  90. self._display.display("skipping: no hosts matched", color=C.COLOR_SKIP)
  91. def v2_playbook_on_no_hosts_remaining(self):
  92. self._display.banner("NO MORE HOSTS LEFT")
  93. def v2_playbook_on_task_start(self, task, is_conditional):
  94. args = ''
  95. # args can be specified as no_log in several places: in the task or in
  96. # the argument spec. We can check whether the task is no_log but the
  97. # argument spec can't be because that is only run on the target
  98. # machine and we haven't run it thereyet at this time.
  99. #
  100. # So we give people a config option to affect display of the args so
  101. # that they can secure this if they feel that their stdout is insecure
  102. # (shoulder surfing, logging stdout straight to a file, etc).
  103. if not task.no_log and C.DISPLAY_ARGS_TO_STDOUT:
  104. args = ', '.join(('%s=%s' % a for a in task.args.items()))
  105. args = ' %s' % args
  106. self._display.banner("TASK [%s%s]" % (task.get_name().strip(), args))
  107. if self._display.verbosity >= 2:
  108. path = task.get_path()
  109. if path:
  110. self._display.display("task path: %s" % path, color=C.COLOR_DEBUG)
  111. def v2_playbook_on_cleanup_task_start(self, task):
  112. self._display.banner("CLEANUP TASK [%s]" % task.get_name().strip())
  113. def v2_playbook_on_handler_task_start(self, task):
  114. self._display.banner("RUNNING HANDLER [%s]" % task.get_name().strip())
  115. def v2_playbook_on_play_start(self, play):
  116. name = play.get_name().strip()
  117. if not name:
  118. msg = "PLAY"
  119. else:
  120. msg = "PLAY [%s]" % name
  121. self._display.banner(msg)
  122. def v2_on_file_diff(self, result):
  123. if result._task.loop and 'results' in result._result:
  124. for res in result._result['results']:
  125. if 'diff' in res and res['diff'] and res.get('changed', False):
  126. diff = self._get_diff(res['diff'])
  127. if diff:
  128. self._display.display(diff)
  129. elif 'diff' in result._result and result._result['diff'] and result._result.get('changed', False):
  130. diff = self._get_diff(result._result['diff'])
  131. if diff:
  132. self._display.display(diff)
  133. def v2_runner_item_on_ok(self, result):
  134. delegated_vars = result._result.get('_ansible_delegated_vars', None)
  135. if result._task.action == 'include':
  136. return
  137. elif result._result.get('changed', False):
  138. msg = 'changed'
  139. color = C.COLOR_CHANGED
  140. else:
  141. msg = 'ok'
  142. color = C.COLOR_OK
  143. if delegated_vars:
  144. msg += ": [%s -> %s]" % (result._host.get_name(), delegated_vars['ansible_host'])
  145. else:
  146. msg += ": [%s]" % result._host.get_name()
  147. msg += " => (item=%s)" % (self._get_item(result._result),)
  148. if (self._display.verbosity > 0 or '_ansible_verbose_always' in result._result) and not '_ansible_verbose_override' in result._result:
  149. msg += " => %s" % self._dump_results(result._result)
  150. self._display.display(msg, color=color)
  151. def v2_runner_item_on_failed(self, result):
  152. delegated_vars = result._result.get('_ansible_delegated_vars', None)
  153. if 'exception' in result._result:
  154. if self._display.verbosity < 3:
  155. # extract just the actual error message from the exception text
  156. error = result._result['exception'].strip().split('\n')[-1]
  157. msg = "An exception occurred during task execution. To see the full traceback, use -vvv. The error was: %s" % error
  158. else:
  159. msg = "An exception occurred during task execution. The full traceback is:\n" + result._result['exception']
  160. self._display.display(msg, color=C.COLOR_ERROR)
  161. msg = "failed: "
  162. if delegated_vars:
  163. msg += "[%s -> %s]" % (result._host.get_name(), delegated_vars['ansible_host'])
  164. else:
  165. msg += "[%s]" % (result._host.get_name())
  166. self._display.display(msg + " (item=%s) => %s" % (self._get_item(result._result), self._dump_results(result._result)), color=C.COLOR_ERROR)
  167. self._handle_warnings(result._result)
  168. def v2_runner_item_on_skipped(self, result):
  169. if C.DISPLAY_SKIPPED_HOSTS:
  170. msg = "skipping: [%s] => (item=%s) " % (result._host.get_name(), self._get_item(result._result))
  171. if (self._display.verbosity > 0 or '_ansible_verbose_always' in result._result) and not '_ansible_verbose_override' in result._result:
  172. msg += " => %s" % self._dump_results(result._result)
  173. self._display.display(msg, color=C.COLOR_SKIP)
  174. def v2_playbook_on_include(self, included_file):
  175. msg = 'included: %s for %s' % (included_file._filename, ", ".join([h.name for h in included_file._hosts]))
  176. self._display.display(msg, color=C.COLOR_SKIP)
  177. def v2_playbook_on_stats(self, stats):
  178. self._display.banner("PLAY RECAP")
  179. hosts = sorted(stats.processed.keys())
  180. for h in hosts:
  181. t = stats.summarize(h)
  182. self._display.display(u"%s : %s %s %s %s" % (
  183. hostcolor(h, t),
  184. colorize(u'ok', t['ok'], C.COLOR_OK),
  185. colorize(u'changed', t['changed'], C.COLOR_CHANGED),
  186. colorize(u'unreachable', t['unreachable'], C.COLOR_UNREACHABLE),
  187. colorize(u'failed', t['failures'], C.COLOR_ERROR)),
  188. screen_only=True
  189. )
  190. self._display.display(u"%s : %s %s %s %s" % (
  191. hostcolor(h, t, False),
  192. colorize(u'ok', t['ok'], None),
  193. colorize(u'changed', t['changed'], None),
  194. colorize(u'unreachable', t['unreachable'], None),
  195. colorize(u'failed', t['failures'], None)),
  196. log_only=True
  197. )
  198. self._display.display("", screen_only=True)
  199. def v2_playbook_on_start(self, playbook):
  200. if self._display.verbosity > 1:
  201. from os.path import basename
  202. self._display.banner("PLAYBOOK: %s" % basename(playbook._file_name))
  203. if self._display.verbosity > 3:
  204. if self._options is not None:
  205. for option in dir(self._options):
  206. if option.startswith('_') or option in ['read_file', 'ensure_value', 'read_module']:
  207. continue
  208. val = getattr(self._options,option)
  209. if val:
  210. self._display.vvvv('%s: %s' % (option,val))
  211. def v2_runner_retry(self, result):
  212. msg = "FAILED - RETRYING: %s (%d retries left)." % (result._task, result._result['retries'] - result._result['attempts'])
  213. if (self._display.verbosity > 2 or '_ansible_verbose_always' in result._result) and not '_ansible_verbose_override' in result._result:
  214. msg += "Result was: %s" % self._dump_results(result._result)
  215. self._display.display(msg, color=C.COLOR_DEBUG)