summaryrefslogtreecommitdiff
path: root/lib/ansible/plugins/test
diff options
context:
space:
mode:
authorPeter Sprygada <psprygada@ansible.com>2016-03-06 08:08:22 -0500
committerPeter Sprygada <psprygada@ansible.com>2016-03-06 08:11:46 -0500
commitc0f1e1801bb29c708671de21344eafd40004b0c9 (patch)
tree1662761c9bb7bee468ad8e0649857197ce6053e8 /lib/ansible/plugins/test
parent2ee0c1b175b3a02196d931f318c77a8fa5f14eca (diff)
downloadansible-c0f1e1801bb29c708671de21344eafd40004b0c9.tar.gz
adds multiline flag to regex test for search and match
This commit adds the multiline flag to the regexp search and match test plugin. It defaults to re.M = False for backwards compatibility. To use the multiline feature add multiline=True to the test filter {{ config | search('^hostname', multiline=True) }}
Diffstat (limited to 'lib/ansible/plugins/test')
-rw-r--r--lib/ansible/plugins/test/core.py17
1 files changed, 9 insertions, 8 deletions
diff --git a/lib/ansible/plugins/test/core.py b/lib/ansible/plugins/test/core.py
index fb9e0fb86e..641172b91c 100644
--- a/lib/ansible/plugins/test/core.py
+++ b/lib/ansible/plugins/test/core.py
@@ -62,26 +62,27 @@ def skipped(*a, **kw):
skipped = item.get('skipped', False)
return skipped
-def regex(value='', pattern='', ignorecase=False, match_type='search'):
+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
- else:
- flags = 0
+ 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))
-def match(value, pattern='', ignorecase=False):
+def match(value, pattern='', ignorecase=False, multiline=False):
''' Perform a `re.match` returning a boolean '''
- return regex(value, pattern, ignorecase, 'match')
+ return regex(value, pattern, ignorecase, multiline, 'match')
-def search(value, pattern='', ignorecase=False):
+def search(value, pattern='', ignorecase=False, multiline=False):
''' Perform a `re.search` returning a boolean '''
- return regex(value, pattern, ignorecase, 'search')
+ return regex(value, pattern, ignorecase, multiline, 'search')
class TestModule(object):
''' Ansible core jinja2 tests '''