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>
pull/80739/head
Brian Coca 1 year ago committed by GitHub
parent 70e0b1f544
commit 5a84ff26df
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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.

@ -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

@ -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'"

Loading…
Cancel
Save