make random_choice more error resilient (#27380)
* make random_choise more error resilient fixes #27373 * missing imports * PEEP 16
This commit is contained in:
parent
e575ff8d8d
commit
b79744f282
1 changed files with 10 additions and 1 deletions
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue