From 61525a97df43e5541adab205af260fd46d54669d Mon Sep 17 00:00:00 2001 From: Matt Martz Date: Fri, 11 Oct 2013 08:03:04 -0500 Subject: [PATCH] Add regex (search, match, regex) jinja2 filters. Fixes #3725 --- lib/ansible/runner/filter_plugins/core.py | 27 +++++++++++++++++++++++ test/TestFilters.py | 27 +++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/lib/ansible/runner/filter_plugins/core.py b/lib/ansible/runner/filter_plugins/core.py index 55bde22abc..dac3d233a7 100644 --- a/lib/ansible/runner/filter_plugins/core.py +++ b/lib/ansible/runner/filter_plugins/core.py @@ -22,6 +22,7 @@ import yaml import types import pipes import glob +import re from ansible import errors from ansible.utils import md5s @@ -98,6 +99,27 @@ def fileglob(pathname): ''' return list of matched files for glob ''' return glob.glob(pathname) +def regex(value='', pattern='', ignorecase=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. + ''' + if ignorecase: + flags = re.I + else: + flags = 0 + _re = re.compile(pattern, flags=flags) + _bool = __builtins__.get('bool') + return _bool(getattr(_re, match_type, 'search')(value)) + +def match(value, pattern='', ignorecase=False): + ''' Perform a `re.match` returning a boolean ''' + return regex(value, pattern, ignorecase, 'match') + +def search(value, pattern='', ignorecase=False): + ''' Perform a `re.search` returning a boolean ''' + return regex(value, pattern, ignorecase, 'search') + class FilterModule(object): ''' Ansible core jinja2 filters ''' @@ -145,5 +167,10 @@ class FilterModule(object): # file glob 'fileglob': fileglob, + + # regex + 'match': match, + 'search': search, + 'regex': regex, } diff --git a/test/TestFilters.py b/test/TestFilters.py index ec92ac3f7f..d850db4c3a 100644 --- a/test/TestFilters.py +++ b/test/TestFilters.py @@ -89,6 +89,33 @@ class TestFilters(unittest.TestCase): a = ansible.runner.filter_plugins.core.fileglob(pathname) assert __file__ in a + def test_regex(self): + a = ansible.runner.filter_plugins.core.regex('ansible', 'ansible', + match_type='findall') + assert a == True + + def test_match_case_sensitive(self): + a = ansible.runner.filter_plugins.core.match('ansible', 'ansible') + assert a == True + + def test_match_case_insensitive(self): + a = ansible.runner.filter_plugins.core.match('ANSIBLE', 'ansible', + True) + assert a == True + + def test_match_no_match(self): + a = ansible.runner.filter_plugins.core.match(' ansible', 'ansible') + assert a == False + + def test_search_case_sensitive(self): + a = ansible.runner.filter_plugins.core.search(' ansible ', 'ansible') + assert a == True + + def test_search_case_insensitive(self): + a = ansible.runner.filter_plugins.core.search(' ANSIBLE ', 'ansible', + True) + assert a == True + #def test_filters(self): # this test is pretty low level using a playbook, hence I am disabling it for now -- MPD.