better backwards compat handling of status

restored 'rc' inspection but only when failed is not specified
removed redundant changed from basic.py as task_executor already adds
removed redundant filters, they are tests
added aliases to tests removed from filters
fixed test to new rc handling
This commit is contained in:
Brian Coca
2017-07-05 13:50:32 -04:00
committed by Brian Coca
parent fba76444e0
commit 2a041d10d2
6 changed files with 36 additions and 101 deletions

View File

@@ -572,7 +572,11 @@ class TaskExecutor:
# set the failed property if it was missing.
if 'failed' not in result:
result['failed'] = False
# rc is here for backwards compatibility and modules that use it instead of 'failed'
if 'rc' in result and result['rc'] not in [0, "0"]:
result['failed'] = True
else:
result['failed'] = False
# set the changed property if it was missing.
if 'changed' not in result:

View File

@@ -2118,9 +2118,6 @@ class AnsibleModule(object):
def exit_json(self, **kwargs):
''' return from the module, without error '''
if 'changed' not in kwargs:
kwargs['changed'] = False
self.do_cleanup_files()
self._return_formatted(kwargs)
sys.exit(0)
@@ -2131,9 +2128,6 @@ class AnsibleModule(object):
assert 'msg' in kwargs, "implementation error -- msg to explain the error is required"
kwargs['failed'] = True
if 'changed' not in kwargs:
kwargs['changed'] = False
# add traceback if debug or high verbosity and it is missing
# Note: badly named as exception, it is really always been 'traceback'
if 'exception' not in kwargs and sys.exc_info()[2] and (self._debug or self._verbosity >= 3):

View File

@@ -432,52 +432,6 @@ def extract(item, container, morekeys=None):
return value
def failed(*a, **kw):
''' Test if task result yields failed '''
item = a[0]
if not isinstance(item, MutableMapping):
raise errors.AnsibleFilterError("|failed expects a dictionary")
rc = item.get('rc', 0)
failed = item.get('failed', False)
if rc != 0 or failed:
return True
else:
return False
def success(*a, **kw):
''' Test if task result yields success '''
return not failed(*a, **kw)
def changed(*a, **kw):
''' Test if task result yields changed '''
item = a[0]
if not isinstance(item, MutableMapping):
raise errors.AnsibleFilterError("|changed expects a dictionary")
if 'changed' not in item:
changed = False
if (
'results' in item and # some modules return a 'results' key
isinstance(item['results'], MutableSequence) and
isinstance(item['results'][0], MutableMapping)
):
for result in item['results']:
changed = changed or result.get('changed', False)
else:
changed = item.get('changed', False)
return changed
def skipped(*a, **kw):
''' Test if task result yields skipped '''
item = a[0]
if not isinstance(item, MutableMapping):
raise errors.AnsibleFilterError("|skipped expects a dictionary")
skipped = item.get('skipped', False)
return skipped
@environmentfilter
def do_groupby(environment, value, attribute):
"""Overridden groupby filter for jinja2, to address an issue with
@@ -593,20 +547,6 @@ class FilterModule(object):
# array and dict lookups
'extract': extract,
# failure testing
'failed': failed,
'failure': failed,
'success': success,
'succeeded': success,
# changed testing
'changed': changed,
'change': changed,
# skip testing
'skipped': skipped,
'skip': skipped,
# debug
'type_debug': lambda o: o.__class__.__name__,
}

View File

@@ -27,50 +27,43 @@ from distutils.version import LooseVersion, StrictVersion
from ansible import errors
def failed(*a, **kw):
def failed(result):
''' Test if task result yields failed '''
item = a[0]
if not isinstance(item, MutableMapping):
if not isinstance(result, MutableMapping):
raise errors.AnsibleFilterError("|failed expects a dictionary")
rc = item.get('rc', 0)
failed = item.get('failed', False)
if rc != 0 or failed:
return True
else:
return False
return result.get('failed', False)
def success(*a, **kw):
def success(result):
''' Test if task result yields success '''
return not failed(*a, **kw)
return not failed(result)
def changed(*a, **kw):
def changed(result):
''' Test if task result yields changed '''
item = a[0]
if not isinstance(item, MutableMapping):
if not isinstance(result, MutableMapping):
raise errors.AnsibleFilterError("|changed expects a dictionary")
if 'changed' not in item:
if 'changed' not in result:
changed = False
if (
'results' in item and # some modules return a 'results' key
isinstance(item['results'], MutableSequence) and
isinstance(item['results'][0], MutableMapping)
'results' in result and # some modules return a 'results' key
isinstance(result['results'], MutableSequence) and
isinstance(result['results'][0], MutableMapping)
):
for result in item['results']:
changed = changed or result.get('changed', False)
for res in result['results']:
if res.get('changed', False):
changed = True
break
else:
changed = item.get('changed', False)
changed = result.get('changed', False)
return changed
def skipped(*a, **kw):
def skipped(result):
''' Test if task result yields skipped '''
item = a[0]
if not isinstance(item, MutableMapping):
if not isinstance(result, MutableMapping):
raise errors.AnsibleFilterError("|skipped expects a dictionary")
skipped = item.get('skipped', False)
return skipped
return result.get('skipped', False)
def regex(value='', pattern='', ignorecase=False, multiline=False, match_type='search'):
@@ -133,13 +126,17 @@ class TestModule(object):
return {
# failure testing
'failed': failed,
'failure': failed,
'succeeded': success,
'success': success,
# changed testing
'changed': changed,
'change': changed,
# skip testing
'skipped': skipped,
'skip': skipped,
# regex
'match': match,