How to execute long running tasks using Ansible

We can use the async module in Ansible to execute long running tasks. This will trigger the task to run in background.

Since this is a long running task, make sure the SSH connections stays alive and for that we can use poll interval which makes the SSH session alive.

Example playbook is given below.

- name: Simulate long running op (15 sec), wait for up to 45 sec, poll every 5 sec
    ansible.builtin.command: /bin/sleep 15
    async: 45
    poll: 5

In this same way we can run any tasks. One disadvantage of this method is that we should have an idea regarding approximate time of task completion.

That's all…

Leave a Reply