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

19 views
Skip to first unread message

lovi

unread,
Aug 25, 2017, 8:42:25 AM8/25/17
to Ansible Project
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

Matt Martz

unread,
Aug 25, 2017, 10:42:31 AM8/25/17
to ansible...@googlegroups.com
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


--
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-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/3d322346-3ab2-4f63-89eb-fc8be3de96f2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Matt Martz
@sivel
sivel.net

Matt Martz

unread,
Aug 25, 2017, 10:42:49 AM8/25/17
to ansible...@googlegroups.com
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.

lovi

unread,
Aug 30, 2017, 10:38:15 AM8/30/17
to Ansible Project
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 ?

 
Reply all
Reply to author
Forward
0 new messages