I want to run an ansible playbook that installs a service and restarts it if anything has changed since the last run (more or less the canonical use-case for ansible handlers).
But I want a different parallelism for installing than for restarting: I want to install on all the hosts at a time but, if the "service-restart" handler gets invoked I want that to run on X hosts at a time.
I know this is possible with different plays that have different serial
values. But I can't see how I could make use of handlers if I go this route. And I can't afford to have a single playbook with a serial
value like 2
, as most of the time nothing will change for that service.
Can the handlers span multiple plays? Or is there any other way to do this without hacks?
Ansible 2.9.0 introduced the throttle
keyword, which can be used at the task, block, or play level to limit the number of workers (up to the specified forks or serial setting) allowed.
For example, this can be used to restart nodes in a database cluster one by one:
- name: Restart MySQL
throttle: 1
service:
name: mysql
state: restarted
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With