Commit 0edaa6f7 by Brian Coca

Merge pull request #9270 from bcoca/do_the_shuffle

added new 'shuffle' filter
parents 11b63410 b592d765
......@@ -297,6 +297,20 @@ Get a random number from 1 to 100 but in steps of 10::
{{ 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']
note that when used with a non 'listable' item it is a noop, otherwise it always returns a list
.. _other_useful_filters:
Other Useful Filters
......
......@@ -28,7 +28,7 @@ import operator as py_operator
from ansible import errors
from ansible.utils import md5s, checksum_s
from distutils.version import LooseVersion, StrictVersion
from random import SystemRandom
from random import SystemRandom, shuffle
from jinja2.filters import environmentfilter
......@@ -235,6 +235,13 @@ def rand(environment, end, start=None, step=None):
else:
raise errors.AnsibleFilterError('random can only be used on sequences and integers')
def randomize_list(mylist):
try:
mylist = list(mylist)
shuffle(mylist)
except:
pass
return mylist
class FilterModule(object):
''' Ansible core jinja2 filters '''
......@@ -310,6 +317,7 @@ class FilterModule(object):
# version comparison
'version_compare': version_compare,
# random numbers
# random stuff
'random': rand,
'shuffle': randomize_list,
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment