mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
shuffle filter: added optional 'seed' parameter
This commit is contained in:
parent
f9385cb972
commit
fce9f4f679
2 changed files with 10 additions and 2 deletions
|
@ -185,6 +185,10 @@ To get a random list from an existing list::
|
|||
{{ ['a','b','c']|shuffle }} => ['c','a','b']
|
||||
{{ ['a','b','c']|shuffle }} => ['b','c','a']
|
||||
|
||||
As of Ansible version 2.3, it's also possible to shuffle a list idempotent. All you need is a seed.::
|
||||
|
||||
{{ ['a','b','c']|shuffle(seed=inventory_hostname) }} => ['b','a','c']
|
||||
|
||||
note that when used with a non 'listable' item it is a noop, otherwise it always returns a list
|
||||
|
||||
|
||||
|
|
|
@ -217,10 +217,14 @@ def rand(environment, end, start=None, step=None, seed=None):
|
|||
else:
|
||||
raise errors.AnsibleFilterError('random can only be used on sequences and integers')
|
||||
|
||||
def randomize_list(mylist):
|
||||
def randomize_list(mylist, seed=None):
|
||||
try:
|
||||
mylist = list(mylist)
|
||||
shuffle(mylist)
|
||||
if seed:
|
||||
r = Random(seed)
|
||||
r.shuffle(mylist)
|
||||
else:
|
||||
shuffle(mylist)
|
||||
except:
|
||||
pass
|
||||
return mylist
|
||||
|
|
Loading…
Reference in a new issue