gjvignesh
gjvignesh

Reputation: 13

How to set timeout to a task that runs command on remote host in ansible playbook

This is my ansible playbook:

---
- name: Check timeout error
  hosts: all
  ignore_errors: yes
  gather_facts: false

  tasks:
    - name: test if server is connecting within n seconds
      become: True
      shell: id
      register: id_result
      async: 60
      poll: 20

    - name: display id result 
      debug: var=id_result

Question: I need to exit shell task, if it runs beyond n seconds (remote host). In above case it is 10 seconds.

Expected Outcome:

{"msg": "Timeout (62s) waiting for privilege escalation prompt: "}

Actual Outcome:

I get the shell command output, but it takes more than 4 minutes to get the output, since ssh itself to remote host is quite slow.

Doubt: I know async works for localhost command, but is there any similar way check timeout for shell module that runs command on remote host?

Upvotes: 0

Views: 887

Answers (1)

Bálint Szigeti
Bálint Szigeti

Reputation: 311

there is an example in the official docs:

https://docs.ansible.com/ansible/latest/collections/ansible/builtin/shell_module.html

# You can use shell to run other executables to perform actions inline
- name: Run expect to wait for a successful PXE boot via out-of-band CIMC
  ansible.builtin.shell: |
    set timeout 300
    spawn ssh admin@{{ cimc_host }}

    expect "password:"
    send "{{ cimc_password }}\n"

    expect "\n{{ cimc_name }}"
    send "connect host\n"

    expect "pxeboot.n12"
    send "\n"

    exit 0

Upvotes: 1

Related Questions