ignore_unreachable statement does not work with block/rescue?

101 views
Skip to first unread message

Peter Gebirgslok

unread,
Aug 11, 2020, 11:38:32 AM8/11/20
to Ansible Project
Hello guys,
I'm trying to check if the server is still reachable with a block -rescue statement.
If the server is no longer available, the rescue statement should do something.

This is basically the simple playbook:

---
- name: Verify if host is up
  hosts: Server2
  gather_facts: no
#  ignore_unreachable: true
  become: true

  tasks:
    - block:
      - name: Ping the Server
        ping:
        ignore_unreachable: true
      rescue:
      - debug:
          msg: "The Ping was not successful"
        ...

The problem is that Playbook aborts directly if the server is not available and does not execute the rescue statement.
I tried the ignore_unreachable statement on all possible positions. I have also added the statement ignore_errors.

If im doing it without block / rescue, it is working without any problems.

So the Question: Can the keyword "ignore_unreachable" not be used in a block - rescue statement?

Hrvoje Gašpar

unread,
Aug 12, 2020, 2:46:56 AM8/12/20
to Ansible Project
wait_for_connection might be more suitable for reachability checks in your case:
https://docs.ansible.com/ansible/latest/modules/wait_for_connection_module.html

---
- name: Verify if host is up
  hosts: Server2
  gather_facts: no
  become: true

  tasks:
    - block:
      - name: Ping the Server
        wait_for_connection:
          timeout: 10

      rescue:
      - debug:
          msg: "The Ping was not successful"

It allows you to set the timeout, and to retry before triggering rescue. And it's designed to allow actions to be taken in case of faliure.

pan...@hotmail.de

unread,
Aug 12, 2020, 7:01:27 AM8/12/20
to Ansible Project
Thank you, that works.

I appreciate the help .
Reply all
Reply to author
Forward
0 new messages