random filter: added optional 'seed' parameter

implements #15621
This commit is contained in:
Brano Zarnovican 2016-11-13 09:13:12 +01:00 committed by Toshio Kuratomi
parent ed00741a01
commit 96ddd29c7e
2 changed files with 11 additions and 3 deletions

View file

@ -168,6 +168,11 @@ Get a random number from 1 to 100 but in steps of 10::
{{ 100 |random(1, 10) }} => 31
{{ 100 |random(start=1, step=10) }} => 51
It's also possible to initialize random number generator from seed. This way, you can create random-but-idempotent
numbers (new in version 2.3)::
{{ 59 |random(seed=inventory_hostname) }} * * * * root /script/from/cron
Shuffle Filter
--------------

View file

@ -31,7 +31,7 @@ import crypt
import hashlib
import string
from functools import partial
from random import SystemRandom, shuffle
from random import Random, SystemRandom, shuffle
from datetime import datetime
import uuid
@ -199,8 +199,11 @@ def from_yaml(data):
return data
@environmentfilter
def rand(environment, end, start=None, step=None):
r = SystemRandom()
def rand(environment, end, start=None, step=None, seed=None):
if seed is None:
r = SystemRandom()
else:
r = Random(seed)
if isinstance(end, (int, long)):
if not start:
start = 0