Interview Prep/Ansible/Ad-hoc Commands, Modules and Idempotency

How do `async` and `poll` work for long-running tasks?

AdvancedConceptAd-hoc Commands, Modules and Idempotency
Answer

async: N lets a task run in the background for up to N seconds. poll: M makes Ansible check every M seconds and wait for completion, which keeps the SSH connection from timing out. With poll: 0 it fires and forgets, and you later check with async_status.

- ansible.builtin.command: /opt/backup/run.sh
  async: 3600
  poll: 0
  register: job

- ansible.builtin.async_status:
    jid: "{{ job.ansible_job_id }}"
  register: res
  until: res.finished
  retries: 120
  delay: 30

This runs the same job on many hosts in parallel without being limited by forks. Fire-and-forget tasks cannot report failure unless you check them, and async does not work with check_mode.

All Ansible questions Roadmap: Ansible

More on Ad-hoc Commands, Modules and Idempotency