Allow var lookup plugin to operate on a list of terms

Not sure this has a good use case but it's how nearly all other lookups
work so we should support the common interface.
pull/35910/head
Toshio Kuratomi 7 years ago
parent 59faad3a6b
commit db29190c28

@ -63,8 +63,6 @@ from ansible.plugins.lookup import LookupBase
class LookupModule(LookupBase): class LookupModule(LookupBase):
def run(self, terms, variables=None, **kwargs): def run(self, terms, variables=None, **kwargs):
ret = []
if variables is not None: if variables is not None:
self._templar.set_available_variables(variables) self._templar.set_available_variables(variables)
myvars = getattr(self._templar, '_available_variables', {}) myvars = getattr(self._templar, '_available_variables', {})
@ -72,30 +70,25 @@ class LookupModule(LookupBase):
self.set_options(direct=kwargs) self.set_options(direct=kwargs)
default = self.get_option('default') default = self.get_option('default')
# Assumes listify_plugin_terms is called previous to run so each term is already templated and terms is always a list ret = []
if isinstance(terms, list): for term in terms:
term = terms[0] if not isinstance(term, string_types):
elif isinstance(terms, string_types): raise AnsibleError('Invalid setting identifier, "%s" is not a string, its a %s' % (term, type(term)))
term = terms
else: try:
raise AnsibleError('Invalid terms passed to "var" lookup, "%s" is not a string, its a %s' % (terms, type(terms))) if term in myvars:
value = myvars[term]
if not isinstance(term, string_types): elif 'hostvars' in myvars and term in myvars['hostvars']:
raise AnsibleError('Invalid setting identifier, "%s" is not a string, its a %s' % (term, type(term))) # maybe it is a host var?
value = myvars['hostvars'][term]
try: else:
if term in myvars: raise AnsibleUndefinedVariable('No variable found with this name: %s' % term)
value = myvars[term] ret.append(self._templar.template(value, fail_on_undefined=True))
elif 'hostvars' in myvars and term in myvars['hostvars']:
# maybe it is a host var? except AnsibleUndefinedVariable:
value = myvars['hostvars'][term] if default is not None:
else: ret.append(default)
raise AnsibleUndefinedVariable('No variable found with this name: %s' % term) else:
ret = [self._templar.template(value, fail_on_undefined=True)] raise
except AnsibleUndefinedVariable:
if default is not None:
ret = [default]
else:
raise
return ret return ret

Loading…
Cancel
Save