Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
ansible
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
OpenEdx
ansible
Commits
603db608
Commit
603db608
authored
Oct 27, 2013
by
Michael DeHaan
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #4288 from sivel/3725-regex
Add regex (search, match, regex) jinja2 filters. Fixes #3725
parents
3778b391
61525a97
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
54 additions
and
0 deletions
+54
-0
lib/ansible/runner/filter_plugins/core.py
+27
-0
test/TestFilters.py
+27
-0
No files found.
lib/ansible/runner/filter_plugins/core.py
View file @
603db608
...
...
@@ -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
,
}
test/TestFilters.py
View file @
603db608
...
...
@@ -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.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment