random filter: added optional 'seed' parameter

implements #15621
pull/18539/head
Brano Zarnovican 8 years ago committed by Toshio Kuratomi
parent ed00741a01
commit 96ddd29c7e

@ -168,6 +168,11 @@ Get a random number from 1 to 100 but in steps of 10::
{{ 100 |random(1, 10) }} => 31 {{ 100 |random(1, 10) }} => 31
{{ 100 |random(start=1, step=10) }} => 51 {{ 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 Shuffle Filter
-------------- --------------

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

Loading…
Cancel
Save