added regex support to find, also added 'singular' aliasess to patterns and paths

This commit is contained in:
Brian Coca 2015-10-13 10:04:50 -04:00
parent 14f32de7a6
commit bc4b40d8e7

View file

@ -50,17 +50,18 @@ options:
required: false required: false
default: '*' default: '*'
description: 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 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: contains:
required: false required: false
default: null default: null
description: 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: paths:
required: true required: true
aliases: [ "name" ] aliases: [ "name", "path" ]
description: description:
- List of paths to the file or directory to search. All paths must be fully qualified. - List of paths to the file or directory to search. All paths must be fully qualified.
file_type: file_type:
@ -121,8 +122,9 @@ EXAMPLES = '''
# Recursively find /var/tmp files with last access time greater than 3600 seconds # 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: 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 /var/log files equal or greater than 10 megabytes ending with .old or .log.gz via regex
- find: paths="/var/tmp" patterns="*.log","*.log.gz" size="10m" - find: paths="/var/tmp" patterns="~.*\.(?:old|log\.gz)$" size="10m"
''' '''
RETURN = ''' RETURN = '''
@ -157,9 +159,11 @@ def pfilter(f, patterns=None):
if patterns is None: if patterns is None:
return True return True
for p in patterns: for p in patterns:
if fnmatch.fnmatch(f, p): if p.startswith('~'):
return True r = re.compile(p[1:])
return False return r.match(f)
else:
return fnmatch.fnmatch(f, p)
def agefilter(st, now, age, timestamp): def agefilter(st, now, age, timestamp):
@ -236,8 +240,8 @@ def statinfo(st):
def main(): def main():
module = AnsibleModule( module = AnsibleModule(
argument_spec = dict( argument_spec = dict(
paths = dict(required=True, aliases=['name'], type='list'), paths = dict(required=True, aliases=['name','path'], type='list'),
patterns = dict(default=['*'], type='list'), patterns = dict(default=['*'], type='list', aliases['pattern']),
contains = dict(default=None, type='str'), contains = dict(default=None, type='str'),
file_type = dict(default="file", choices=['file', 'directory'], type='str'), file_type = dict(default="file", choices=['file', 'directory'], type='str'),
age = dict(default=None, type='str'), age = dict(default=None, type='str'),