Search code examples
sshansiblepingreachabilityconnectivity

Need code to Check for server connectivity using Ansible


Below is how i invoke my ansible play

ansible-playbook -v -i /web/admin/playbooks/applist.hosts /web/admin/playbooks/restart.yml -e ENV=dev -e ACTION=start --tags checkserver

Below is my playbook:

- hosts: "{{ENV}}_*"
  user: "{{USER}}"
  gather_facts: yes
  pre_tasks:

       - name: Check PING
         ping:
         tags: checkserver


- hosts: "{{ENV}}_*"
  user: "{{USER}}"
  gather_facts: yes
  pre_tasks:

       - name: Check if all hosts are reachable
         fail:
           msg: >
             [REQUIRED] ALL hosts to be reachable, so flagging {{ inventory_hostname }} as failed,
              because host {{ item }} has no facts, meaning it is UNREACHABLE.
         when: hostvars[item].ansible_facts is defined
         with_items: "{{ ansible_play_hosts }}"
         run_once: true
         tags: checkserver

I'm certain there the connectivity is good and the target host is reachable.

Same is evident from the successful ansible ping module output below:

PLAY [dev_*] *******************************************************************

TASK [Gathering Facts] *********************************************************
[1;35m[WARNING]: Platform sunos on host remotehost7.com is using the[0m
[1;35mdiscovered Python interpreter at /usr/bin/python, but future installation of[0m
[1;35manother Python interpreter could change the meaning of that path. See https://d[0m
[1;35mocs.ansible.com/ansible/2.10/reference_appendices/interpreter_discovery.html[0m
[1;35mfor more information.[0m
[0;32mok: [remotehost7.com][0m

PLAY [dev_*] *******************************************************************

TASK [Gathering Facts] *********************************************************
[0;32mok: [remotehost7.com][0m

TASK [Check PING] **************************************************************
[0;32mok: [remotehost7.com] => {"changed": false, "ping": "pong"}[0m

PLAY [dev_*] *******************************************************************

TASK [Gathering Facts] *********************************************************
[0;32mok: [remotehost7.com][0m

TASK [Check if all hosts are reachable] ****************************************
[0;31mfailed: [remotehost7.com] (item=remotehost7.com) => {"ansible_loop_var": "item", "changed": false, "item": "remotehost7.com", "msg": "[REQUIRED] ALL hosts to be reachable, so flagging remotehost7.com as failed,\n because host remotehost7.com has no facts, meaning it is UNREACHABLE.\n"}[0m

NO MORE HOSTS LEFT *************************************************************

PLAY RECAP *********************************************************************
[0;31mremotehost7.com[0m    : [0;32mok=4   [0m changed=0    unreachable=0    [0;31mfailed=1   [0m skipped=0    rescued=0    ignored=0   

I had taken the code from stackoverflow here: Ansible: Abort Execution if a host is unreachable

I'm on ansible version 2.9.x

Note: This code used to work fine on old versions of ansible like 2.4

Can you please suggest?


Solution

  • You're explicitly failing when ansible_facts is defined, which in general will mean that the host is reachable. Maybe you just have your logic backwards? I think you mean:

    - name: Check if all hosts are reachable
      fail:
        msg: >
          [REQUIRED] ALL hosts to be reachable, so flagging {{ inventory_hostname }} as failed,
           because host {{ item }} has no facts, meaning it is UNREACHABLE.
      when: hostvars[item].ansible_facts is not defined
      with_items: "{{ ansible_play_hosts }}"
      run_once: true
      tags: checkserver
    

    But even with the logic fixed, I don't think this gets you anything: you're iterating over ansible_play_hosts, and from the documentation:

    ansible_play_hosts

    List of hosts in the current play run, not limited by the serial. Failed/Unreachable hosts are excluded from this list.

    That is, hosts that were unreachable won't be included in anisble_play_hosts, so your task is checking to see if reachable hosts were reachable, which will always be true.