0

The below task is waiting for long time, I would like to timeout after 5 minutes.

- name: Get disk size from server1
  shell: time df -h
  delegate_to: "server-1"
  ignore_errors: true

Tried with wait_for and timeout. Both are not working.

Can you please suggest hwo to handle a timeout with a delegate_to task?

Like to timeout this task after 5 minutes but a timeout attribute are not supported in delegate_to.

Please let me know alternate way to timeout.

U880D
  • 8,601
  • 6
  • 24
  • 40
Subbiah
  • 11
  • 1
  • The question as it is written currently looks like a [XY Problem](https://meta.stackexchange.com/questions/66377/) for me since it seems you are trying to gather facts about a remote system. – U880D Jul 19 '23 at 05:22

3 Answers3

0

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
Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63
0

In order to get the disk size from a Remote Node it is recommend to gather_facts. See in example the minimal playbook

---
- hosts: test
  become: false
  gather_facts: false

  tasks:

  - name: Gather date and time only
    setup:
      gather_subset:
        - 'hardware'
        - '!min'

  - name: Show Gathered Facts
    debug:
      msg: "{{ ansible_facts }}"

Since the information about the Remote Node will be gathered from the Control Node there is also no need to push a job to a Remote Node and initiating a connection back from there, unless one like to add unnecessary complexity.

Delegating tasks is to

... perform a task on one host with reference to other hosts ... ideal for managing nodes in a load balanced pool or for controlling outage windows.

Similar Q&A

U880D
  • 8,601
  • 6
  • 24
  • 40
-1

Provide the timeout as part of shell command as listed below:

- name: Get disk size from server1
  shell: timeout 30s time df -h
  delegate_to: "server-1"
  ignore_errors: true
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
Subbiah
  • 11
  • 1