It might look very simple, but I haven’t found it anywhere already described.

So I wanted to execute some task in Ansible playbook not always, with random decision.

And the solution is simple as pie:

tasks:
- name: Random task
debug:
msg: 'This task runs randomly'
when: 10 | random | bool

So here we use Random Number Filter with bool filter. It will generate numbers from 0 to 10, and thanks to bool filter, the result will be true only if value will be equal to 1. According to this, this task will be executed once in 10 runs. You can change the number, to get more or less probability.

--

--