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.
More on Ad-hoc Commands, Modules and Idempotency
What is an ad-hoc command and what is its syntax?BeginnerWhat does `ansible all -m ping` actually test?BeginnerGive practical use cases for ad-hoc commands.IntermediateWhat is the difference between the `command` and `shell` modules?BeginnerCompare `raw`, `command`, `shell` and `script`.IntermediateWhy prefer a dedicated module over `shell`/`command`?Beginner