Fix collision in random filter name by merging functionality

Merges the functionality of the original jinja2 random filter with
the one we provide.

Fixes #7113
pull/7115/merge
James Cammarata 11 years ago
parent d88ac5e24d
commit a6068b09ac

@ -241,6 +241,13 @@ Random Number Filter
.. versionadded:: 1.6
This filter can be used similar to the default jinja2 random filter (returning a random item from a sequence of
items), but can also generate a random number based on a range.
To get a random item from a list::
{{ ['a','b','c']|random }} => 'c'
To get a random number from 0 to supplied end::
{{ 59 |random}} * * * * root /script/from/cron

@ -181,9 +181,20 @@ def version_compare(value, version, operator='eq', strict=False):
except Exception, e:
raise errors.AnsibleFilterError('Version comparison: %s' % e)
def rand(end, start=0, step=1):
def rand(end, start=None, step=None):
r = SystemRandom()
return r.randrange(start, end, step)
if isinstance(end, (int, long)):
if not start:
start = 0
if not step:
step = 1
return r.randrange(start, end, step)
elif hasattr(end, '__iter__'):
if start or step:
raise errors.AnsibleFilterError('start and step can only be used with integer values')
return r.choice(end)
else:
raise errors.AnsibleFilterError('random can only be used on sequences and integers')
class FilterModule(object):
''' Ansible core jinja2 filters '''

Loading…
Cancel
Save