On 08. aug. 2017 20:08, Alyson Whitaker wrote:
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:


---
- hosts: clientservers

   tasks:
   - 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
      ignore_errors: yes


I have researched many forums and sites, and I am currently looking at the
wait_for and register module.  After reading the following advice:


Fine-tuning output

If you want more specific output for the success and failure cases, the
code must be more complex, adding a second task:

    - wait_for task must register a variable
    - the second task produces output using debug based on success/failure
    condition (e.g. using Jinja2 conditional expression
    <https://stackoverflow.com/a/14215034/992887>)
    - then you need to put both these tasks in an include file (without any
    with_items loop), and write a main playbook task that uses an include
    ... with_items to call the include file once per port.



I'm not sure how to put all this together.  Is it even possible?

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 %}
{% endfor %}


--
Kai Stian Olstad

--
You received this message because you are subscribed to the Google Groups "Ansible 
Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to ansible-project+unsubscr...@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/d8e8ddb9-9bcf-32ab-d47d-a6ee689ee7ae%40olstad.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to