From 3d614bfe84d7274b0042685625b5c5b5c27f50f1 Mon Sep 17 00:00:00 2001 From: Toshio Kuratomi Date: Mon, 29 Jan 2018 13:36:51 -0800 Subject: [PATCH] Clarify lookup plugin TypeError recovery Change the code and comments around recovering from a TypeError in handling the return value from lookup plugins to make it clearer what we're recovering from. --- lib/ansible/template/__init__.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/ansible/template/__init__.py b/lib/ansible/template/__init__.py index 79756dd242..4cd9ee8b3f 100644 --- a/lib/ansible/template/__init__.py +++ b/lib/ansible/template/__init__.py @@ -27,6 +27,7 @@ import pwd import re import time +from collections import Sequence from functools import wraps from io import StringIO from numbers import Number @@ -635,7 +636,15 @@ class Templar: try: ran = UnsafeProxy(",".join(ran)) except TypeError: - if isinstance(ran, list) and len(ran) == 1: + # Lookup Plugins should always return lists. Throw an error if that's not + # the case: + if not isinstance(ran, Sequence): + raise AnsibleError("The lookup plugin '%s' did not return a list." + % name) + + # The TypeError we can recover from is when the value *inside* of the list + # is not a string + if len(ran) == 1: ran = wrap_var(ran[0]) else: ran = wrap_var(ran)