Merge "Add spacing lines and formatting to result dicts" into feature/zuulv3
This commit is contained in:
commit
34bafd22eb
|
@ -13,8 +13,15 @@
|
|||
# You should have received a copy of the GNU General Public License
|
||||
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
# This is not needed in python3 - but it is needed in python2 because there
|
||||
# is a json module in ansible.plugins.callback and python2 gets confused.
|
||||
# Easy local testing with ansible-playbook is handy when hacking on zuul_stream
|
||||
# so just put in the __future__ statement.
|
||||
from __future__ import absolute_import
|
||||
|
||||
import datetime
|
||||
import logging
|
||||
import json
|
||||
import os
|
||||
import socket
|
||||
import threading
|
||||
|
@ -153,6 +160,8 @@ class CallbackModule(default.CallbackModule):
|
|||
playbook=self._playbook_name, name=name)
|
||||
|
||||
self._log(msg)
|
||||
# Log an extra blank line to get space after each play
|
||||
self._log("")
|
||||
|
||||
def v2_playbook_on_task_start(self, task, is_conditional):
|
||||
self._task = task
|
||||
|
@ -234,12 +243,11 @@ class CallbackModule(default.CallbackModule):
|
|||
pass
|
||||
else:
|
||||
self._log_message(
|
||||
result=result,
|
||||
msg="Results: => {results}".format(
|
||||
results=self._dump_results(result_dict)),
|
||||
status='ERROR')
|
||||
result=result, status='ERROR', result_dict=result_dict)
|
||||
if ignore_errors:
|
||||
self._log_message(result, "Ignoring Errors", status="ERROR")
|
||||
# Log an extra blank line to get space after each task
|
||||
self._log("")
|
||||
|
||||
def v2_runner_on_ok(self, result):
|
||||
if (self._play.strategy == 'free'
|
||||
|
@ -285,9 +293,8 @@ class CallbackModule(default.CallbackModule):
|
|||
elif result._task.action not in ('command', 'shell'):
|
||||
self._log_message(
|
||||
result=result,
|
||||
msg="Results: => {results}".format(
|
||||
results=self._dump_results(result_dict)),
|
||||
status=status)
|
||||
status=status,
|
||||
result_dict=result_dict)
|
||||
elif 'results' in result_dict:
|
||||
for res in result_dict['results']:
|
||||
self._log_message(
|
||||
|
@ -300,6 +307,8 @@ class CallbackModule(default.CallbackModule):
|
|||
result,
|
||||
"Runtime: {delta} Start: {start} End: {end}".format(
|
||||
**result_dict))
|
||||
# Log an extra blank line to get space after each task
|
||||
self._log("")
|
||||
|
||||
def v2_runner_item_on_ok(self, result):
|
||||
result_dict = dict(result._result)
|
||||
|
@ -313,9 +322,8 @@ class CallbackModule(default.CallbackModule):
|
|||
if result._task.action not in ('command', 'shell'):
|
||||
self._log_message(
|
||||
result=result,
|
||||
msg="Item: {item} => {results}".format(
|
||||
item=result_dict['item'],
|
||||
results=self._dump_results(result_dict)),
|
||||
msg="Item: {item}".format(item=result_dict['item']),
|
||||
result_dict=result_dict,
|
||||
status=status)
|
||||
else:
|
||||
self._log_message(
|
||||
|
@ -325,6 +333,8 @@ class CallbackModule(default.CallbackModule):
|
|||
|
||||
if self._deferred_result:
|
||||
self._process_deferred(result)
|
||||
# Log an extra blank line to get space after each task
|
||||
self._log("")
|
||||
|
||||
def v2_runner_item_on_failed(self, result):
|
||||
result_dict = dict(result._result)
|
||||
|
@ -333,10 +343,9 @@ class CallbackModule(default.CallbackModule):
|
|||
if result._task.action not in ('command', 'shell'):
|
||||
self._log_message(
|
||||
result=result,
|
||||
msg="Item: {item} => {results}".format(
|
||||
item=result_dict['item'],
|
||||
results=self._dump_results(result_dict)),
|
||||
status='ERROR')
|
||||
msg="Item: {item}".format(item=result_dict['item']),
|
||||
status='ERROR',
|
||||
result_dict=result_dict)
|
||||
else:
|
||||
self._log_message(
|
||||
result,
|
||||
|
@ -345,6 +354,8 @@ class CallbackModule(default.CallbackModule):
|
|||
|
||||
if self._deferred_result:
|
||||
self._process_deferred(result)
|
||||
# Log an extra blank line to get space after each task
|
||||
self._log("")
|
||||
|
||||
def _process_deferred(self, result):
|
||||
self._items_done = True
|
||||
|
@ -383,6 +394,8 @@ class CallbackModule(default.CallbackModule):
|
|||
task=task_name,
|
||||
args=args)
|
||||
self._log(msg)
|
||||
# Log an extra blank line to get space after each task
|
||||
self._log("")
|
||||
return task
|
||||
|
||||
def _get_task_hosts(self, task):
|
||||
|
@ -401,10 +414,26 @@ class CallbackModule(default.CallbackModule):
|
|||
hosts = play_vars.keys()
|
||||
return hosts
|
||||
|
||||
def _log_message(self, result, msg, status="ok"):
|
||||
def _dump_result_dict(self, result_dict):
|
||||
result_dict = result_dict.copy()
|
||||
for key in result_dict.keys():
|
||||
if key.startswith('_ansible') or key == 'zuul_log_id':
|
||||
del result_dict[key]
|
||||
return result_dict
|
||||
|
||||
def _log_message(self, result, msg=None, status="ok", result_dict=None):
|
||||
hostname = self._get_hostname(result)
|
||||
self._log("{host} | {status}: {msg}".format(
|
||||
host=hostname, status=status, msg=msg))
|
||||
if msg:
|
||||
self._log("{host} | {status}: {msg}".format(
|
||||
host=hostname, status=status, msg=msg))
|
||||
else:
|
||||
self._log("{host} | {status}".format(
|
||||
host=hostname, status=status, msg=msg))
|
||||
if result_dict:
|
||||
result_string = json.dumps(self._dump_result_dict(result_dict),
|
||||
indent=2, sort_keys=True)
|
||||
for line in result_string.split('\n'):
|
||||
self._log("{host} | {line}".format(host=hostname, line=line))
|
||||
|
||||
def _get_hostname(self, result):
|
||||
delegated_vars = result._result.get('_ansible_delegated_vars', None)
|
||||
|
|
Loading…
Reference in New Issue