# Grep value from a dict of list of dict ...

**URL:** <https://forum.ansible.com/t/grep-value-from-a-dict-of-list-of-dict/25329>\
**Category:** Ansible Project\
**Created:** [August 25, 2017, 12:42pm UTC](https://forum.ansible.com/t/grep-value-from-a-dict-of-list-of-dict/25329 "2017-08-25T12:42:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![lovi](https://avatars.discourse-cdn.com/v4/letter/l/ecc23a/32.png) [@lovi](https://forum.ansible.com/u/lovi)\
**Post date:** [August 25, 2017, 12:42pm UTC](https://forum.ansible.com/t/grep-value-from-a-dict-of-list-of-dict/25329/1 "2017-08-25T12:42:25Z")

</div>

Hello,

I am struggling with this kind of structure :

{  
“out.results”: [  
{  
“\_ansible\_item\_result”: true,  
“\_ansible\_no\_log”: false,  
“\_ansible\_parsed”: true,  
“changed”: false,  
“exitcode”: “NoChangeNeeded”,  
“feature\_result”: ,  
“item”: “A”,  
“restart\_needed”: false,  
“success”: true  
},  
{  
“\_ansible\_item\_result”: true,  
“\_ansible\_no\_log”: false,  
“\_ansible\_parsed”: true,  
“changed”: false,  
“exitcode”: “NoChangeNeeded”,  
“feature\_result”: ,  
“item”: “B”,  
“restart\_needed”: false,  
“success”: true  
},  
{  
“\_ansible\_item\_result”: true,  
“\_ansible\_no\_log”: false,  
“\_ansible\_parsed”: true,  
“changed”: false,  
“exitcode”: “NoChangeNeeded”,  
“feature\_result”: ,  
“item”: “C”,  
“restart\_needed”: false,  
“success”: true  
},  
{  
“\_ansible\_item\_result”: true,  
“\_ansible\_no\_log”: false,  
“\_ansible\_parsed”: true,  
“changed”: false,  
“exitcode”: “NoChangeNeeded”,  
“feature\_result”: ,  
“item”: “D”,  
“restart\_needed”: true,  
“success”: true  
}  
]  
}

And I want to catch if restart\_needed is true … any help is welcome.

Regards

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [August 25, 2017, 2:40pm UTC](https://forum.ansible.com/t/grep-value-from-a-dict-of-list-of-dict/25329/2 "2017-08-25T14:40:37Z")

</div>

What you need to use here is selectattr and equalto from jinja2:

results\>selectattr(‘restart\_needed’, ‘equalto’, true)|list|length

If my memory is correct, `equalto` was not added until jinja2 v2.8

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [August 25, 2017, 2:41pm UTC](https://forum.ansible.com/t/grep-value-from-a-dict-of-list-of-dict/25329/3 "2017-08-25T14:41:37Z")

</div>

Actually that can be simplified, I completely forgot that the default for selectattr is to check if the value is true.

So:

results\>selectattr(‘restart\_needed’)|list|length

That doesn’t even require the `equalto` test.

---

<div class="post-metadata">

**Author:** ![lovi](https://avatars.discourse-cdn.com/v4/letter/l/ecc23a/32.png) [@lovi](https://forum.ansible.com/u/lovi)\
**Post date:** [August 30, 2017, 2:38pm UTC](https://forum.ansible.com/t/grep-value-from-a-dict-of-list-of-dict/25329/4 "2017-08-30T14:38:15Z")

</div>

thanks it works fine !!

PS : Do you know how to put jinja code in task/role/play ? or do you have to put it in default.yml files ?
