How to validate the results for with_items?

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

Just use the jinja2 filters that operate on lists:

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

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

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

Just add the map filter

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

Thanks Kai.