Learning Ansible
The “async” task parameter can be used to push a command to the background and continue the play or task-list. This is useful for long-running operations, like installing a large application or waiting for a reboot to complete.
This parameter “async” can also be used to run a long running job and keep the connection alive.
- name: Do something that takes a long time
ansible.builtin.package:
name: "*"
state: latest
async: 3600
poll: 0
register: long_running_task
- name: Do something in between
ansible.builtin.command:
cmd: "echo 'Hello world!'"
- name: Check if the long running task is done
ansible.builtin.async_status:
jid: ""
loop: ""
register: job_status
until: job_status.finished
retries: 359
delay: 10
In the example above, the command will be pushed to the background and Ansible will continue with the next task. Ansible will check every 10 seconds if the command has completed. If the command has not completed after 3600 seconds, Ansible will fail the task.
Write a playbook that has a couple of tasks:
async
to push this job to the background.Have a look at this solution.