4

I am trying to print a custom message, when certain host is unreachable. My problem is, when the host is unreachable, it will be skipped on the next task, thus the fail module will never be triggered. And the host that is reachable, will also skip the task due to the when condition are not met.

I have tried ignore_unreachable: false, still the same. Any idea would be appreciated.

---
- hosts: prod
  gather_facts: no
  tasks:
  - name: fail when not reachable
    action: ping
    register: ping_result
#    any_errors_fatal: true

  - fail:
      msg: "please make sure all server up and run again"
    when: ping_result.unreachable is defined
    any_errors_fatal: true
β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
sloweriang
  • 308
  • 4
  • 19

1 Answers1

5

You are looking for the keyword ignore_unreachable, that should be set to true, because you do want to ignore the unreachable host in order to go forward to your fail task.

Given the playbook:

- hosts: nodes
  gather_facts: no

  tasks:
    - ping:
      register: ping_result
      ignore_unreachable: true

    - fail:
        msg: "please make sure all server up and run again"
      when: ping_result.unreachable is defined
      any_errors_fatal: true

This yields:

TASK [ping] *******************************************************
fatal: [node1]: UNREACHABLE! => changed=false 
  msg: 'Failed to connect to the host via ssh'
  skip_reason: Host node1 is unreachable
  unreachable: true
fatal: [node2]: UNREACHABLE! => changed=false 
  msg: 'Failed to connect to the host via ssh'
  skip_reason: Host node2 is unreachable
  unreachable: true
fatal: [node3]: UNREACHABLE! => changed=false 
  msg: 'Failed to connect to the host via ssh'
  skip_reason: Host node3 is unreachable
  unreachable: true

TASK [fail] *******************************************************
fatal: [node1]: FAILED! => changed=false 
  msg: please make sure all server up and run again
fatal: [node2]: FAILED! => changed=false 
  msg: please make sure all server up and run again
fatal: [node3]: FAILED! => changed=false 
  msg: please make sure all server up and run again

NO MORE HOSTS LEFT ************************************************  
β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83