I'm attempting to write information to a file from a registered variable in my task. It's basically a port test and for the life of me I cannot figure out how to only list items that failed.
Here is what I have:
Task:
- name: Check if ports are open
wait_for:
host: "{{ item.name }}"
port: "{{ item.port }}"
state: started
delay: 0
timeout: 5
ignore_errors: true
loop: "{{ server_facts.server_port|default([]) }} + {{ server_port | default([]) }}"
register: server_port_check
Here is the content for the registered variable server_port_check:
server_port_check:
changed: false
failed: true
msg: All items completed
results:
- ansible_loop_var: item
changed: false
elapsed: 0
failed: false
invocation:
module_args:
active_connection_states:
- ESTABLISHED
- FIN_WAIT1
- FIN_WAIT2
- SYN_RECV
- SYN_SENT
- TIME_WAIT
connect_timeout: 5
delay: 0
exclude_hosts: null
host: dc.com
msg: null
path: null
port: 636
search_regex: null
sleep: 1
state: started
timeout: 5
item:
name: dc.com
port: 636
match_groupdict: {}
match_groups: []
path: null
port: 636
search_regex: null
state: started
- ansible_loop_var: item
changed: false
elapsed: 6
failed: true
invocation:
module_args:
active_connection_states:
- ESTABLISHED
- FIN_WAIT1
- FIN_WAIT2
- SYN_RECV
- SYN_SENT
- TIME_WAIT
connect_timeout: 5
delay: 0
exclude_hosts: null
host: salt
msg: null
path: null
port: 4505
search_regex: null
sleep: 1
state: started
timeout: 5
item:
name: salt
port: 4505
msg: Timeout when waiting for salt:4505
Then in my template file, I have the following
Port Checks:
{% if server_port_check.failed == true %}
The following ports are not open:
{% for result in server_port_check.results %}
Host: {{ result.item.name }}
Port: {{ result.item.port }}
{% endfor %}
{% endif %}
{% if server_port_check.failed == false %}
The following ports are open:
{% for result in server_port_check.results %}
Host: {{ result.item.name }}
Port: {{ result.item.port }}
{% endfor %}
{% endif %}
This ends up with the following results:
Port Checks:
The following ports are not open:
Host: dc.com
Port: 636
Host: salt
Port: 4505
Which is incorrect since the port for dc.com is open according to the job output.