Source code for ansible.plugins.test.core

# (c) 2012, Jeroen Hoekx <jeroen@hoekx.be>
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.

# Make coding more python3-ish
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

import re
from ansible import errors

[docs]def failed(*a, **kw): ''' Test if task result yields failed ''' item = a[0] if type(item) != dict: 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
[docs]def success(*a, **kw): ''' Test if task result yields success ''' return not failed(*a, **kw)
[docs]def changed(*a, **kw): ''' Test if task result yields changed ''' item = a[0] if type(item) != dict: raise errors.AnsibleFilterError("|changed expects a dictionary") if not 'changed' in item: changed = False if ('results' in item # some modules return a 'results' key and type(item['results']) == list and type(item['results'][0]) == dict): for result in item['results']: changed = changed or result.get('changed', False) else: changed = item.get('changed', False) return changed
[docs]def skipped(*a, **kw): ''' Test if task result yields skipped ''' item = a[0] if type(item) != dict: raise errors.AnsibleFilterError("|skipped expects a dictionary") skipped = item.get('skipped', False) return skipped
[docs]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 have their own filters. ''' flags = 0 if ignorecase: flags |= re.I if multiline: flags |= re.M _re = re.compile(pattern, flags=flags) _bool = __builtins__.get('bool') return _bool(getattr(_re, match_type, 'search')(value))
[docs]def match(value, pattern='', ignorecase=False, multiline=False): ''' Perform a `re.match` returning a boolean ''' return regex(value, pattern, ignorecase, multiline, 'match')
[docs]class TestModule(object): ''' Ansible core jinja2 tests '''
[docs] def tests(self): return { # failure testing 'failed' : failed, 'failure' : failed, 'success' : success, 'succeeded' : success, # changed testing 'changed' : changed, 'change' : changed, # skip testing 'skipped' : skipped, 'skip' : skipped, # regex 'match': match, 'search': search, 'regex': regex, }