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.
This commit is contained in:
Toshio Kuratomi 2018-01-29 13:36:51 -08:00
parent 3729cc57d0
commit 3d614bfe84

View file

@ -27,6 +27,7 @@ import pwd
import re import re
import time import time
from collections import Sequence
from functools import wraps from functools import wraps
from io import StringIO from io import StringIO
from numbers import Number from numbers import Number
@ -635,7 +636,15 @@ class Templar:
try: try:
ran = UnsafeProxy(",".join(ran)) ran = UnsafeProxy(",".join(ran))
except TypeError: 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]) ran = wrap_var(ran[0])
else: else:
ran = wrap_var(ran) ran = wrap_var(ran)