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

[stable-2.9] find - set proper default based on use_regex (#73961) #73966

Merged
merged 2 commits into from Apr 5, 2021
Merged
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
2 changes: 2 additions & 0 deletions changelogs/fragments/fix_find_default.yml
@@ -0,0 +1,2 @@
bugfixes:
- find - fix default pattern when use_regex is true (https://github.com/ansible/ansible/issues/50067).
19 changes: 15 additions & 4 deletions lib/ansible/modules/files/find.py
Expand Up @@ -32,7 +32,7 @@
first letter of any of those words (e.g., "1w").
type: str
patterns:
default: '*'
default: []
description:
- One or more (shell or regex) patterns, which type is controlled by C(use_regex) option.
- The patterns restrict the list of files to be returned to those whose basenames match at
Expand All @@ -44,6 +44,7 @@
- This parameter expects a list, which can be either comma separated or YAML. If any of the
patterns contain a comma, make sure to put them in a list to avoid splitting the patterns
in undesirable ways.
- Defaults to '*' when C(use_regex=False), or '.*' when C(use_regex=True).
type: list
aliases: [ pattern ]
excludes:
Expand Down Expand Up @@ -359,9 +360,9 @@ def statinfo(st):
def main():
module = AnsibleModule(
argument_spec=dict(
paths=dict(type='list', required=True, aliases=['name', 'path']),
patterns=dict(type='list', default=['*'], aliases=['pattern']),
excludes=dict(type='list', aliases=['exclude']),
paths=dict(type='list', required=True, aliases=['name', 'path'], elements='str'),
patterns=dict(type='list', default=[], aliases=['pattern'], elements='str'),
excludes=dict(type='list', aliases=['exclude'], elements='str'),
contains=dict(type='str'),
file_type=dict(type='str', default="file", choices=['any', 'directory', 'file', 'link']),
age=dict(type='str'),
Expand All @@ -379,6 +380,16 @@ def main():

params = module.params

# Set the default match pattern to either a match-all glob or
# regex depending on use_regex being set. This makes sure if you
# set excludes: without a pattern pfilter gets something it can
# handle.
if not params['patterns']:
if params['use_regex']:
params['patterns'] = ['.*']
else:
params['patterns'] = ['*']

filelist = []

if params['age'] is None:
Expand Down
22 changes: 22 additions & 0 deletions test/integration/targets/find/tasks/main.yml
Expand Up @@ -95,3 +95,25 @@
- 'find_test2.matched == 1'
- 'find_test2.files[0].pw_name is defined'
- 'find_test2.files[0].gr_name is defined'

- name: exclude with regex
find:
paths: "{{ output_dir_test }}"
recurse: yes
use_regex: true
exclude: .*\.ogg
register: find_test3
# Note that currently sane ways of doing this with map() or
# selectattr() aren't available in centos6 era jinja2 ...
- set_fact:
find_test3_list: >-
[ {% for f in find_test3.files %}
{{ f.path }}
{% if not loop.last %},{% endif %}
{% endfor %}
]
- debug: var=find_test3_list
- name: assert we skipped the ogg file
assert:
that:
- '"{{ output_dir_test }}/e/f/g/h/8.ogg" not in find_test3_list'