diff --git a/changelogs/fragments/first_found_fixes.yml b/changelogs/fragments/first_found_fixes.yml new file mode 100644 index 00000000000..edf2ef3674c --- /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 5b94b103a4e..0b00b5f5379 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 9aeaf1d1ed5..d0ca91eaf54 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'"