From db29190c28067dfe3ed31e89f3f91870bf809e10 Mon Sep 17 00:00:00 2001 From: Toshio Kuratomi Date: Wed, 7 Feb 2018 16:11:28 -0800 Subject: [PATCH] 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. --- lib/ansible/plugins/lookup/var.py | 47 +++++++++++++------------------ 1 file changed, 20 insertions(+), 27 deletions(-) diff --git a/lib/ansible/plugins/lookup/var.py b/lib/ansible/plugins/lookup/var.py index 954fdd53cc8..a6f26b4e0f7 100644 --- a/lib/ansible/plugins/lookup/var.py +++ b/lib/ansible/plugins/lookup/var.py @@ -63,8 +63,6 @@ from ansible.plugins.lookup import LookupBase class LookupModule(LookupBase): def run(self, terms, variables=None, **kwargs): - - ret = [] if variables is not None: self._templar.set_available_variables(variables) myvars = getattr(self._templar, '_available_variables', {}) @@ -72,30 +70,25 @@ class LookupModule(LookupBase): self.set_options(direct=kwargs) 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 - if isinstance(terms, list): - term = terms[0] - elif isinstance(terms, string_types): - term = terms - else: - raise AnsibleError('Invalid terms passed to "var" lookup, "%s" is not a string, its a %s' % (terms, type(terms))) - - if not isinstance(term, string_types): - raise AnsibleError('Invalid setting identifier, "%s" is not a string, its a %s' % (term, type(term))) - - try: - if term in myvars: - value = myvars[term] - elif 'hostvars' in myvars and term in myvars['hostvars']: - # maybe it is a host var? - value = myvars['hostvars'][term] - else: - raise AnsibleUndefinedVariable('No variable found with this name: %s' % term) - ret = [self._templar.template(value, fail_on_undefined=True)] - except AnsibleUndefinedVariable: - if default is not None: - ret = [default] - else: - raise + ret = [] + for term in terms: + if not isinstance(term, string_types): + raise AnsibleError('Invalid setting identifier, "%s" is not a string, its a %s' % (term, type(term))) + + try: + if term in myvars: + value = myvars[term] + elif 'hostvars' in myvars and term in myvars['hostvars']: + # maybe it is a host var? + value = myvars['hostvars'][term] + else: + raise AnsibleUndefinedVariable('No variable found with this name: %s' % term) + ret.append(self._templar.template(value, fail_on_undefined=True)) + + except AnsibleUndefinedVariable: + if default is not None: + ret.append(default) + else: + raise return ret