From d35b9569006be8b6fb2090b24969b678166386e2 Mon Sep 17 00:00:00 2001 From: Toshio Kuratomi Date: Mon, 10 Aug 2015 09:07:37 -0700 Subject: [PATCH] listify lookup plugin terms when they're specified as "{{ lookup(terms) }}" Before this, they were not listified there but they were listified when specified like this: with_lookup: terms --- lib/ansible/executor/task_executor.py | 1 - lib/ansible/plugins/lookup/cartesian.py | 10 ++++++++-- lib/ansible/plugins/lookup/credstash.py | 3 --- lib/ansible/plugins/lookup/csvfile.py | 3 --- lib/ansible/plugins/lookup/dnstxt.py | 3 --- lib/ansible/plugins/lookup/env.py | 3 --- lib/ansible/plugins/lookup/etcd.py | 3 --- lib/ansible/plugins/lookup/file.py | 3 --- lib/ansible/plugins/lookup/flattened.py | 1 + lib/ansible/plugins/lookup/indexed_items.py | 1 + lib/ansible/plugins/lookup/ini.py | 3 --- lib/ansible/plugins/lookup/inventory_hostnames.py | 1 + lib/ansible/plugins/lookup/items.py | 3 --- lib/ansible/plugins/lookup/nested.py | 4 ++-- lib/ansible/plugins/lookup/password.py | 3 --- lib/ansible/plugins/lookup/pipe.py | 3 --- lib/ansible/plugins/lookup/redis_kv.py | 3 --- lib/ansible/plugins/lookup/sequence.py | 3 --- lib/ansible/plugins/lookup/subelements.py | 1 - lib/ansible/plugins/lookup/template.py | 3 --- lib/ansible/plugins/lookup/together.py | 4 ++-- lib/ansible/plugins/lookup/url.py | 3 --- lib/ansible/template/__init__.py | 1 + 23 files changed, 16 insertions(+), 50 deletions(-) diff --git a/lib/ansible/executor/task_executor.py b/lib/ansible/executor/task_executor.py index 297d8b2526f..c5e532fe45b 100644 --- a/lib/ansible/executor/task_executor.py +++ b/lib/ansible/executor/task_executor.py @@ -153,7 +153,6 @@ class TaskExecutor: items = None if self._task.loop: if self._task.loop in self._shared_loader_obj.lookup_loader: - loop_terms = listify_lookup_plugin_terms(terms=self._task.loop_args, templar=templar, loader=self._loader, fail_on_undefined=True) items = self._shared_loader_obj.lookup_loader.get(self._task.loop, loader=self._loader, templar=templar).run(terms=loop_terms, variables=vars_copy) else: raise AnsibleError("Unexpected failure in finding the lookup named '%s' in the available lookup plugins" % self._task.loop) diff --git a/lib/ansible/plugins/lookup/cartesian.py b/lib/ansible/plugins/lookup/cartesian.py index 9ae18587aeb..ce31ef9700b 100644 --- a/lib/ansible/plugins/lookup/cartesian.py +++ b/lib/ansible/plugins/lookup/cartesian.py @@ -29,7 +29,13 @@ class LookupModule(LookupBase): [1, 2, 3], [a, b] -> [1, a], [1, b], [2, a], [2, b], [3, a], [3, b] """ - def __lookup_variables(self, terms): + def _lookup_variables(self, terms): + """ + Turn this: + terms == ["1,2,3", "a,b"] + into this: + terms == [[1,2,3], [a, b]] + """ results = [] for x in terms: intermediate = listify_lookup_plugin_terms(x, templar=self._templar, loader=self._loader) @@ -38,7 +44,7 @@ class LookupModule(LookupBase): def run(self, terms, variables=None, **kwargs): - terms = self.__lookup_variables(terms) + terms = self._lookup_variables(terms) my_list = terms[:] if len(my_list) == 0: diff --git a/lib/ansible/plugins/lookup/credstash.py b/lib/ansible/plugins/lookup/credstash.py index 6587d525ffa..9d548baea6e 100644 --- a/lib/ansible/plugins/lookup/credstash.py +++ b/lib/ansible/plugins/lookup/credstash.py @@ -35,9 +35,6 @@ class LookupModule(LookupBase): if not CREDSTASH_INSTALLED: raise AnsibleError('The credstash lookup plugin requires credstash to be installed.') - if isinstance(terms, basestring): - terms = [terms] - ret = [] for term in terms: try: diff --git a/lib/ansible/plugins/lookup/csvfile.py b/lib/ansible/plugins/lookup/csvfile.py index 1a277887222..478f063a120 100644 --- a/lib/ansible/plugins/lookup/csvfile.py +++ b/lib/ansible/plugins/lookup/csvfile.py @@ -42,9 +42,6 @@ class LookupModule(LookupBase): def run(self, terms, variables=None, **kwargs): - if isinstance(terms, basestring): - terms = [ terms ] - basedir = self.get_basedir(variables) ret = [] diff --git a/lib/ansible/plugins/lookup/dnstxt.py b/lib/ansible/plugins/lookup/dnstxt.py index e9dd27bfb6c..59d3820f710 100644 --- a/lib/ansible/plugins/lookup/dnstxt.py +++ b/lib/ansible/plugins/lookup/dnstxt.py @@ -44,9 +44,6 @@ class LookupModule(LookupBase): if HAVE_DNS == False: raise AnsibleError("Can't LOOKUP(dnstxt): module dns.resolver is not installed") - if isinstance(terms, basestring): - terms = [ terms ] - ret = [] for term in terms: domain = term.split()[0] diff --git a/lib/ansible/plugins/lookup/env.py b/lib/ansible/plugins/lookup/env.py index 55847dd7779..0314863f6a6 100644 --- a/lib/ansible/plugins/lookup/env.py +++ b/lib/ansible/plugins/lookup/env.py @@ -25,9 +25,6 @@ class LookupModule(LookupBase): def run(self, terms, variables, **kwargs): - if isinstance(terms, basestring): - terms = [ terms ] - ret = [] for term in terms: var = term.split()[0] diff --git a/lib/ansible/plugins/lookup/etcd.py b/lib/ansible/plugins/lookup/etcd.py index 46a81e4d6bb..25b5c049e27 100644 --- a/lib/ansible/plugins/lookup/etcd.py +++ b/lib/ansible/plugins/lookup/etcd.py @@ -66,9 +66,6 @@ class LookupModule(LookupBase): def run(self, terms, variables, **kwargs): - if isinstance(terms, basestring): - terms = [ terms ] - validate_certs = kwargs.get('validate_certs', True) etcd = Etcd(validate_certs=validate_certs) diff --git a/lib/ansible/plugins/lookup/file.py b/lib/ansible/plugins/lookup/file.py index 2498f90c9cc..cbfb8f31b48 100644 --- a/lib/ansible/plugins/lookup/file.py +++ b/lib/ansible/plugins/lookup/file.py @@ -27,9 +27,6 @@ class LookupModule(LookupBase): def run(self, terms, variables=None, **kwargs): - if not isinstance(terms, list): - terms = [ terms ] - ret = [] basedir = self.get_basedir(variables) diff --git a/lib/ansible/plugins/lookup/flattened.py b/lib/ansible/plugins/lookup/flattened.py index 7477db4b834..b9e1009c26c 100644 --- a/lib/ansible/plugins/lookup/flattened.py +++ b/lib/ansible/plugins/lookup/flattened.py @@ -63,6 +63,7 @@ class LookupModule(LookupBase): def run(self, terms, variables, **kwargs): + ### FIXME: Is this needed now that listify is run on all lookup plugin terms? if not isinstance(terms, list): raise AnsibleError("with_flattened expects a list") diff --git a/lib/ansible/plugins/lookup/indexed_items.py b/lib/ansible/plugins/lookup/indexed_items.py index 9e242ac6bfc..4721918f309 100644 --- a/lib/ansible/plugins/lookup/indexed_items.py +++ b/lib/ansible/plugins/lookup/indexed_items.py @@ -27,6 +27,7 @@ class LookupModule(LookupBase): def run(self, terms, variables, **kwargs): + ### FIXME: Is this needed now that listify is run on all lookup plugin terms? if not isinstance(terms, list): raise AnsibleError("with_indexed_items expects a list") diff --git a/lib/ansible/plugins/lookup/ini.py b/lib/ansible/plugins/lookup/ini.py index 0c04f06909d..7ea8f92aaf5 100644 --- a/lib/ansible/plugins/lookup/ini.py +++ b/lib/ansible/plugins/lookup/ini.py @@ -53,9 +53,6 @@ class LookupModule(LookupBase): def run(self, terms, variables=None, **kwargs): - if isinstance(terms, basestring): - terms = [ terms ] - basedir = self.get_basedir(variables) self.basedir = basedir self.cp = ConfigParser.ConfigParser() diff --git a/lib/ansible/plugins/lookup/inventory_hostnames.py b/lib/ansible/plugins/lookup/inventory_hostnames.py index d09dec0c7b5..046e1482597 100644 --- a/lib/ansible/plugins/lookup/inventory_hostnames.py +++ b/lib/ansible/plugins/lookup/inventory_hostnames.py @@ -25,6 +25,7 @@ from ansible.plugins.lookup import LookupBase class LookupModule(LookupBase): def run(self, terms, inject=None, **kwargs): + ### FIXME: Is this needed now that listify is run on all lookup plugin terms? if not isinstance(terms, list): raise AnsibleError("with_inventory_hostnames expects a list") diff --git a/lib/ansible/plugins/lookup/items.py b/lib/ansible/plugins/lookup/items.py index 65ff66d854a..43bb77e1449 100644 --- a/lib/ansible/plugins/lookup/items.py +++ b/lib/ansible/plugins/lookup/items.py @@ -23,8 +23,5 @@ class LookupModule(LookupBase): def run(self, terms, **kwargs): - if not isinstance(terms, list): - terms = [ terms ] - return self._flatten(terms) diff --git a/lib/ansible/plugins/lookup/nested.py b/lib/ansible/plugins/lookup/nested.py index 23938f6a19c..ff865c28eec 100644 --- a/lib/ansible/plugins/lookup/nested.py +++ b/lib/ansible/plugins/lookup/nested.py @@ -25,7 +25,7 @@ from ansible.utils.listify import listify_lookup_plugin_terms class LookupModule(LookupBase): - def __lookup_variables(self, terms, variables): + def _lookup_variables(self, terms, variables): foo = variables.copy() foo.pop('vars') results = [] @@ -39,7 +39,7 @@ class LookupModule(LookupBase): def run(self, terms, variables=None, **kwargs): - terms = self.__lookup_variables(terms, variables) + terms = self._lookup_variables(terms, variables) my_list = terms[:] my_list.reverse() diff --git a/lib/ansible/plugins/lookup/password.py b/lib/ansible/plugins/lookup/password.py index 47ec786429e..3c80e6811f4 100644 --- a/lib/ansible/plugins/lookup/password.py +++ b/lib/ansible/plugins/lookup/password.py @@ -59,9 +59,6 @@ class LookupModule(LookupBase): ret = [] - if not isinstance(terms, list): - terms = [ terms ] - for term in terms: # you can't have escaped spaces in yor pathname params = term.split() diff --git a/lib/ansible/plugins/lookup/pipe.py b/lib/ansible/plugins/lookup/pipe.py index d9f74708b28..71b0ed97774 100644 --- a/lib/ansible/plugins/lookup/pipe.py +++ b/lib/ansible/plugins/lookup/pipe.py @@ -26,9 +26,6 @@ class LookupModule(LookupBase): def run(self, terms, variables, **kwargs): - if isinstance(terms, basestring): - terms = [ terms ] - ret = [] for term in terms: ''' diff --git a/lib/ansible/plugins/lookup/redis_kv.py b/lib/ansible/plugins/lookup/redis_kv.py index e499e83f938..982cceebef8 100644 --- a/lib/ansible/plugins/lookup/redis_kv.py +++ b/lib/ansible/plugins/lookup/redis_kv.py @@ -43,9 +43,6 @@ class LookupModule(LookupBase): if not HAVE_REDIS: raise AnsibleError("Can't LOOKUP(redis_kv): module redis is not installed") - if not isinstance(terms, list): - terms = [ terms ] - ret = [] for term in terms: (url,key) = term.split(',') diff --git a/lib/ansible/plugins/lookup/sequence.py b/lib/ansible/plugins/lookup/sequence.py index 5cd87f4f524..280f9ec8512 100644 --- a/lib/ansible/plugins/lookup/sequence.py +++ b/lib/ansible/plugins/lookup/sequence.py @@ -184,9 +184,6 @@ class LookupModule(LookupBase): def run(self, terms, variables, **kwargs): results = [] - if isinstance(terms, basestring): - terms = [ terms ] - for term in terms: try: self.reset() # clear out things for this iteration diff --git a/lib/ansible/plugins/lookup/subelements.py b/lib/ansible/plugins/lookup/subelements.py index e014e382bab..5ac384f6f14 100644 --- a/lib/ansible/plugins/lookup/subelements.py +++ b/lib/ansible/plugins/lookup/subelements.py @@ -34,7 +34,6 @@ class LookupModule(LookupBase): "subelements lookup expects a list of two or three items, " + msg) - terms = listify_lookup_plugin_terms(terms, templar=self._templar, loader=self._loader) terms[0] = listify_lookup_plugin_terms(terms[0], templar=self._templar, loader=self._loader) # check lookup terms - check number of terms diff --git a/lib/ansible/plugins/lookup/template.py b/lib/ansible/plugins/lookup/template.py index 8f793a2066f..16b86c2de5b 100644 --- a/lib/ansible/plugins/lookup/template.py +++ b/lib/ansible/plugins/lookup/template.py @@ -27,9 +27,6 @@ class LookupModule(LookupBase): def run(self, terms, variables, **kwargs): - if not isinstance(terms, list): - terms = [ terms ] - basedir = self.get_basedir(variables) ret = [] diff --git a/lib/ansible/plugins/lookup/together.py b/lib/ansible/plugins/lookup/together.py index 42c98455070..ac94a414f25 100644 --- a/lib/ansible/plugins/lookup/together.py +++ b/lib/ansible/plugins/lookup/together.py @@ -31,7 +31,7 @@ class LookupModule(LookupBase): [1, 2], [3] -> [1, 3], [2, None] """ - def __lookup_variables(self, terms): + def _lookup_variables(self, terms): results = [] for x in terms: intermediate = listify_lookup_plugin_terms(x, templar=self._templar, loader=self._loader) @@ -40,7 +40,7 @@ class LookupModule(LookupBase): def run(self, terms, variables=None, **kwargs): - terms = self.__lookup_variables(terms) + terms = self._lookup_variables(terms) my_list = terms[:] if len(my_list) == 0: diff --git a/lib/ansible/plugins/lookup/url.py b/lib/ansible/plugins/lookup/url.py index 216b07d1f86..a893493db4a 100644 --- a/lib/ansible/plugins/lookup/url.py +++ b/lib/ansible/plugins/lookup/url.py @@ -29,9 +29,6 @@ class LookupModule(LookupBase): def run(self, terms, variables=None, **kwargs): - if isinstance(terms, basestring): - terms = [ terms ] - validate_certs = kwargs.get('validate_certs', True) ret = [] diff --git a/lib/ansible/template/__init__.py b/lib/ansible/template/__init__.py index ec9866c3143..f34a069f2f4 100644 --- a/lib/ansible/template/__init__.py +++ b/lib/ansible/template/__init__.py @@ -251,6 +251,7 @@ class Templar: instance = self._lookup_loader.get(name.lower(), loader=self._loader, templar=self) if instance is not None: + loop_terms = listify_lookup_plugin_terms(terms=8args, templar=self, loader=self._loader, fail_on_undefined=True) # safely catch run failures per #5059 try: ran = instance.run(*args, variables=self._available_variables, **kwargs)