From b79744f2823f972764cdda178d4d220ff0358fdc Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Mon, 31 Jul 2017 11:44:53 -0400 Subject: [PATCH] make random_choice more error resilient (#27380) * make random_choise more error resilient fixes #27373 * missing imports * PEEP 16 --- lib/ansible/plugins/lookup/random_choice.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/ansible/plugins/lookup/random_choice.py b/lib/ansible/plugins/lookup/random_choice.py index 2b016c78d91..8888db43bc4 100644 --- a/lib/ansible/plugins/lookup/random_choice.py +++ b/lib/ansible/plugins/lookup/random_choice.py @@ -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