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 2 years 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: for term in terms:
if isinstance(term, Mapping): if isinstance(term, Mapping):
self.set_options(var_options=variables, direct=term) self.set_options(var_options=variables, direct=term)
files = self.get_option('files')
elif isinstance(term, string_types): elif isinstance(term, string_types):
self.set_options(var_options=variables, direct=kwargs) files = [term]
elif isinstance(term, Sequence): elif isinstance(term, Sequence):
partial, skip = self._process_terms(term, variables, kwargs) partial, skip = self._process_terms(term, variables, kwargs)
total_search.extend(partial) total_search.extend(partial)
@ -178,7 +179,6 @@ class LookupModule(LookupBase):
else: else:
raise AnsibleLookupError("Invalid term supplied, can handle string, mapping or list of strings but got: %s for %s" % (type(term), term)) 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') paths = self.get_option('paths')
# NOTE: this is used as 'global' but can be set many times?!?!? # 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) f = os.path.join(path, fn)
total_search.append(f) total_search.append(f)
elif filelist: elif filelist:
# NOTE: this seems wrong, should be 'extend' as any option/entry can clobber all # NOTE: this is now 'extend', previouslly it would clobber all options, but we deemed that a bug
total_search = filelist total_search.extend(filelist)
else: else:
total_search.append(term) total_search.append(term)
@ -204,6 +204,10 @@ class LookupModule(LookupBase):
def run(self, terms, variables, **kwargs): 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) total_search, skip = self._process_terms(terms, variables, kwargs)
# NOTE: during refactor noticed that the 'using a dict' as term # NOTE: during refactor noticed that the 'using a dict' as term

@ -94,3 +94,11 @@
- assert: - assert:
that: that:
- foo is defined - 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