ansible.builtin.random filter – random number or list item
Note
This filter plugin is part of ansible-core
and included in all Ansible
installations. In most cases, you can use the short
plugin name
random
.
However, we recommend you use the Fully Qualified Collection Name (FQCN) ansible.builtin.random
for easy linking to the
plugin documentation and to avoid conflicting with other collections that may have
the same filter plugin name.
Synopsis
Use the input to either select a random element of a list or generate a random number.
Input
This describes the input of the filter, the value before | ansible.builtin.random
.
Positional parameters
This describes positional parameters of the filter. These are the values positional1
, positional2
and so on in the following
example: input | ansible.builtin.random(positional1, positional2, ...)
Examples
# can be any item from the list
random_item: "{{ ['a','b','c'] | random }}"
# cron line, select random minute repeatable for each host
"{{ 60 | random(seed=inventory_hostname) }} * * * * root /script/from/cron"
Return Value
Hint
Configuration entries for each entry type have a low to high priority order. For example, a variable that is lower in the list will override a variable that is higher up.