Answers: <p>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 <code>testall</code>):</p>
  
  <pre><code>- 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
  
  </code></pre>
  
  <p>hosts that have this port opened will be in <code>/tmp/good_file</code>, others are in <code>/tmp/bad_file</code></p>
  
Answers: CC BY-SA 4.0
Answers: 2020-03-19T22:36:23.363Z
Answers: thank you it works
Answers: user12189187
Answers: CC BY-SA 4.0
Answers: 2020-03-31T11:17:00.323Z
Answers: @user12189187 glad it helped, can you please mark it as a solution in this case?
Answers: DominiCane
Answers: CC BY-SA 4.0
Answers: 2019-11-01T21:37:48.380Z
Answers: 2019-11-01T21:37:48.380Z
Answers: DominiCane
Body: <p>I need to check connectivity of hosts on port 43. I can manually login and check connectivity by:</p>
  
  <pre><code>nc -v `hostname` 43
  nc -v `hostname_1` 43
  </code></pre>
  
  <p>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?</p>
  
Comments: CC BY-SA 4.0
Comments: 2019-10-29T14:57:11.943Z
Comments: if you add all the hosts in the inventory, you can run the tasks against all of them, catch the ones which return an error and put them into the "bad" group for later.
Comments: Bruce Becker
Comments: CC BY-SA 4.0
Comments: 2019-10-29T14:59:01.460Z
Comments: yeah i added all of them in inventory, but had trouble writing ansible script and filter them out. using wait_for module
Comments: user12189187
Tags: <ansible><automation><networking>
Title: Check connectivity of 5000 + servers from ansible and get list of hosts which are connectable and which are not
UserDisplayName: user12189187
ThreadText: # Check connectivity of 5000 + servers from ansible and get list of hosts which are connectable and which are not
  
  (Asked by: username_0 on 2019-10-29)
  
  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?
  
  **Tags:** ansible, automation, networking
  
  > **Comments:**
  > **username_1**: if you add all the hosts in the inventory, you can run the tasks against all of them, catch the ones which return an error and put them into the "bad" group for later.  
  > **username_0**: yeah i added all of them in inventory, but had trouble writing ansible script and filter them out. using wait_for module  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2019-11-01. Score: 2
  
  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`
  
  > **Comments:**
  > **username_0**: thank you it works  
  > **username_2**: @username_0 glad it helped, can you please mark it as a solution in this case?  
  
  ---
  
  