Run the task asynchronously. See Asynchronous actions and polling. For example, set async: 300
to timeout after 5 minutes. To poll with the period of 5 seconds set poll: 60
- name: Get disk size from server1
shell: time df -h
delegate_to: server-1
run_once: true
ignore_errors: true
async: 300
poll: 60
Test it. For example, given the inventory
shell> cat hosts
test_11
test_13
The below playbook runs the task asynchronously with a timeout of 15 seconds while the command sleeps 20 seconds. The play will poll the command periodically every 5 seconds and then the task will timeout
shell> cat pb.yml
- hosts: all
tasks:
- command: date
register: out
- debug:
var: out.stdout
- command: sleep 20
register: out
delegate_to: test_13
run_once: true
ignore_errors: true
async: 15
poll: 3
- debug:
msg: Play running ...
run_once: true
gives
shell> ansible-playbook pb.yml
PLAY [all] ************************************************************************************
TASK [command] ********************************************************************************
changed: [test_13]
changed: [test_11]
TASK [debug] **********************************************************************************
ok: [test_11] =>
out.stdout: Wed Jul 19 04:22:11 UTC 2023
ok: [test_13] =>
out.stdout: Wed Jul 19 04:22:11 UTC 2023
TASK [command] ********************************************************************************
ASYNC POLL on test_11: jid=551192544238.49820 started=1 finished=0
ASYNC POLL on test_11: jid=551192544238.49820 started=1 finished=0
ASYNC POLL on test_11: jid=551192544238.49820 started=1 finished=0
ASYNC POLL on test_11: jid=551192544238.49820 started=1 finished=0
ASYNC FAILED on test_11: jid=551192544238.49820
fatal: [test_11 -> test_13]: FAILED! => changed=false
ansible_job_id: '551192544238.49820'
child_pid: 49824
finished: 1
msg: Timeout exceeded
results_file: /root/.ansible_async/551192544238.49820
started: 1
stderr: ''
stderr_lines: <omitted>
stdout: ''
stdout_lines: <omitted>
...ignoring
TASK [debug] **********************************************************************************
ok: [test_11] =>
msg: Play running ...
PLAY RECAP ************************************************************************************
test_11: ok=4 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=1
test_13: ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0