title: ansible task - Locate running port on a list of servers
body: <p>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</p>
  <p>I have nc installed on the server and I thought about using a shell with the following task:</p>
  <blockquote>
  <pre><code>server_details:
    - server1
    - server2
    - server3
    - server4
    - server5
  
  - name: locate servers running port 9090 
    shell: nc -zv {{ item.hostname }} 9090
    register: results_port_9090
    with_items:
      - &quot;{{ server_details }}&quot;
    ignore_errors: yes
  </code></pre>
  </blockquote>
  <p>But I don't know how to filter the answer and add the hostname to a new register - I assume an additional task is required that runs on the existing register and creates a new register with the relevant outputs</p>
  <pre><code>For example:
  server1
  server2
  server3 #running 9090
  server4
  server5 #running 9090
  </code></pre>
  <ul>
  <li>The first task should create a register that holds server 3 <strong>or</strong> server 5 hostname</li>
  <li>The second task should create a register that holds server 3 <strong>and</strong> server 5 hostname</li>
  </ul>