diff options
author | Brian Coca <brian.coca+git@gmail.com> | 2015-10-13 10:04:50 -0400 |
---|---|---|
committer | Brian Coca <brian.coca+git@gmail.com> | 2015-10-19 20:44:44 -0400 |
commit | bc4b40d8e788ca362b597c5e92670ca7ad9005c4 (patch) | |
tree | 51f7f63cea0790c58cf27003277df6e24baa55d4 | |
parent | 14f32de7a6eb4bb67c372a6df4c4bcb7284ce432 (diff) | |
download | ansible-modules-core-bc4b40d8e788ca362b597c5e92670ca7ad9005c4.tar.gz |
added regex support to find, also added 'singular' aliasess to patterns and paths
-rw-r--r-- | files/find.py | 26 |
1 files changed, 15 insertions, 11 deletions
diff --git a/files/find.py b/files/find.py index 659ec160..d7042f00 100644 --- a/files/find.py +++ b/files/find.py @@ -50,17 +50,18 @@ options: required: false default: '*' description: - - One or more (shell type) file glob patterns, which restrict the list of files to be returned to + - One or more (shell type) 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. + specified using a list. The patterns can be simple shell globs or a python regex prefixed by a '~'. + aliases: ['pattern'] contains: required: false default: null description: - - One or more re patterns which should be matched against the file content + - One or more re patterns which should be matched against the file content paths: required: true - aliases: [ "name" ] + aliases: [ "name", "path" ] description: - List of paths to the file or directory to search. All paths must be fully qualified. file_type: @@ -121,8 +122,9 @@ EXAMPLES = ''' # Recursively find /var/tmp files with last access time greater than 3600 seconds - find: paths="/var/tmp" age="3600" age_stamp=atime recurse=yes -# find /var/log files equal or greater than 10 megabytes ending with .log or .log.gz -- find: paths="/var/tmp" patterns="*.log","*.log.gz" size="10m" +# 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" + ''' RETURN = ''' @@ -157,9 +159,11 @@ def pfilter(f, patterns=None): if patterns is None: return True for p in patterns: - if fnmatch.fnmatch(f, p): - return True - return False + if p.startswith('~'): + r = re.compile(p[1:]) + return r.match(f) + else: + return fnmatch.fnmatch(f, p) def agefilter(st, now, age, timestamp): @@ -236,8 +240,8 @@ def statinfo(st): def main(): module = AnsibleModule( argument_spec = dict( - paths = dict(required=True, aliases=['name'], type='list'), - patterns = dict(default=['*'], type='list'), + paths = dict(required=True, aliases=['name','path'], type='list'), + patterns = dict(default=['*'], type='list', aliases['pattern']), contains = dict(default=None, type='str'), file_type = dict(default="file", choices=['file', 'directory'], type='str'), age = dict(default=None, type='str'), |