Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible task timeout max length

Tags:

docker

ansible

I execute a shell: docker ps ... task in some of my playbooks. This normally works but sometimes the docker daemon hangs and docker ps does not return for ~2 hours.

How can I configure Ansible to timeout in a reasonable amount of time if docker ps does not return?

like image 705
user18007 Avatar asked Jan 31 '17 00:01

user18007


People also ask

Does Ansible have a timeout?

Ansible will still block the next task in your playbook, waiting until the async task either completes, fails or times out. However, the task will only time out if it exceeds the timeout limit you set with the async parameter.

How do you handle long running tasks in Ansible?

By default, for Ansible tasks, the connections stay open until the task is done on each node, but this may not be the desired behavior for some functions as sometimes the task can take more time than SSH timeouts. You can run such long running tasks in the background and check their status later.

How do I delay an Ansible task?

To pause/wait/sleep per host, use the ansible. builtin. wait_for module. You can use ctrl+c if you wish to advance a pause earlier than it is set to expire or if you need to abort a playbook run entirely.


2 Answers

A task timeout is added in 2.10 release, which is useful in such scenario.

https://github.com/ansible/ansible/issues/33180

https://github.com/ansible/ansible/pull/69284

For example, below playbook fails in 2.10 version:

---
- hosts: localhost
  connection: local
  gather_facts: false
  tasks:
  - shell: |
      while true; do
        sleep 1
      done
    timeout: 5
...

with an error message like:

TASK [shell] **************************************************************************************************************************
fatal: [localhost]: FAILED! => {"changed": false, "msg": "The shell action failed to execute in the expected time frame (5) and was terminated"}
like image 194
Vijesh Avatar answered Oct 23 '22 14:10

Vijesh


There is no timeout-for-a-task-functionality implemented in Ansible.

You can try a workaround using asynchronous call, but for this case (clearly a kind of a bug) relying on the system might be easier and more appropriate.

See the GNU timeout command (if you run Docker, chances are the command is present on your OS):

shell: timeout 5m docker ps ...
like image 27
techraf Avatar answered Oct 23 '22 12:10

techraf