make random_choice more error resilient (#27380)

* make random_choise more error resilient

fixes #27373

* missing imports

* PEEP 16
This commit is contained in:
Brian Coca 2017-07-31 11:44:53 -04:00 committed by GitHub
parent e575ff8d8d
commit b79744f282

View file

@ -19,6 +19,8 @@ __metaclass__ = type
import random
from ansible.errors import AnsibleError
from ansible.module_utils._text import to_native
from ansible.plugins.lookup import LookupBase
# useful for introducing chaos ... or just somewhat reasonably fair selection
@ -36,4 +38,11 @@ class LookupModule(LookupBase):
def run(self, terms, inject=None, **kwargs):
return [random.choice(terms)]
ret = terms
if terms:
try:
ret = [random.choice(terms)]
except Exception as e:
raise AnsibleError("Unable to choose random term: %s" % to_native(e))
return ret