How to fail some items in a loop without outputing the full item?

I matched a list of patterns individually in a loop to files of a directory resulting in the varaible found_cfgs and now I want to report with “local failure” when for a pattern no match had occurred. I want something like a failure (so that the isue get reported up the chain, but I don’t want to stop execution. The following seems to do the job:

- name: Alert if no file is found to pattern
  ansible.builtin.fail:
    msg: "!!! {{ item.item }}*.cfg - No match founds for pattern!!!"
  loop: "{{ found_cfgs.results | selectattr('matched', 'defined') | selectattr('matched', 'lessthan', 1) | list }}"
  loop_control:
    label: "{{ item.item }}"
  ignore_errors: true

The problem though is a cosmetical one when a failure is repored:

TASK [Alert if no file is found to pattern] ***********************************************
failed: [devel] (item=not-existing) => {“ansible_loop_var”: “item”, “changed”: false, “item”: {“ansible_loop_var”: “item”, “changed”: false, “examined”: 25, “failed”: false, “files”: , “invocation”: {“module_args”: {“age”: null, “age_stamp”: “mtime”, “contains”: null, “depth”: null, “excludes”: null, “file_type”: “file”, “follow”: false, “get_checksum”: false, “hidden”: false, “paths”: [“/…/my-dir”], “patterns”: [“not-existing*.cfg”], “read_whole_file”: false, “recurse”: false, “size”: null, “use_regex”: false}}, “item”: “not-existing”, “matched”: 0, “msg”: “All paths examined”, “skipped_paths”: {}}, “msg”: “!!! not-existing*.cfg - No match founds for extension pattern!!!”}
…ignoring

Now I would like to get rid of the whole details item and just output item.item. loop_control.label does seem to affect only the left side of the output, not the right side. What is the attribute I have to tweak here?

Thanks for any pointers!

Try debug module instead:

- name: Alert if no file is found to pattern
  ansible.builtin.debug:
    msg: "!!! {{ item.item }}*.cfg - No match founds for pattern!!!"
  loop: "{{ found_cfgs.results | selectattr('matched', 'defined') | selectattr('matched', 'lessthan', 1) | list }}"
  loop_control:
    label: "{{ item.item }}"

If you’d like the output to be red as in failed, you can add:

  failed_when: true
  ignore_errors: true
1 Like

Wonderful! This does the trick.