Need a random number for your Ansible playbook? But want to be idempotent on subsequent runs? There is an answer!
Let's say you want to register
cron
jobs on a bunch of servers and don't want it to start on the same time. You can use:but this will generate random number during each playbook execution, giving you unnecessary
changed
state for tasks. Winx club pc game download.Update for Ansible 2.3:
As of Ansible version 2.3, it’s also possible to initialize the random number generator from a seed. This way, you can create random-but-idempotent numbers:
You must either add a leading zero so that Ansible's YAML parser knows it is an octal number (like 0644 or 01777) or quote it (like '644' or '1777') so Ansible receives a string and can do its own conversion from string into number. Generate a random string of a fixed length. Generate a random string with a combination of lower and upper case letters. Create a unique random string without repeating characters in it; Generate a random alphanumeric string with letters and numbers. Generate a random string password which contains the letters, digits, and special characters.
Ansible Generate Random String Python
https://how-to-eject-dvd-in-vlc-for-mac4.peatix.com. Kia spectra 2008 repair or maintenance manual.
'{{ 59 |random(seed=inventory_hostname) }} * * * * root /script/from/cron'
For previous Ansible versions:
But you can craft a pseudo-random number based on any variable/fact you want. For example, you can choose
inventory_hostname
to make this number different between servers but the same on subsequent playbook runs:Magic explained:
- we take
inventory_hostname
string (e.g.'myserver'
) - make a
hash
from it ('c3a7a35a28dcce27daad3a7a90caad99b967a904'
) - split it into array of characters (
['c','3','a',.]
)
where every character is a hexadecimal digit - apply
int
filter withbase=16
to every character to convert it to number0.15
([12,3,10,.]
) sum
all numbers (334
)- limit our pseudo-random number by taking the remainder of division
% 60
(34
)
Ansible Generate Random Password
So your cron task may look like:
5.1 surround sound speaker test. This will start
myscript.sh
at some random time between 6:00
and 7:59
and this time will be idempotent on subsequent playbook runs.