简体   繁体   中英

Rolling restart with ansible handlers

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?

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

Currently it's not possible. There is an issue oppened for this.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM