make random_choice more error resilient (#27380)

* make random_choise more error resilient

fixes #27373

* missing imports

* PEEP 16
pull/27495/head
Brian Coca 7 years ago committed by GitHub
parent e575ff8d8d
commit b79744f282

@ -19,6 +19,8 @@ __metaclass__ = type
import random import random
from ansible.errors import AnsibleError
from ansible.module_utils._text import to_native
from ansible.plugins.lookup import LookupBase from ansible.plugins.lookup import LookupBase
# useful for introducing chaos ... or just somewhat reasonably fair selection # useful for introducing chaos ... or just somewhat reasonably fair selection
@ -36,4 +38,11 @@ class LookupModule(LookupBase):
def run(self, terms, inject=None, **kwargs): 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

Loading…
Cancel
Save