Send results to localhost (ansible host)

Here is what I am trying to do:

  • Connect to [clientservers] and perform telnet mywebserver.com on port 443, 80, 8443
  • if result = ok (meaning if that port is open), then do nothing.
  • if result = fail, then send output to failed-clients.txt listing hostname, ip and port that was unreachable

I have successfully completed the first task using this playbook:

Something like this:

     - name: Check all port numbers for current host
       wait_for:
         host: mywebserver.com
         port: "{{ item }}"
         state: started # Port should be open
         delay: 0 # No wait before first check (sec)
         timeout: 3 # Stop checking after timeout (sec)
       with_items:
         - 443
         - 80
         - 8443
       register: result
       ignore_errors: yes

     - template:
         src: port.j2
         dest: /tmp/failed-clients.txt
       when: result | failed
       delegate_to: localhost
       run_once: yes

port.j2:
{% for h in ansible_play_batch %}
{% for i in hostvars[h].result.results %}
{% if i.failed is defined %}
{{ h }} failed port {{ i.item }}
{% endif %}
{% endfor %}

Thank you, I will give it a try.
My greatest challenge is getting the yaml syntax correct! I will let you know.