From 87b05138fb6aaab82e55dfa368b32cafa5febdb2 Mon Sep 17 00:00:00 2001 From: Ian Wienand Date: Tue, 18 Dec 2018 17:56:29 +1100 Subject: [PATCH 1/3] find: set pattern to default regex when use_regex specified 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 --- test/integration/targets/find/tasks/main.yml | 21 ++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/test/integration/targets/find/tasks/main.yml b/test/integration/targets/find/tasks/main.yml index 2678c954127621..91d924719e4d3f 100644 --- a/test/integration/targets/find/tasks/main.yml +++ b/test/integration/targets/find/tasks/main.yml @@ -251,3 +251,24 @@ # dir contents are considered until the depth exceeds the requested depth # there are 8 files/directories in the requested depth and 4 that exceed it by 1 - files_with_depth.examined == 12 +- 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' From 8a46adcd1a63866ebab66dc13f6674f291b63663 Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Fri, 19 Mar 2021 11:19:04 -0400 Subject: [PATCH 2/3] moved change to new location added changelog --- changelogs/fragments/fix_find_default.yml | 2 ++ lib/ansible/modules/find.py | 15 +++++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/fix_find_default.yml diff --git a/changelogs/fragments/fix_find_default.yml b/changelogs/fragments/fix_find_default.yml new file mode 100644 index 00000000000000..85c640ae958ce6 --- /dev/null +++ b/changelogs/fragments/fix_find_default.yml @@ -0,0 +1,2 @@ +bugfixes: + - find module, fix default pattern when use_regex is true. diff --git a/lib/ansible/modules/find.py b/lib/ansible/modules/find.py index 323d0223f26917..4ad1f057a01b94 100644 --- a/lib/ansible/modules/find.py +++ b/lib/ansible/modules/find.py @@ -29,7 +29,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 @@ -41,6 +41,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 '*', or '.*' when use_regex enabled type: list aliases: [ pattern ] elements: str @@ -375,7 +376,7 @@ def main(): module = AnsibleModule( argument_spec=dict( paths=dict(type='list', required=True, aliases=['name', 'path'], elements='str'), - patterns=dict(type='list', default=['*'], aliases=['pattern'], elements='str'), + patterns=dict(type='list', default=[], aliases=['pattern'], elements='str'), excludes=dict(type='list', aliases=['exclude'], elements='str'), contains=dict(type='str'), read_whole_file=dict(type='bool', default=False), @@ -395,6 +396,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: From 492deab3e909927841fbdcd8b5df630d649e4ef4 Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Fri, 19 Mar 2021 12:40:14 -0400 Subject: [PATCH 3/3] Update lib/ansible/modules/find.py Co-authored-by: Sam Doran --- lib/ansible/modules/find.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ansible/modules/find.py b/lib/ansible/modules/find.py index 4ad1f057a01b94..7db5004ca34379 100644 --- a/lib/ansible/modules/find.py +++ b/lib/ansible/modules/find.py @@ -41,7 +41,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 '*', or '.*' when use_regex enabled + - Defaults to '*' when C(use_regex=False), or '.*' when C(use_regex=True). type: list aliases: [ pattern ] elements: str