Skip to content

Commit

Permalink
Add mindepth, maxdepth parameters to find module
Browse files Browse the repository at this point in the history
Fixes #36369
  • Loading branch information
giovannisciortino committed Feb 19, 2018
1 parent 542d27e commit 56ba8c6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
18 changes: 18 additions & 0 deletions lib/ansible/modules/files/find.py
Expand Up @@ -96,6 +96,17 @@
choices: [ 'no', 'yes' ]
description:
- If false the patterns are file globs (shell) if true they are python regexes.
mindepth:
default: 'no'
description:
- 'Descend at most levels (a non-negative integer) levels of directories below the starting-points.'
version_added: "2.6"
maxdepth:
default: 'no'
description:
- 'Do not apply any tests or actions at levels less than the argument (a non-negative integer)'
version_added: "2.6"
notes:
- For Windows targets, use the M(win_find) module instead.
'''
Expand Down Expand Up @@ -328,6 +339,8 @@ def main():
follow=dict(type='bool', default='no'),
get_checksum=dict(type='bool', default='no'),
use_regex=dict(type='bool', default='no'),
maxdepth=dict(type='int'),
mindepth=dict(type='int'),
),
supports_check_mode=True,
)
Expand Down Expand Up @@ -363,6 +376,7 @@ def main():
looked = 0
for npath in params['paths']:
npath = os.path.expanduser(os.path.expandvars(npath))
npath_depth = npath.count(os.path.sep)
if os.path.isdir(npath):
''' ignore followlinks for python version < 2.6 '''
for root, dirs, files in (sys.version_info < (2, 6, 0) and os.walk(npath)) or os.walk(npath, followlinks=params['follow']):
Expand All @@ -373,6 +387,10 @@ def main():
if os.path.basename(fsname).startswith('.') and not params['hidden']:
continue

depth = fsname.count(os.path.sep) - npath_depth
if (params['maxdepth'] and depth > params['maxdepth']) or (params['mindepth'] and depth < params['mindepth']):
continue

try:
st = os.lstat(fsname)
except:
Expand Down
19 changes: 19 additions & 0 deletions test/integration/targets/find/tasks/main.yml
Expand Up @@ -95,3 +95,22 @@
- 'find_test2.matched == 1'
- 'find_test2.files[0].pw_name is defined'
- 'find_test2.files[0].gr_name is defined'

- name: find with max_depth and min_depth
find:
paths: "{{ output_dir_test }}"
recurse: yes
mindepth: 3
maxdepth: 4
register: find_test3
- debug: var=find_test3
- name: validate find with max_depth and min_depth
assert:
that:
- 'find_test3 is defined'
- 'find_test3.examined is defined'
- 'find_test3.files is defined'
- 'find_test3.matched is defined'
- 'find_test3.msg is defined'
- 'find_test3.matched == 4'
- 'find_test3.files | length == 4'

0 comments on commit 56ba8c6

Please sign in to comment.