mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-08 06:12:51 +00:00
committed by
John R Barker
parent
2f33c1a1a1
commit
5553b20828
@@ -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,
|
||||
|
||||
@@ -22,20 +22,21 @@ __metaclass__ = type
|
||||
from os.path import isdir, isfile, isabs, exists, lexists, islink, samefile, ismount
|
||||
from ansible import errors
|
||||
|
||||
|
||||
class TestModule(object):
|
||||
''' Ansible file jinja2 tests '''
|
||||
|
||||
def tests(self):
|
||||
return {
|
||||
# file testing
|
||||
'is_dir' : isdir,
|
||||
'is_file' : isfile,
|
||||
'is_link' : islink,
|
||||
'exists' : exists,
|
||||
'link_exists' : lexists,
|
||||
'is_dir': isdir,
|
||||
'is_file': isfile,
|
||||
'is_link': islink,
|
||||
'exists': exists,
|
||||
'link_exists': lexists,
|
||||
|
||||
# path testing
|
||||
'is_abs' : isabs,
|
||||
'is_same_file' : samefile,
|
||||
'is_mount' : ismount,
|
||||
'is_abs': isabs,
|
||||
'is_same_file': samefile,
|
||||
'is_mount': ismount,
|
||||
}
|
||||
|
||||
@@ -20,18 +20,22 @@ __metaclass__ = type
|
||||
|
||||
import math
|
||||
|
||||
|
||||
def issubset(a, b):
|
||||
return set(a) <= set(b)
|
||||
|
||||
|
||||
def issuperset(a, b):
|
||||
return set(a) >= set(b)
|
||||
|
||||
|
||||
def isnotanumber(x):
|
||||
try:
|
||||
return math.isnan(x)
|
||||
except TypeError:
|
||||
return False
|
||||
|
||||
|
||||
class TestModule:
|
||||
''' Ansible math jinja2 tests '''
|
||||
|
||||
|
||||
Reference in New Issue
Block a user