From 62d69fc7245167d919fdc4c406d2a6dd9ec832ae Mon Sep 17 00:00:00 2001 From: David Shrewsbury Date: Mon, 17 May 2021 01:23:06 -0400 Subject: [PATCH] Fix fileglob parameter order bug (#72879) (#72904) (cherry picked from commit fe17cb6eba0df9e6c19e6dd8a78701fca6fa70e4) --- .../fragments/72873-fix-fileglob-ordering.yml | 2 ++ lib/ansible/plugins/lookup/fileglob.py | 5 +-- .../lookup_fileglob/issue72873/test.yml | 31 +++++++++++++++++++ .../targets/lookup_fileglob/runme.sh | 3 ++ 4 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/72873-fix-fileglob-ordering.yml create mode 100644 test/integration/targets/lookup_fileglob/issue72873/test.yml diff --git a/changelogs/fragments/72873-fix-fileglob-ordering.yml b/changelogs/fragments/72873-fix-fileglob-ordering.yml new file mode 100644 index 00000000000..329befc9079 --- /dev/null +++ b/changelogs/fragments/72873-fix-fileglob-ordering.yml @@ -0,0 +1,2 @@ +bugfixes: +- Fix fileglob bug where it could return different results for different order of parameters (https://github.com/ansible/ansible/issues/72873). diff --git a/lib/ansible/plugins/lookup/fileglob.py b/lib/ansible/plugins/lookup/fileglob.py index aa5d7d34d8a..5264bbc923f 100644 --- a/lib/ansible/plugins/lookup/fileglob.py +++ b/lib/ansible/plugins/lookup/fileglob.py @@ -76,7 +76,8 @@ class LookupModule(LookupBase): for dwimmed_path in found_paths: if dwimmed_path: globbed = glob.glob(to_bytes(os.path.join(dwimmed_path, term_file), errors='surrogate_or_strict')) - ret.extend(to_text(g, errors='surrogate_or_strict') for g in globbed if os.path.isfile(g)) - if ret: + term_results = [to_text(g, errors='surrogate_or_strict') for g in globbed if os.path.isfile(g)] + if term_results: + ret.extend(term_results) break return ret diff --git a/test/integration/targets/lookup_fileglob/issue72873/test.yml b/test/integration/targets/lookup_fileglob/issue72873/test.yml new file mode 100644 index 00000000000..218ee58d903 --- /dev/null +++ b/test/integration/targets/lookup_fileglob/issue72873/test.yml @@ -0,0 +1,31 @@ +- hosts: localhost + connection: local + gather_facts: false + vars: + dir: files + tasks: + - file: path='{{ dir }}' state=directory + + - file: path='setvars.bat' state=touch # in current directory! + + - file: path='{{ dir }}/{{ item }}' state=touch + loop: + - json.c + - strlcpy.c + - base64.c + - json.h + - base64.h + - strlcpy.h + - jo.c + + - name: Get working order results and sort them + set_fact: + working: '{{ query("fileglob", "setvars.bat", "{{ dir }}/*.[ch]") | sort }}' + + - name: Get broken order results and sort them + set_fact: + broken: '{{ query("fileglob", "{{ dir }}/*.[ch]", "setvars.bat") | sort }}' + + - assert: + that: + - working == broken diff --git a/test/integration/targets/lookup_fileglob/runme.sh b/test/integration/targets/lookup_fileglob/runme.sh index 1e0297c79d0..be04421144f 100755 --- a/test/integration/targets/lookup_fileglob/runme.sh +++ b/test/integration/targets/lookup_fileglob/runme.sh @@ -13,3 +13,6 @@ for seed in foo foo/bar foo/bar/baz do ansible-playbook non_existent/play.yml -e "seed='${seed}'" "$@" done + +# test for issue 72873 fix +ansible-playbook issue72873/test.yml "$@"