HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Check connectivity of 5000 + servers from ansible and get list of hosts which are connectable and which are not

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
connectableconnectivitycheckhostsserversarewhichget5000and

Problem

I need to check connectivity of hosts on port 43. I can manually login and check connectivity by:

nc -v `hostname` 43
nc -v `hostname_1` 43


Is there any ansible script that goes through all the servers and saves the output of connectable hosts in one file and not connectable in another file?

Solution

Maybe a simple shell script would be more useful. If must to use ansible, it could be something like that (assuming you have all servers in group testall):

- hosts: testall
  connection: local
  gather_facts: false
  tasks:

    - wait_for:
        timeout: 2
        port: 43
        host: '{{ (ansible_ssh_host|default(ansible_host))|default(inventory_hostname) }}'
      register: waitfor
      ignore_errors: true

    - copy:
        content: |
          {% for host in groups['testall'] %}
          {% if hostvars[host].waitfor.failed %}{{ hostvars[host].inventory_hostname }} {% endif %}
          {% endfor %}
        dest: /tmp/bad_file
      delegate_to: localhost

    - copy:
        content: |
          {% for host in groups['testall'] %}
          {% if not hostvars[host].waitfor.failed %}{{ hostvars[host].inventory_hostname }} {% endif %}
          {% endfor %}
        dest: /tmp/good_file
      delegate_to: localhost


hosts that have this port opened will be in /tmp/good_file, others are in /tmp/bad_file

Code Snippets

- hosts: testall
  connection: local
  gather_facts: false
  tasks:

    - wait_for:
        timeout: 2
        port: 43
        host: '{{ (ansible_ssh_host|default(ansible_host))|default(inventory_hostname) }}'
      register: waitfor
      ignore_errors: true

    - copy:
        content: |
          {% for host in groups['testall'] %}
          {% if hostvars[host].waitfor.failed %}{{ hostvars[host].inventory_hostname }} {% endif %}
          {% endfor %}
        dest: /tmp/bad_file
      delegate_to: localhost

    - copy:
        content: |
          {% for host in groups['testall'] %}
          {% if not hostvars[host].waitfor.failed %}{{ hostvars[host].inventory_hostname }} {% endif %}
          {% endfor %}
        dest: /tmp/good_file
      delegate_to: localhost

Context

StackExchange DevOps Q#9606, answer score: 2

Revisions (0)

No revisions yet.