Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mindepth, maxdepth parameters to find module #36389

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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'