Score:3

How to debug Ansible dict for failed ports with wait_for module?

mk flag

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?

Score:2
br flag

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
Kamal avatar
mk flag
I tried both method suggested in your code and now I am able to get the failed msg printed in debug msg. Thank you.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.