2017-09-19 14:10:21 +00:00
|
|
|
# Copyright: (c) 2012-2014, Michael DeHaan <michael.dehaan@gmail.com>
|
|
|
|
|
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
|
2014-10-15 23:18:12 +00:00
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
|
|
|
|
2017-10-16 13:44:11 +00:00
|
|
|
from copy import deepcopy
|
|
|
|
|
2015-10-26 21:23:09 +00:00
|
|
|
from ansible.parsing.dataloader import DataLoader
|
2017-10-29 04:33:02 +00:00
|
|
|
from ansible.vars.clean import strip_internal_keys
|
2017-10-16 13:44:11 +00:00
|
|
|
|
2017-10-17 01:44:17 +00:00
|
|
|
_IGNORE = ('failed', 'skipped')
|
2017-12-12 18:01:52 +00:00
|
|
|
_PRESERVE = ('attempts', 'changed', 'retries')
|
2014-11-14 22:14:08 +00:00
|
|
|
|
2017-02-24 17:33:24 +00:00
|
|
|
|
2014-11-14 22:14:08 +00:00
|
|
|
class TaskResult:
|
|
|
|
'''
|
2016-12-11 02:50:09 +00:00
|
|
|
This class is responsible for interpreting the resulting data
|
2014-11-14 22:14:08 +00:00
|
|
|
from an executed task, and provides helper methods for determining
|
|
|
|
the result of a given task.
|
|
|
|
'''
|
|
|
|
|
2017-02-17 15:14:26 +00:00
|
|
|
def __init__(self, host, task, return_data, task_fields=None):
|
2014-11-14 22:14:08 +00:00
|
|
|
self._host = host
|
|
|
|
self._task = task
|
2017-02-17 15:14:26 +00:00
|
|
|
|
2014-11-14 22:14:08 +00:00
|
|
|
if isinstance(return_data, dict):
|
|
|
|
self._result = return_data.copy()
|
|
|
|
else:
|
|
|
|
self._result = DataLoader().load(return_data)
|
|
|
|
|
2017-02-17 15:14:26 +00:00
|
|
|
if task_fields is None:
|
|
|
|
self._task_fields = dict()
|
|
|
|
else:
|
|
|
|
self._task_fields = task_fields
|
|
|
|
|
2017-02-24 17:33:24 +00:00
|
|
|
@property
|
|
|
|
def task_name(self):
|
|
|
|
return self._task_fields.get('name', None) or self._task.get_name()
|
|
|
|
|
2014-11-14 22:14:08 +00:00
|
|
|
def is_changed(self):
|
|
|
|
return self._check_key('changed')
|
|
|
|
|
|
|
|
def is_skipped(self):
|
2016-05-05 22:26:40 +00:00
|
|
|
# loop results
|
2016-07-31 08:23:28 +00:00
|
|
|
if 'results' in self._result:
|
2016-05-04 05:14:28 +00:00
|
|
|
results = self._result['results']
|
2016-05-05 22:26:40 +00:00
|
|
|
# Loop tasks are only considered skipped if all items were skipped.
|
|
|
|
# some squashed results (eg, yum) are not dicts and can't be skipped individually
|
|
|
|
if results and all(isinstance(res, dict) and res.get('skipped', False) for res in results):
|
|
|
|
return True
|
|
|
|
|
|
|
|
# regular tasks and squashed non-dict results
|
|
|
|
return self._result.get('skipped', False)
|
2014-11-14 22:14:08 +00:00
|
|
|
|
|
|
|
def is_failed(self):
|
2015-06-26 20:54:13 +00:00
|
|
|
if 'failed_when_result' in self._result or \
|
|
|
|
'results' in self._result and True in [True for x in self._result['results'] if 'failed_when_result' in x]:
|
2015-01-19 22:18:18 +00:00
|
|
|
return self._check_key('failed_when_result')
|
|
|
|
else:
|
2017-06-21 10:33:42 +00:00
|
|
|
return self._check_key('failed')
|
2014-11-14 22:14:08 +00:00
|
|
|
|
|
|
|
def is_unreachable(self):
|
|
|
|
return self._check_key('unreachable')
|
|
|
|
|
|
|
|
def _check_key(self, key):
|
2017-09-19 14:10:21 +00:00
|
|
|
'''get a specific key from the result or its items'''
|
2016-10-27 13:43:49 +00:00
|
|
|
|
|
|
|
if isinstance(self._result, dict) and key in self._result:
|
|
|
|
return self._result.get(key, False)
|
|
|
|
else:
|
2014-11-14 22:14:08 +00:00
|
|
|
flag = False
|
|
|
|
for res in self._result.get('results', []):
|
2015-01-20 07:16:19 +00:00
|
|
|
if isinstance(res, dict):
|
|
|
|
flag |= res.get(key, False)
|
2015-06-17 19:38:52 +00:00
|
|
|
return flag
|
2017-10-16 13:44:11 +00:00
|
|
|
|
|
|
|
def clean_copy(self):
|
|
|
|
|
|
|
|
''' returns 'clean' taskresult object '''
|
|
|
|
|
|
|
|
# FIXME: clean task_fields, _task and _host copies
|
|
|
|
result = TaskResult(self._host, self._task, {}, self._task_fields)
|
|
|
|
|
|
|
|
# statuses are already reflected on the event type
|
|
|
|
if result._task and result._task.action in ['debug']:
|
|
|
|
# debug is verbose by default to display vars, no need to add invocation
|
|
|
|
ignore = _IGNORE + ('invocation',)
|
|
|
|
else:
|
|
|
|
ignore = _IGNORE
|
|
|
|
|
|
|
|
if self._result.get('_ansible_no_log', False):
|
2017-12-12 18:01:52 +00:00
|
|
|
x = {"censored": "the output has been hidden due to the fact that 'no_log: true' was specified for this result"}
|
|
|
|
for preserve in _PRESERVE:
|
|
|
|
if preserve in self._result:
|
|
|
|
x[preserve] = self._result[preserve]
|
|
|
|
result._result = x
|
2017-10-16 13:44:11 +00:00
|
|
|
elif self._result:
|
|
|
|
result._result = deepcopy(self._result)
|
|
|
|
|
|
|
|
# actualy remove
|
|
|
|
for remove_key in ignore:
|
|
|
|
if remove_key in result._result:
|
|
|
|
del result._result[remove_key]
|
|
|
|
|
|
|
|
# remove almost ALL internal keys, keep ones relevant to callback
|
|
|
|
strip_internal_keys(result._result, exceptions=('_ansible_verbose_always', '_ansible_item_label', '_ansible_no_log'))
|
|
|
|
|
|
|
|
return result
|