# How to validate the results for with\_items?

**URL:** <https://forum.ansible.com/t/how-to-validate-the-results-for-with-items/28429>\
**Category:** Ansible Project\
**Created:** [August 27, 2018, 6:34pm UTC](https://forum.ansible.com/t/how-to-validate-the-results-for-with-items/28429 "2018-08-27T18:34:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ramu](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Ramu](https://forum.ansible.com/u/Ramu)\
**Post date:** [August 27, 2018, 6:34pm UTC](https://forum.ansible.com/t/how-to-validate-the-results-for-with-items/28429/1 "2018-08-27T18:34:56Z")

</div>

Hi All,  
I want to implement a logic for below requirement

block:

- name: A task  
shell: grep '{{ item }} is present ’ /in/a/file  
register: output  
with\_items: “{{ host\_group }}”

always:

- set\_fact:  
failed\_items: 

However, the output is a list. And each item in the list would contain rc, whose value could be true or false based on the item in the group.

Is there a way to find the items for which the task is failed and construct a final result string.

Thanks in advance,  
Ramu

---

<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:** [August 27, 2018, 6:43pm UTC](https://forum.ansible.com/t/how-to-validate-the-results-for-with-items/28429/2 "2018-08-27T18:43:00Z")

</div>

Just use the jinja2 filters that operate on lists:

failed\_items: '{{ output.results|selectattr('failed', 'equalto', True)list }}'

---

<div class="post-metadata">

**Author:** ![ttwalto](https://avatars.discourse-cdn.com/v4/letter/t/ecb155/32.png) [@ttwalto](https://forum.ansible.com/u/ttwalto)\
**Post date:** [August 27, 2018, 7:25pm UTC](https://forum.ansible.com/t/how-to-validate-the-results-for-with-items/28429/3 "2018-08-27T19:25:25Z")

</div>

You probably want a pipe before “list” and after the parens.

---

<div class="post-metadata">

**Author:** ![Ramu](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Ramu](https://forum.ansible.com/u/Ramu)\
**Post date:** [August 27, 2018, 7:59pm UTC](https://forum.ansible.com/t/how-to-validate-the-results-for-with-items/28429/4 "2018-08-27T19:59:10Z")

</div>

Thanks Brian and Todd.  
But the list will still have all other attributes. I just want to know the items for which the test is failed.

Thanks and regards,  
Ramu

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [August 27, 2018, 8:07pm UTC](https://forum.ansible.com/t/how-to-validate-the-results-for-with-items/28429/5 "2018-08-27T20:07:47Z")

</div>

Just add the map filter

{{ output.results|selectattr('failed', 'equalto', True) | map(attribute='item') | list }}

---

<div class="post-metadata">

**Author:** ![Ramu](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Ramu](https://forum.ansible.com/u/Ramu)\
**Post date:** [August 27, 2018, 8:15pm UTC](https://forum.ansible.com/t/how-to-validate-the-results-for-with-items/28429/6 "2018-08-27T20:15:33Z")

</div>

Thanks Kai.
