mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-04-24 11:21:25 -07:00
parent
2f33c1a1a1
commit
5553b20828
206 changed files with 1853 additions and 1870 deletions
|
@ -26,38 +26,44 @@ from distutils.version import LooseVersion, StrictVersion
|
|||
|
||||
from ansible import errors
|
||||
|
||||
|
||||
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)
|
||||
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 not 'changed' in item:
|
||||
if 'changed' not in item:
|
||||
changed = False
|
||||
if ('results' in item # some modules return a 'results' key
|
||||
and isinstance(item['results'], MutableSequence)
|
||||
and isinstance(item['results'][0], MutableMapping)):
|
||||
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]
|
||||
|
@ -66,6 +72,7 @@ def skipped(*a, **kw):
|
|||
skipped = item.get('skipped', False)
|
||||
return skipped
|
||||
|
||||
|
||||
def regex(value='', pattern='', ignorecase=False, multiline=False, match_type='search'):
|
||||
''' Expose `re` as a boolean filter using the `search` method by default.
|
||||
This is likely only useful for `search` and `match` which already
|
||||
|
@ -80,21 +87,24 @@ def regex(value='', pattern='', ignorecase=False, multiline=False, match_type='s
|
|||
_bool = __builtins__.get('bool')
|
||||
return _bool(getattr(_re, match_type, 'search')(value))
|
||||
|
||||
|
||||
def match(value, pattern='', ignorecase=False, multiline=False):
|
||||
''' Perform a `re.match` returning a boolean '''
|
||||
return regex(value, pattern, ignorecase, multiline, 'match')
|
||||
|
||||
|
||||
def search(value, pattern='', ignorecase=False, multiline=False):
|
||||
''' Perform a `re.search` returning a boolean '''
|
||||
return regex(value, pattern, ignorecase, multiline, 'search')
|
||||
|
||||
|
||||
def version_compare(value, version, operator='eq', strict=False):
|
||||
''' Perform a version comparison on a value '''
|
||||
op_map = {
|
||||
'==': 'eq', '=': 'eq', 'eq': 'eq',
|
||||
'<': 'lt', 'lt': 'lt',
|
||||
'==': 'eq', '=': 'eq', 'eq': 'eq',
|
||||
'<': 'lt', 'lt': 'lt',
|
||||
'<=': 'le', 'le': 'le',
|
||||
'>': 'gt', 'gt': 'gt',
|
||||
'>': 'gt', 'gt': 'gt',
|
||||
'>=': 'ge', 'ge': 'ge',
|
||||
'!=': 'ne', '<>': 'ne', 'ne': 'ne'
|
||||
}
|
||||
|
@ -115,20 +125,21 @@ def version_compare(value, version, operator='eq', strict=False):
|
|||
except Exception as e:
|
||||
raise errors.AnsibleFilterError('Version comparison: %s' % e)
|
||||
|
||||
|
||||
class TestModule(object):
|
||||
''' Ansible core jinja2 tests '''
|
||||
|
||||
def tests(self):
|
||||
return {
|
||||
# failure testing
|
||||
'failed' : failed,
|
||||
'succeeded' : success,
|
||||
'failed': failed,
|
||||
'succeeded': success,
|
||||
|
||||
# changed testing
|
||||
'changed' : changed,
|
||||
'changed': changed,
|
||||
|
||||
# skip testing
|
||||
'skipped' : skipped,
|
||||
'skipped': skipped,
|
||||
|
||||
# regex
|
||||
'match': match,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue