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

**URL:** <https://forum.ansible.com/t/how-to-fail-some-items-in-a-loop-without-outputing-the-full-item/41488>\
**Category:** Get Help\
**Created:** [April 3, 2025, 4:27am UTC](https://forum.ansible.com/t/how-to-fail-some-items-in-a-loop-without-outputing-the-full-item/41488 "2025-04-03T04:27:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![halloleo](https://avatars.discourse-cdn.com/v4/letter/h/ccd318/32.png) [@halloleo](https://forum.ansible.com/u/halloleo)\
**Post date:** [April 3, 2025, 4:27am UTC](https://forum.ansible.com/t/how-to-fail-some-items-in-a-loop-without-outputing-the-full-item/41488/1 "2025-04-03T04:27:29Z")

</div>

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:

```auto
- 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!

---

<div class="post-metadata">

**Author:** ![kristianheljas](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/kristianheljas/32/7702_2.png) [@kristianheljas](https://forum.ansible.com/u/kristianheljas)\
**Post date:** [April 3, 2025, 8:25am UTC](https://forum.ansible.com/t/how-to-fail-some-items-in-a-loop-without-outputing-the-full-item/41488/2 "2025-04-03T08:25:34Z")

</div>

Try debug module instead:

```auto
- 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:

```auto
  failed_when: true
  ignore_errors: true

```

---

<div class="post-metadata">

**Author:** ![halloleo](https://avatars.discourse-cdn.com/v4/letter/h/ccd318/32.png) [@halloleo](https://forum.ansible.com/u/halloleo)\
**Post date:** [April 4, 2025, 1:25am UTC](https://forum.ansible.com/t/how-to-fail-some-items-in-a-loop-without-outputing-the-full-item/41488/3 "2025-04-04T01:25:56Z")

</div>

> [@kristianheljas](#):
>
> ```auto
> failed_when: true
> 
> ```

Wonderful! This does the trick.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [May 4, 2025, 1:26am UTC](https://forum.ansible.com/t/how-to-fail-some-items-in-a-loop-without-outputing-the-full-item/41488/4 "2025-05-04T01:26:44Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
