Null Check before loop

Hi Everyone,

How can i do a null check before going into the loop to avoid “obj must be a list of dicts or a nested dict” error

Thanks

I have tried this but not work

  • name: Download packages
    win_get_url:
    url: “{{ item.1 }}”
    dest: C:\temp\{{ item.0.type }}.zip
    loop: “{{ result.targets|subelements(‘url’) }}”
    loop_control:
    index_var: my_idx
    when: “‘targets’ in result”

Hard to say without seeing what is in your result var but you probably need to make use of default filter

something like

“{{ result.targets|default()|subelements(‘url’) }}”

perhaps, but I’m guessing a bit as I don’t know which bit of the results structure is not present.

Hope this helps,

Jon

Hi Jon,

It works now. Thanks a lot.