diff options
author | Brian Coca <bcoca@users.noreply.github.com> | 2023-05-08 14:25:25 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-08 14:25:25 -0400 |
commit | 5a84ff26df8e32cde312c95963071eb375a7cd27 (patch) | |
tree | b0993c3e913a0185d3fdca3116ee930a3187c46f | |
parent | 70e0b1f5448143f54f096b9a0a991e1d70fd7690 (diff) | |
download | ansible-5a84ff26df8e32cde312c95963071eb375a7cd27.tar.gz |
first_found fix no terms option (#76550)
also fix bug with multiple items clobbering previous settings
Co-authored-by: Sloane Hertel <19572925+s-hertel@users.noreply.github.com>
-rw-r--r-- | changelogs/fragments/first_found_fixes.yml | 3 | ||||
-rw-r--r-- | lib/ansible/plugins/lookup/first_found.py | 12 | ||||
-rw-r--r-- | test/integration/targets/lookup_first_found/tasks/main.yml | 8 |
3 files changed, 19 insertions, 4 deletions
diff --git a/changelogs/fragments/first_found_fixes.yml b/changelogs/fragments/first_found_fixes.yml new file mode 100644 index 0000000000..edf2ef3674 --- /dev/null +++ b/changelogs/fragments/first_found_fixes.yml @@ -0,0 +1,3 @@ +bugfixes: + - first found lookup has been updated to use the normalized argument parsing (pythonic) matching the documented examples. + - first found lookup, fixed an issue with subsequent items clobbering information from previous ones. diff --git a/lib/ansible/plugins/lookup/first_found.py b/lib/ansible/plugins/lookup/first_found.py index 5b94b103a4..0b00b5f537 100644 --- a/lib/ansible/plugins/lookup/first_found.py +++ b/lib/ansible/plugins/lookup/first_found.py @@ -169,8 +169,9 @@ class LookupModule(LookupBase): for term in terms: if isinstance(term, Mapping): self.set_options(var_options=variables, direct=term) + files = self.get_option('files') elif isinstance(term, string_types): - self.set_options(var_options=variables, direct=kwargs) + files = [term] elif isinstance(term, Sequence): partial, skip = self._process_terms(term, variables, kwargs) total_search.extend(partial) @@ -178,7 +179,6 @@ class LookupModule(LookupBase): else: raise AnsibleLookupError("Invalid term supplied, can handle string, mapping or list of strings but got: %s for %s" % (type(term), term)) - files = self.get_option('files') paths = self.get_option('paths') # NOTE: this is used as 'global' but can be set many times?!?!? @@ -195,8 +195,8 @@ class LookupModule(LookupBase): f = os.path.join(path, fn) total_search.append(f) elif filelist: - # NOTE: this seems wrong, should be 'extend' as any option/entry can clobber all - total_search = filelist + # NOTE: this is now 'extend', previouslly it would clobber all options, but we deemed that a bug + total_search.extend(filelist) else: total_search.append(term) @@ -204,6 +204,10 @@ class LookupModule(LookupBase): def run(self, terms, variables, **kwargs): + if not terms: + self.set_options(var_options=variables, direct=kwargs) + terms = self.get_option('files') + total_search, skip = self._process_terms(terms, variables, kwargs) # NOTE: during refactor noticed that the 'using a dict' as term diff --git a/test/integration/targets/lookup_first_found/tasks/main.yml b/test/integration/targets/lookup_first_found/tasks/main.yml index 9aeaf1d1ed..d0ca91eaf5 100644 --- a/test/integration/targets/lookup_first_found/tasks/main.yml +++ b/test/integration/targets/lookup_first_found/tasks/main.yml @@ -94,3 +94,11 @@ - assert: that: - foo is defined + +# TODO: no 'terms' test +- name: test first_found lookup with no terms + set_fact: + no_terms: "{{ query('first_found', files=['missing1', 'hosts', 'missing2'], paths=['/etc'], errors='ignore') }}" + +- assert: + that: "no_terms|first == '/etc/hosts'" |