# finding a specific port on a list of servers

**URL:** <https://forum.ansible.com/t/finding-a-specific-port-on-a-list-of-servers/37015>\
**Category:** Ansible Project\
**Created:** [August 20, 2022, 3:05pm UTC](https://forum.ansible.com/t/finding-a-specific-port-on-a-list-of-servers/37015 "2022-08-20T15:05:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dudu.c](https://avatars.discourse-cdn.com/v4/letter/d/278dde/32.png) [@dudu.c](https://forum.ansible.com/u/dudu.c)\
**Post date:** [August 20, 2022, 3:05pm UTC](https://forum.ansible.com/t/finding-a-specific-port-on-a-list-of-servers/37015/1 "2022-08-20T15:05:47Z")

</div>

HI,  
I have a list of servers that only part of them are running port 9090.  
I need to create two tasks, each should loop the server’s hostnames. the first task should define inside a register the first server hostname that was found running port 9090 , the second task should define in a register all server’s hostnames that are running port 9090. If no server is running port 9090 the tasks should fail

I have nc installed on the server and i thought about using a shell with the command "nc -zv {{ item}} 9090

But I don’t know how to filter the relevant answers

For example:  
server1  
server2

server3 running 9090

server4

server5 running 9090

- The first task should include in a register server 3 **or** server 5 hostname
- The second task should include in a register server 3 **and** server 5 hostname

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [August 20, 2022, 5:45pm UTC](https://forum.ansible.com/t/finding-a-specific-port-on-a-list-of-servers/37015/2 "2022-08-20T17:45:46Z")

</div>

Let’s assume (ha!) you’ve got a shell task that loops over a list of servers, and for any server with port 9090 open it prints “{{item}} running 9090” to its stdout. And that you register that to “fake” to match the code below.

```auto
---
- name: Working with registered data
  hosts: localhost
  gather_facts: no
  vars:
    fake:
      results:
        - item: server1
          stdout: ""
        - item: server2
          stdout: ""
        - item: server3
          stdout: "server3 running 9090\n"
        - item: server4
          stdout: ""
        - item: server5
          stdout: "server5 running 9090\n"
  tasks:
    - name: task1 pick the first server running 9090
      debug:
        msg: "{{ fake.results | selectattr('stdout', 'search', 'running 9090') | first }}"

    - name: task2 pick all servers running 9090
      debug:
        msg: "{{ fake.results | selectattr('stdout', 'search', 'running 9090') | map(attribute='item') }}"

```

That produces the following output:

```auto
TASK [task1 pick the first server running 9090] ********************************
task path: /home/utoddl/ansible/register-games.yml:19
ok: [localhost] => {
    "msg": "server3"
}

TASK [task2 pick all servers running 9090] *************************************
task path: /home/utoddl/ansible/register-games.yml:25
ok: [localhost] => {
    "msg": [
        "server3",
        "server5"
    ]
}

```

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [August 20, 2022, 5:56pm UTC](https://forum.ansible.com/t/finding-a-specific-port-on-a-list-of-servers/37015/3 "2022-08-20T17:56:31Z")

</div>

…except that first task should be

- name: task1 pick the first server running 9090  
debug:  
msg: “{{ item.item }}”  
vars:  
item: “{{ fake.results | selectattr(‘stdout’, ‘search’, ‘running 9090’) | first }}”

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [August 20, 2022, 5:58pm UTC](https://forum.ansible.com/t/finding-a-specific-port-on-a-list-of-servers/37015/4 "2022-08-20T17:58:34Z")

</div>

… or

- name: task1 pick the first server running 9090  
debug:  
msg: “{{ (fake.results | selectattr(‘stdout’, ‘search’, ‘running 9090’) | first).item }}”

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [August 22, 2022, 11:19am UTC](https://forum.ansible.com/t/finding-a-specific-port-on-a-list-of-servers/37015/5 "2022-08-22T11:19:27Z")

</div>

The wait\_for module can test for a given port.

- name: Check if port 9090 is listening (times out in 3 secs per host)  
wait\_for:  
port: 9090  
timeout: 3  
msg: “Timeout waiting for 9090 to respond”  
register: port\_check  
ignore\_errors: yes

Then you can look in port\_check to see whether it succeeded.

Walter
