Find matched element in key value

Hi All,

Our required in-built module gives huge output of different keys that have multiple values with objects. I used ‘with_dict’ and when to narrow and get desired output as below. Here also am facing a road block that how can I get the element where any value doesn’t meet. How can get output of the block when “status”: “CRITICAL” instead of all.

Output:
“msg”: {
“key”: “devices”,
“value”: [
{
“configuration”: “Configured”,
“version”: “PD-0.7”,
“label”: “device-1”,
“location”: “1”,
“serial_number”: “PWH12GVF”,
“status”: “OK”
},
{
“configuration”: “Configured”,
“version”: “PD-0.7”,
“label”: “device-2”,
“location”: “2”,
“serial_number”: “PWH12GWF”,
“status”: “OK”
},
{
“configuration”: “Configured”,
“version”: “PD-0.7”,
“label”: “device-3”,
“location”: “3”,
“serial_number”: “PWH12GXF”,
“status”: “CRITICAL”
}
]
}

Desired output:
“configuration”: “Configured”,
version": “PD-0.7”,
“label”: “device-3”,
“location”: “3”,
“serial_number”: “PWH12GXF”,
“status”: “CRITICAL”

You can use the Jinja selectattr

{{ msg.value | selectattr('status', 'equalto', 'CRITICAL') | list }}

Hi,

Thank you for your reply. I added another ‘debug’, var with given jinja variable. But unfortunately, it gives error with below. Can you please assist.

fatal: [localhost]: FAILED! => {“msg”: “The task includes an option with an undefined variable. The error was: ‘msg’ is undefined\n\nThe error appears to have been in ‘/root/devices…yml’: line 20, column 6, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - debug:\n ^ here\n”}

Without the code there is nothing I can do to assist.

Hi,

The playbook is as follows and highlighted text is added.

You don't have any variable in here called msg, so when you use msg in the debug task it will fail with undefined variable.
In you first mail you did have an output that was called msg.

Hi,

With this playbook we still getting msg output with first debug that i mentioned in my mail. It fails where second debug defined with ‘selectattr’

OK, then I understand where the output are from.

Your with_dict debug task make things more confusing, it would be better to get the output of this task

- debug:
    var: hw_health.storage['Controller on System Board']['logical_drives']

Hi,

Edited the PB and it is

This makes it easier to understand structure of the variable, you should get the critical ones with this

{{ hw_health.storage['Controller on System Board']['logical_drives'][0]['physical_drives'] | selectattr('status', 'equalto', 'CRITICAL') | list }}

Hi,

Enclosed the var and made PB as below and resulted FAILED output.

PB:

var: takes a variable not the content, change your var: to msg:

ah, again the same error with msg rather than var
TASK [debug] **************************************************************************************************************************************
fatal: [localhost]: FAILED! => {“msg”: “Unexpected failure during module execution.”}

Then you have some other typos in you debug task somewhere.

Hi,

Thank you for your assistance. In this case selectattr match works that as I expected.

Hi,

I am facing another issue. In the first scenario we used {{hw_health.storage[‘Controller on System Board’][‘logical_drives’][0][‘physical_drives’] | selectattr(‘status’, ‘match’, ‘Failed’) | list}} where only one physical_drives. Some servers having two “physical_drives” sections. The status: Failed may happen either sections. Can you please help me here how can we get the Failed list from either.

Output :
“storage”: {
“Controller on System Board”: {
“logical_drives”: [
{
“physical_drives”: [
{
“configuration”: “Configured”,
“version”: “PD-0.7”,
“label”: “device-1”,
“location”: “1”,
“serial_number”: “PWH12GVF”,
“status”: “OK”
},
{
“configuration”: “Configured”,
“version”: “PD-0.7”,
“label”: “device-2”,
“location”: “2”,
“serial_number”: “PWH12GWF”,
“status”: “OK”
}
],
},
{
“physical_drives”: [
{
“configuration”: “Configured”,
“version”: “PD-0.7”,
“label”: “device-3”,
“location”: “3”,
“serial_number”: “PWH12GVA”,
“status”: “OK”
},
{
“configuration”: “Configured”,
“version”: “PD-0.7”,
“label”: “device-4”,
“location”: “4”,
“serial_number”: “PWH12GWB”,
“status”: “OK”
}
],
}
],
}
}

It's not pretty and it's probably a better way to this, but this should work

"{{ hw_health.storage['Controller on System Board'].logical_drives | map(attribute='physical_drives') | list | flatten | selectattr('status', 'match', 'OK') | list }}"