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
|
|
|
|
|
2018-05-31 15:14:26 +00:00
|
|
|
from ansible import constants as C
|
2015-10-26 21:23:09 +00:00
|
|
|
from ansible.parsing.dataloader import DataLoader
|
2018-08-20 20:08:29 +00:00
|
|
|
from ansible.vars.clean import module_response_deepcopy, strip_internal_keys
|
2017-10-16 13:44:11 +00:00
|
|
|
|
2018-02-20 13:41:18 +00:00
|
|
|
_IGNORE = ('failed', 'skipped')
|
|
|
|
_PRESERVE = ('attempts', 'changed', 'retries')
|
2018-07-12 00:41:37 +00:00
|
|
|
_SUB_PRESERVE = {'_ansible_delegated_vars': ('ansible_host', 'ansible_port', 'ansible_user', 'ansible_connection')}
|
2014-11-14 22:14:08 +00:00
|
|
|
|
2019-03-12 22:18:38 +00:00
|
|
|
# stuff callbacks need
|
|
|
|
CLEAN_EXCEPTIONS = (
|
|
|
|
'_ansible_verbose_always', # for debug and other actions, to always expand data (pretty jsonification)
|
|
|
|
'_ansible_item_label', # to know actual 'item' variable
|
|
|
|
'_ansible_no_log', # jic we didnt clean up well enough, DON'T LOG
|
|
|
|
'_ansible_verbose_override', # controls display of ansible_facts, gathering would be very noise with -v otherwise
|
|
|
|
)
|
|
|
|
|
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')
|
|
|
|
|
Provide a way to explicitly invoke the debugger (#34006)
* Provide a way to explicitly invoke the debugger with in the debug strategy
* Merge the debugger strategy into StrategyBase
* Fix some logic, pin to a single result
* Make redo also continue
* Make sure that if the debug closure doesn't need to process the result, that we still return it
* Fix failing tests for the strategy
* Clean up messages from debugger and exit code to match bin/ansible
* Move the FieldAttribute higher, to apply at different levels
* make debugger a string, expand logic
* Better host state rollbacks
* More explicit debugger prompt
* ENABLE_TASK_DEBUGGER should be boolean, and better docs
* No bare except, add pprint, alias h, vars to task_vars
* _validate_debugger can ignore non-string, that can be caught later
* Address issue if there were no previous tasks/state, and use the correct key
* Update docs for changes to the debugger
* Guard against a stat going negative through use of decrement
* Add a few notes about using the debugger on the free strategy
* Add changelog entry for task debugger
* Add a few versionadded indicators and a note about vars -> task_vars
2018-01-09 19:50:07 +00:00
|
|
|
def needs_debugger(self, globally_enabled=False):
|
|
|
|
_debugger = self._task_fields.get('debugger')
|
2018-05-31 15:14:26 +00:00
|
|
|
_ignore_errors = C.TASK_DEBUGGER_IGNORE_ERRORS and self._task_fields.get('ignore_errors')
|
Provide a way to explicitly invoke the debugger (#34006)
* Provide a way to explicitly invoke the debugger with in the debug strategy
* Merge the debugger strategy into StrategyBase
* Fix some logic, pin to a single result
* Make redo also continue
* Make sure that if the debug closure doesn't need to process the result, that we still return it
* Fix failing tests for the strategy
* Clean up messages from debugger and exit code to match bin/ansible
* Move the FieldAttribute higher, to apply at different levels
* make debugger a string, expand logic
* Better host state rollbacks
* More explicit debugger prompt
* ENABLE_TASK_DEBUGGER should be boolean, and better docs
* No bare except, add pprint, alias h, vars to task_vars
* _validate_debugger can ignore non-string, that can be caught later
* Address issue if there were no previous tasks/state, and use the correct key
* Update docs for changes to the debugger
* Guard against a stat going negative through use of decrement
* Add a few notes about using the debugger on the free strategy
* Add changelog entry for task debugger
* Add a few versionadded indicators and a note about vars -> task_vars
2018-01-09 19:50:07 +00:00
|
|
|
|
|
|
|
ret = False
|
2018-05-31 15:14:26 +00:00
|
|
|
if globally_enabled and ((self.is_failed() and not _ignore_errors) or self.is_unreachable()):
|
Provide a way to explicitly invoke the debugger (#34006)
* Provide a way to explicitly invoke the debugger with in the debug strategy
* Merge the debugger strategy into StrategyBase
* Fix some logic, pin to a single result
* Make redo also continue
* Make sure that if the debug closure doesn't need to process the result, that we still return it
* Fix failing tests for the strategy
* Clean up messages from debugger and exit code to match bin/ansible
* Move the FieldAttribute higher, to apply at different levels
* make debugger a string, expand logic
* Better host state rollbacks
* More explicit debugger prompt
* ENABLE_TASK_DEBUGGER should be boolean, and better docs
* No bare except, add pprint, alias h, vars to task_vars
* _validate_debugger can ignore non-string, that can be caught later
* Address issue if there were no previous tasks/state, and use the correct key
* Update docs for changes to the debugger
* Guard against a stat going negative through use of decrement
* Add a few notes about using the debugger on the free strategy
* Add changelog entry for task debugger
* Add a few versionadded indicators and a note about vars -> task_vars
2018-01-09 19:50:07 +00:00
|
|
|
ret = True
|
|
|
|
|
|
|
|
if _debugger in ('always',):
|
|
|
|
ret = True
|
|
|
|
elif _debugger in ('never',):
|
|
|
|
ret = False
|
2018-05-31 15:14:26 +00:00
|
|
|
elif _debugger in ('on_failed',) and self.is_failed() and not _ignore_errors:
|
Provide a way to explicitly invoke the debugger (#34006)
* Provide a way to explicitly invoke the debugger with in the debug strategy
* Merge the debugger strategy into StrategyBase
* Fix some logic, pin to a single result
* Make redo also continue
* Make sure that if the debug closure doesn't need to process the result, that we still return it
* Fix failing tests for the strategy
* Clean up messages from debugger and exit code to match bin/ansible
* Move the FieldAttribute higher, to apply at different levels
* make debugger a string, expand logic
* Better host state rollbacks
* More explicit debugger prompt
* ENABLE_TASK_DEBUGGER should be boolean, and better docs
* No bare except, add pprint, alias h, vars to task_vars
* _validate_debugger can ignore non-string, that can be caught later
* Address issue if there were no previous tasks/state, and use the correct key
* Update docs for changes to the debugger
* Guard against a stat going negative through use of decrement
* Add a few notes about using the debugger on the free strategy
* Add changelog entry for task debugger
* Add a few versionadded indicators and a note about vars -> task_vars
2018-01-09 19:50:07 +00:00
|
|
|
ret = True
|
|
|
|
elif _debugger in ('on_unreachable',) and self.is_unreachable():
|
|
|
|
ret = True
|
|
|
|
elif _debugger in('on_skipped',) and self.is_skipped():
|
|
|
|
ret = True
|
|
|
|
|
|
|
|
return ret
|
|
|
|
|
2014-11-14 22:14:08 +00:00
|
|
|
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
|
|
|
|
|
2018-08-22 01:53:56 +00:00
|
|
|
if isinstance(self._task.no_log, bool) and self._task.no_log or 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"}
|
2018-07-12 00:41:37 +00:00
|
|
|
|
|
|
|
# preserve full
|
2017-12-12 18:01:52 +00:00
|
|
|
for preserve in _PRESERVE:
|
|
|
|
if preserve in self._result:
|
|
|
|
x[preserve] = self._result[preserve]
|
2018-07-12 00:41:37 +00:00
|
|
|
|
|
|
|
# preserve subset
|
|
|
|
for sub in _SUB_PRESERVE:
|
|
|
|
if sub in self._result:
|
|
|
|
x[sub] = {}
|
|
|
|
for key in _SUB_PRESERVE[sub]:
|
2018-07-24 12:54:58 +00:00
|
|
|
if key in self._result[sub]:
|
|
|
|
x[sub][key] = self._result[sub][key]
|
2018-07-12 00:41:37 +00:00
|
|
|
|
2017-12-12 18:01:52 +00:00
|
|
|
result._result = x
|
2017-10-16 13:44:11 +00:00
|
|
|
elif self._result:
|
2018-08-20 20:08:29 +00:00
|
|
|
result._result = module_response_deepcopy(self._result)
|
2017-10-16 13:44:11 +00:00
|
|
|
|
|
|
|
# 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
|
2019-03-12 22:18:38 +00:00
|
|
|
strip_internal_keys(result._result, exceptions=CLEAN_EXCEPTIONS)
|
2017-10-16 13:44:11 +00:00
|
|
|
|
|
|
|
return result
|