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

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

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

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.

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 ?