mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
added new 'shuffle' filter
This commit is contained in:
parent
8cecb0690f
commit
3534bdf953
2 changed files with 19 additions and 2 deletions
|
@ -297,6 +297,19 @@ Get a random number from 1 to 100 but in steps of 10::
|
||||||
{{ 100 |random(start=1, step=10) }} => 51
|
{{ 100 |random(start=1, step=10) }} => 51
|
||||||
|
|
||||||
|
|
||||||
|
Shuffle Filter
|
||||||
|
--------------
|
||||||
|
|
||||||
|
.. versionadded:: 1.8
|
||||||
|
|
||||||
|
This filter will randomize an existing list, giving a differnt order every invocation.
|
||||||
|
|
||||||
|
To get a random list from an existing list::
|
||||||
|
|
||||||
|
{{ ['a','b','c']|shuffle }} => ['c','a','b']
|
||||||
|
{{ ['a','b','c']|shuffle }} => ['b','c','a']
|
||||||
|
|
||||||
|
|
||||||
.. _other_useful_filters:
|
.. _other_useful_filters:
|
||||||
|
|
||||||
Other Useful Filters
|
Other Useful Filters
|
||||||
|
|
|
@ -28,7 +28,7 @@ import operator as py_operator
|
||||||
from ansible import errors
|
from ansible import errors
|
||||||
from ansible.utils import md5s
|
from ansible.utils import md5s
|
||||||
from distutils.version import LooseVersion, StrictVersion
|
from distutils.version import LooseVersion, StrictVersion
|
||||||
from random import SystemRandom
|
from random import SystemRandom, shuffle
|
||||||
from jinja2.filters import environmentfilter
|
from jinja2.filters import environmentfilter
|
||||||
|
|
||||||
|
|
||||||
|
@ -235,6 +235,9 @@ def rand(environment, end, start=None, step=None):
|
||||||
else:
|
else:
|
||||||
raise errors.AnsibleFilterError('random can only be used on sequences and integers')
|
raise errors.AnsibleFilterError('random can only be used on sequences and integers')
|
||||||
|
|
||||||
|
def randomize_list(mylist):
|
||||||
|
shuffle(mylist)
|
||||||
|
return mylist
|
||||||
|
|
||||||
class FilterModule(object):
|
class FilterModule(object):
|
||||||
''' Ansible core jinja2 filters '''
|
''' Ansible core jinja2 filters '''
|
||||||
|
@ -305,6 +308,7 @@ class FilterModule(object):
|
||||||
# version comparison
|
# version comparison
|
||||||
'version_compare': version_compare,
|
'version_compare': version_compare,
|
||||||
|
|
||||||
# random numbers
|
# random stuff
|
||||||
'random': rand,
|
'random': rand,
|
||||||
|
'shuffle': randomize_list,
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue