Ansible – How to Run One Task Host by Host

ansibleansible-playbooktask

On the play-level, we have serial: 1 to allow us to run the whole play one host at a time. But I haven't found a simple way to do this on a single task. This is especially relevant, if the task in question doesn't perform proper locking (for whatever reason).

One obvious answer is to put the task in its own play. But that doesn't help with roles. (Having to put serial: 1 on the play using the role isn't really intuitive.)

Best Answer

You can run a single task in serial (i.e. host-by-host) by adding throttle: 1 to it.

Example:

---

- hosts: all
  tasks:
      - name: wait in parallel
        command: sleep 20
      - name: wait  in serial
        command: sleep 30
        throttle: 1

References:

NB: throttle was introduced in Ansible 2.9