mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-07 22:02:50 +00:00
Merge pull request #4288 from sivel/3725-regex
Add regex (search, match, regex) jinja2 filters. Fixes #3725
This commit is contained in:
@@ -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 '''
|
||||
|
||||
@@ -146,5 +168,10 @@ class FilterModule(object):
|
||||
|
||||
# file glob
|
||||
'fileglob': fileglob,
|
||||
|
||||
# regex
|
||||
'match': match,
|
||||
'search': search,
|
||||
'regex': regex,
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user