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?
Currently it's not possible. There is an issue oppened for this.
Handlers are just tasks that Ansible will run at the end of a play if necessary. Given that they're implicitly added to the end of your play, they're going to be treated the same as any other tasks as far as parameters like serial
go. Unfortunately this means that without a feature request that the Ansible developers accept you're unlikely to see a change in the behavior of serial
to support what you're trying to do.
I know you mentioned wanting to avoid hacks, but that's going to be the only way you can do something like this at this point. It shouldn't be too difficult to set up something that's not a major hack, like creating a temporary file to flag the restart:
- hosts: some_hosts
name: install service
serial: 10
- handlers:
- name: schedule restart
command: touch /tmp/restart_flag
- tasks:
- name: install service
action: whatever...
notify: schedule restart
- hosts: some_hosts
name: restart service
serial: 2
- handlers:
- name: perform restart
service: name=foo state=restarted
- tasks:
- name: Delete /tmp/restart_flag. Restart service if file is deleted.
file: path=/tmp/restart_flag state=absent
notify: perform restart