mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-07 13:52:54 +00:00
make random_choice more error resilient (#27380)
* make random_choise more error resilient fixes #27373 * missing imports * PEEP 16
This commit is contained in:
@@ -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
|
||||||
|
|||||||
Reference in New Issue
Block a user