# Find matched element in key value

**URL:** <https://forum.ansible.com/t/find-matched-element-in-key-value/29383>\
**Category:** Ansible Project\
**Created:** [January 7, 2019, 4:21pm UTC](https://forum.ansible.com/t/find-matched-element-in-key-value/29383 "2019-01-07T16:21:41Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rama](https://avatars.discourse-cdn.com/v4/letter/r/ccd318/32.png) [@Rama](https://forum.ansible.com/u/Rama)\
**Post date:** [January 7, 2019, 4:21pm UTC](https://forum.ansible.com/t/find-matched-element-in-key-value/29383/1 "2019-01-07T16:21:41Z")

</div>

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”

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [January 7, 2019, 5:15pm UTC](https://forum.ansible.com/t/find-matched-element-in-key-value/29383/2 "2019-01-07T17:15:51Z")

</div>

You can use the Jinja selectattr

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

---

<div class="post-metadata">

**Author:** ![Rama](https://avatars.discourse-cdn.com/v4/letter/r/ccd318/32.png) [@Rama](https://forum.ansible.com/u/Rama)\
**Post date:** [January 8, 2019, 1:31pm UTC](https://forum.ansible.com/t/find-matched-element-in-key-value/29383/3 "2019-01-08T13:31:23Z")

</div>

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”}

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [January 8, 2019, 4:09pm UTC](https://forum.ansible.com/t/find-matched-element-in-key-value/29383/4 "2019-01-08T16:09:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Rama](https://avatars.discourse-cdn.com/v4/letter/r/ccd318/32.png) [@Rama](https://forum.ansible.com/u/Rama)\
**Post date:** [January 8, 2019, 5:26pm UTC](https://forum.ansible.com/t/find-matched-element-in-key-value/29383/5 "2019-01-08T17:26:54Z")

</div>

Hi,

The playbook is as follows and highlighted text is added.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [January 8, 2019, 8:20pm UTC](https://forum.ansible.com/t/find-matched-element-in-key-value/29383/6 "2019-01-08T20:20:02Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Rama](https://avatars.discourse-cdn.com/v4/letter/r/ccd318/32.png) [@Rama](https://forum.ansible.com/u/Rama)\
**Post date:** [January 8, 2019, 8:35pm UTC](https://forum.ansible.com/t/find-matched-element-in-key-value/29383/7 "2019-01-08T20:35:54Z")

</div>

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’

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [January 8, 2019, 8:57pm UTC](https://forum.ansible.com/t/find-matched-element-in-key-value/29383/8 "2019-01-08T20:57:37Z")

</div>

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:  
&nbsp;&nbsp;&nbsp;&nbsp;var: hw\_health.storage['Controller on System Board']['logical\_drives']

---

<div class="post-metadata">

**Author:** ![Rama](https://avatars.discourse-cdn.com/v4/letter/r/ccd318/32.png) [@Rama](https://forum.ansible.com/u/Rama)\
**Post date:** [January 8, 2019, 9:21pm UTC](https://forum.ansible.com/t/find-matched-element-in-key-value/29383/9 "2019-01-08T21:21:44Z")

</div>

Hi,

Edited the PB and it is

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [January 8, 2019, 10:26pm UTC](https://forum.ansible.com/t/find-matched-element-in-key-value/29383/10 "2019-01-08T22:26:33Z")

</div>

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 }}

---

<div class="post-metadata">

**Author:** ![Rama](https://avatars.discourse-cdn.com/v4/letter/r/ccd318/32.png) [@Rama](https://forum.ansible.com/u/Rama)\
**Post date:** [January 8, 2019, 10:48pm UTC](https://forum.ansible.com/t/find-matched-element-in-key-value/29383/11 "2019-01-08T22:48:16Z")

</div>

Hi,

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

PB:

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [January 9, 2019, 3:55pm UTC](https://forum.ansible.com/t/find-matched-element-in-key-value/29383/12 "2019-01-09T15:55:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Rama](https://avatars.discourse-cdn.com/v4/letter/r/ccd318/32.png) [@Rama](https://forum.ansible.com/u/Rama)\
**Post date:** [January 9, 2019, 5:10pm UTC](https://forum.ansible.com/t/find-matched-element-in-key-value/29383/13 "2019-01-09T17:10:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [January 9, 2019, 5:31pm UTC](https://forum.ansible.com/t/find-matched-element-in-key-value/29383/14 "2019-01-09T17:31:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Rama](https://avatars.discourse-cdn.com/v4/letter/r/ccd318/32.png) [@Rama](https://forum.ansible.com/u/Rama)\
**Post date:** [January 19, 2019, 9:36am UTC](https://forum.ansible.com/t/find-matched-element-in-key-value/29383/15 "2019-01-19T09:36:36Z")

</div>

Hi,

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

---

<div class="post-metadata">

**Author:** ![Rama](https://avatars.discourse-cdn.com/v4/letter/r/ccd318/32.png) [@Rama](https://forum.ansible.com/u/Rama)\
**Post date:** [January 21, 2019, 4:38pm UTC](https://forum.ansible.com/t/find-matched-element-in-key-value/29383/16 "2019-01-21T16:38:10Z")

</div>

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”  
}  
],  
}  
],  
}  
}

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [January 22, 2019, 8:24pm UTC](https://forum.ansible.com/t/find-matched-element-in-key-value/29383/17 "2019-01-22T20:24:43Z")

</div>

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 }}"
