Ansible / Advanced: orchestration, platforms, tests, and diagnosis / Lesson 21
Track asynchronous Ansible task completion
Distinguish asynchronous launch, polling and verified completion in a bounded automation workflow.
On this page
Lesson goals
- Compare positive polling with poll zero.
- Track job identity and final result per host.
- Plan safe cleanup and recovery for incomplete jobs.
Define the lifecycle
Ansible normally waits for a task to finish. Asynchronous execution adds a maximum time and a polling choice; it does not make a long operation safe or idempotent. Identify the host, operation, acceptable duration and recovery owner before use. The records here are fictional and no host or job was contacted. Review module and installed-version support before adapting the pattern.
Wait with positive polling
With async and a positive poll interval, Ansible waits for the task result while polling until completion, failure or timeout. The first record shows the intended waiting behavior without a runnable task. A timeout is not proof that every remote side effect was undone. Inspect the operation and service state before retrying a task that might already have changed data.
async_seconds: 300
poll_seconds: 10
next_task: waits-for-result
The record describes timing, not an executed Ansible playbook.
Track a detached launch
With poll zero, Ansible starts the asynchronous task and proceeds without waiting for completion. The second record deliberately marks completion unverified. Preserve the job identifier for each target and use the documented status mechanism in an authorized context before dependent work. A successful launch is not a successful result, and concurrent operations may conflict over shared resources.
async_seconds: 300
poll_seconds: 0
job_id: synthetic-job
completion: not-yet-verified
The synthetic identifier and status are not output from Ansible.
Handle failures and cleanup
Define what happens when a host is unreachable, the job exceeds its allowed time or its result is missing. The remote action may have partially completed, so inspect its actual state and coordinate recovery with the owner. Poll-zero jobs require explicit attention to their async cache cleanup according to the installed version and module behavior. Avoid deleting evidence before diagnosis.
Verify in an isolated lab
In a disposable lab, test a harmless bounded task with positive polling and poll zero. Verify that dependent work waits for completion and exercise a timeout. Record sanitized per-host results. A lab result does not prove production safety.
Common errors and fixes
A successful launch is reported as completion
- Why it happens
- Poll zero does not wait for the final result.
- How to fix it
- Check the job status for each host before dependent work.
- Prevent it next time
- Record launch and completion as distinct states.
A timeout is assumed to reverse remote changes
- Why it happens
- The operation may have partially changed state.
- How to fix it
- Inspect the target and follow the recovery plan.
- Prevent it next time
- Design idempotence and failure handling before use.
The async cache is forgotten
- Why it happens
- Detached job records may need explicit cleanup.
- How to fix it
- Use the documented cleanup procedure after preserving evidence.
- Prevent it next time
- Assign an owner and retention rule.
2 practice exercises
1. A poll-zero task returns a job ID. Can the next task assume the operation finished?
Show worked solution
No. Query the jobโs final result for the relevant host and gate dependent work on the approved success condition.
2. A positive-poll task times out after writing part of a file. Is a blind retry safe?
Show worked solution
Not necessarily. Inspect the target state and use the documented recovery or idempotent retry procedure.
Key takeaways
- Positive polling waits; poll zero launches without waiting.
- Job launch, completion and cleanup are distinct.
- Inspect partial effects before retrying.