<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>

CC BY-SA 4.0
2020-03-19T22:36:23.363Z
thank you it works
user12189187
CC BY-SA 4.0
2020-03-31T11:17:00.323Z
@user12189187 glad it helped, can you please mark it as a solution in this case?
DominiCane
CC BY-SA 4.0
2019-11-01T21:37:48.380Z
2019-11-01T21:37:48.380Z
DominiCane
<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>

CC BY-SA 4.0
2019-10-29T14:57:11.943Z
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.
Bruce Becker
CC BY-SA 4.0
2019-10-29T14:59:01.460Z
yeah i added all of them in inventory, but had trouble writing ansible script and filter them out. using wait_for module
user12189187
<ansible><automation><networking>
Check connectivity of 5000 + servers from ansible and get list of hosts which are connectable and which are not
user12189187
# 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?  

---

