From 3e5dc1fd74811e02bbfe5fbaa9a71a2165f034a6 Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Mon, 19 Oct 2015 17:42:36 -0400 Subject: [PATCH] changed so regexes and shell globs work transparently --- lib/ansible/modules/files/find.py | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/lib/ansible/modules/files/find.py b/lib/ansible/modules/files/find.py index 10d5de834f..9ec6f6eb8b 100644 --- a/lib/ansible/modules/files/find.py +++ b/lib/ansible/modules/files/find.py @@ -50,9 +50,9 @@ options: required: false default: '*' description: - - One or more (shell type) patterns, which restrict the list of files to be returned to + - One or more (shell or regex) patterns, which restrict the list of files to be returned to those whose basenames match at least one of the patterns specified. Multiple patterns can be - specified using a list. The patterns can be simple shell globs or a python regex prefixed by a '~'. + specified using a list. aliases: ['pattern'] contains: required: false @@ -123,7 +123,7 @@ EXAMPLES = ''' - find: paths="/var/tmp" age="3600" age_stamp=atime recurse=yes # find /var/log files equal or greater than 10 megabytes ending with .old or .log.gz via regex -- find: paths="/var/tmp" patterns="~.*\.(?:old|log\.gz)$" size="10m" +- find: paths="/var/tmp" patterns="^.*?\.(?:old|log\.gz)$" size="10m" ''' @@ -156,14 +156,25 @@ examined: def pfilter(f, patterns=None): '''filter using glob patterns''' + if patterns is None: return True + + match = False for p in patterns: - if p.startswith('~'): - r = re.compile(p[1:]) - return r.match(f) - else: - return fnmatch.fnmatch(f, p) + try: + r = re.compile(p) + match = r.match(f) + except: + pass + + if not match: + match = fnmatch.fnmatch(f, p) + + if match: + break + + return match def agefilter(st, now, age, timestamp):