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?
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.
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.
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.
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"}
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 ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With