How to validate the results for with_items?

33 views
Skip to first unread message

Ramu

unread,
Aug 27, 2018, 2:35:21 PM8/27/18
to ansible...@googlegroups.com
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:  <All the items for which the result is failed>
      

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


Brian Coca

unread,
Aug 27, 2018, 2:43:26 PM8/27/18
to Ansible Project
Just use the jinja2 filters that operate on lists:

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


--
----------
Brian Coca

ttw...@gmail.com

unread,
Aug 27, 2018, 3:25:25 PM8/27/18
to Ansible Project
You probably want a pipe before "list" and after the parens.

--
Todd

Ramu

unread,
Aug 27, 2018, 3:59:32 PM8/27/18
to ansible...@googlegroups.com
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

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/16a3367a-9c21-4175-89da-3c729d500529%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Kai Stian Olstad

unread,
Aug 27, 2018, 4:08:04 PM8/27/18
to ansible...@googlegroups.com
On Monday, 27 August 2018 21.59.10 CEST Ramu wrote:
> 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.

Just add the map filter

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


--
Kai Stian Olstad


Ramu

unread,
Aug 27, 2018, 4:15:53 PM8/27/18
to ansible...@googlegroups.com
Thanks Kai. 

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages