summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorSam Doran <sdoran@redhat.com>2021-04-05 01:09:42 -0400
committerGitHub <noreply@github.com>2021-04-05 00:09:42 -0500
commit0d668b6046ca3abe53c2a4aedabe983f42b3edf9 (patch)
tree2d6a6331e699fe7058ab935788eee88a7d3c9fae /lib
parentf8caa43dc7a7beaf66e719e1e5f7bc6ff70fa7e6 (diff)
downloadansible-0d668b6046ca3abe53c2a4aedabe983f42b3edf9.tar.gz
[stable-2.9] find - set proper default based on use_regex (#73961) (#73966)
When using "use_regex: yes" and setting an excludes: without specifying a pattern: the existing code passes the file-glob '*' to the regex matcher. This results in an internal invalid-regex exception being thrown. This maintains the old semantics of a default match-all for pattern: but switches the default to '.*' when use_regex is specified. The code made sense as-is before excludes: was added (2.5). In that case, it made no sense to set use_regex but *not* set a pattern. However, with excludes: it now makes sense to only want to exclude a given regex but not specify a specific matching pattern. Closes: #50067 * moved change to new location added changelog * Update lib/ansible/modules/find.py Co-authored-by: Ian Wienand <iwienand@redhat.com>. (cherry picked from commit 089d0a0508a470799d099d95fc371e66756a00b3) Co-authored-by: Brian Coca <bcoca@users.noreply.github.com> * Update fix_find_default.yml Co-authored-by: Rick Elrod <rick@elrod.me>
Diffstat (limited to 'lib')
-rw-r--r--lib/ansible/modules/files/find.py19
1 files changed, 15 insertions, 4 deletions
diff --git a/lib/ansible/modules/files/find.py b/lib/ansible/modules/files/find.py
index b516c4fbbe..718f5eabcc 100644
--- a/lib/ansible/modules/files/find.py
+++ b/lib/ansible/modules/files/find.py
@@ -32,7 +32,7 @@ options:
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
@@ -44,6 +44,7 @@ options:
- 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:
@@ -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'),
@@ -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: