3

I have written the below playbook to fetch the failed port status.

 - name: Check port status wait_for: host: 127.0.0.1 port: "{{ item }}" timeout: 2 state: started ignore_errors: yes with_items: - 80 - 22 - 8080 register: wait_result - debug: msg: "{{ 'Failed with message: ' ~ wait_result.msg if wait_result.failed else 'Success' }}" 

While fetching the msg from debug logs I am unable to get the timeout message.

PLAY [mysrv] ************************************************************************************************************************************ TASK [Gathering Facts] ************************************************************************************************************************** ok: [localhost] TASK [Check Nagios port status] ***************************************************************************************************************** ok: [localhost] => (item=80) ok: [localhost] => (item=22) failed: [localhost] (item=8080) => {"ansible_loop_var": "item", "changed": false, "elapsed": 2, "item": 8080, "msg": "Timeout when waiting for 127.0.0.1:8080"} ...ignoring TASK [debug] ************************************************************************************************************************************ ok: [localhost] => { "msg": "Failed with message: One or more items failed" } PLAY RECAP ************************************************************************************************************************************** localhost : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=1 

Can someone please help to correct my code?

1 Answer 1

2

For example, you can write the messages

 - debug: msg: | {% for i in wait_result.results|select('failed') %} {{ i.msg }} {% endfor %} 

, or create a list and use it later

 failed_msg: "{{ wait_result.results| select('failed')| map(attribute='msg')|list }}" 

Example of a complete playbook for testing

- hosts: localhost vars: failed_msg: "{{ wait_result.results| select('failed')| map(attribute='msg')|list }}" tasks: - name: Check port status wait_for: host: 127.0.0.1 port: "{{ item }}" timeout: 2 state: started register: wait_result ignore_errors: yes loop: [22, 25, 53, 80] - debug: var: wait_result when: debug|d(false)|bool - debug: var: failed_msg - debug: msg: | {% for i in wait_result.results|select('failed') %} {{ i.msg }} {% endfor %} - assert: that: wait_result.results|select('failed')|length == 0 fail_msg: | {% for i in wait_result.results|select('failed') %} {{ i.msg }} {% endfor %} success_msg: All services started. 

gives

PLAY [localhost] ***************************************************************************** TASK [Check port status] ******************************************************************** ok: [localhost] => (item=22) ok: [localhost] => (item=25) failed: [localhost] (item=53) => changed=false ansible_loop_var: item elapsed: 2 item: 53 msg: Timeout when waiting for 127.0.0.1:53 failed: [localhost] (item=80) => changed=false ansible_loop_var: item elapsed: 2 item: 80 msg: Timeout when waiting for 127.0.0.1:80 ...ignoring TASK [debug] ********************************************************************************* skipping: [localhost] TASK [debug] ********************************************************************************* ok: [localhost] => failed_msg: - Timeout when waiting for 127.0.0.1:53 - Timeout when waiting for 127.0.0.1:80 TASK [debug] ********************************************************************************* ok: [localhost] => msg: |- Timeout when waiting for 127.0.0.1:53 Timeout when waiting for 127.0.0.1:80 TASK [assert] ******************************************************************************** fatal: [localhost]: FAILED! => changed=false assertion: wait_result.results|select('failed')|length == 0 evaluated_to: false msg: |- Timeout when waiting for 127.0.0.1:53 Timeout when waiting for 127.0.0.1:80 PLAY RECAP *********************************************************************************** localhost: ok=2 changed=0 unreachable=0 failed=1 skipped=1 rescued=0 ignored=1 
1
  • I tried both method suggested in your code and now I am able to get the failed msg printed in debug msg. Thank you. Commented Feb 1, 2023 at 18:10

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.