Is there a way to guarantee idempotence for playbooks that use randomly generated variables?
For example, I want to setup my crontabs to trigger emails on multiple serve
As of Ansible version 2.3, it’s possible to initialize the random number generator from a seed. This way, you can create random-but-idempotent numbers:
"{{ 59 |random(seed=inventory_hostname) }} * * * * root /script/from/cron"
Source: random number filter
I've used this pattern to produce random cron start times with:
Requires Ansible >=2.3:
cron:
name: "{{some_name}}_{{item.day}}"
state: present
job: "{{some_job}}"
weekday: "{{item.day}}"
hour: "{{item.hour}}"
minute: "{{59|random(seed=inventory_hostname + item.dow)}}"
with_items:
- { day: 0, hour: 3, dow: "sunday" }
- { day: 1, hour: 7, dow: "monday" }
- { day: 2, hour: 1, dow: "tuesday" }
- { day: 3, hour: 5, dow: "wednesday" }
- { day: 4, hour: 2, dow: "thursday" }
- { day: 5, hour: 4, dow: "friday" }
- { day: 6, hour: 7, dow: "saturday" }