# Selecting attributes from a registered variable

**URL:** <https://forum.ansible.com/t/selecting-attributes-from-a-registered-variable/34374>\
**Category:** Ansible Project\
**Created:** [December 28, 2020, 4:43pm UTC](https://forum.ansible.com/t/selecting-attributes-from-a-registered-variable/34374 "2020-12-28T16:43:35Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![harry\_devine](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/harry_devine/32/5293_2.png) [@harry\_devine](https://forum.ansible.com/u/harry_devine)\
**Post date:** [December 28, 2020, 4:43pm UTC](https://forum.ansible.com/t/selecting-attributes-from-a-registered-variable/34374/1 "2020-12-28T16:43:35Z")

</div>

I’m trying to use “gluster\_heal\_info” to retrieve the “no\_of\_entries” attribute. When I print out the values that gluster\_heal\_info returns, I see that this attribute exists:

storage1 | SUCCESS =\> {  
“msg”: {  
“brick”: " storage1-storage:/gluster\_bricks/brick3/1",  
“no\_of\_entries”: “0”,  
“status”: “Connected”  
}  
}

If I try to retrieve that attribute as follows:

- name: Find out the healing status of /home  
gluster\_heal\_info:  
name: home  
status\_filter: self-heal  
register: home\_heal\_status

until: home\_heal\_status.glusterfs.heal\_info | selectattr(‘no\_of\_entries’,‘ne’,‘0’) | list | count == 0

I get the error: The task includes an option with an undefined variable. The error was: ‘dict object’ has no attribute ‘no\_of\_entries’.

This worked until a few weeks ago, so I’m wondering if either: a) something changed in the module in the Ansible version we’re using (2.9.16), or b) I was not using the syntax correctly and there was a false positive.

So how do I use selectattr to retrieve the ‘no\_of\_entries’ attribute?

Thanks,  
Harry

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [December 28, 2020, 5:18pm UTC](https://forum.ansible.com/t/selecting-attributes-from-a-registered-variable/34374/2 "2020-12-28T17:18:20Z")

</div>

What a module returns is not entirely the same as the variable value when using “register”.

Can you register the result and show what that values is (using debug task)?

---

<div class="post-metadata">

**Author:** ![harry\_devine](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/harry_devine/32/5293_2.png) [@harry\_devine](https://forum.ansible.com/u/harry_devine)\
**Post date:** [December 28, 2020, 6:08pm UTC](https://forum.ansible.com/t/selecting-attributes-from-a-registered-variable/34374/3 "2020-12-28T18:08:25Z")

</div>

I register the variable from the “gluster\_heal\_info” module and here’s the printout of it. I can shorten it if this is too long or too much to look at. Basically, I’m trying to gather the “no\_of\_entries” attribute from each gluster brick and sum them together and make sure that it equals zero. That means that no healing is going on, so I can then shut down the glusterd service and to a “yum update”.

Thanks,  
Harry

storage1 | SUCCESS =\> {  
“msg”: {  
“ansible\_facts”: {  
“discovered\_interpreter\_python”: “/usr/bin/python”  
},  
“changed”: false,  
“failed”: false,  
“glusterfs”: {  
“heal\_info”: [  
{  
“brick”: " storage1-storage:/gluster\_bricks/brick3/1",  
“no\_of\_entries”: “0”,  
“status”: “Connected”  
},  
{  
“brick”: " storage2-storage:/gluster\_bricks/brick4/1",  
“no\_of\_entries”: “0”,  
“status”: “Connected”  
},  
{  
“brick”: " storage3-storage:/gluster\_bricks/arbiter2/1",  
“no\_of\_entries”: “0”,  
“status”: “Connected”  
},  
{  
“brick”: " storage2-storage:/gluster\_bricks/brick3/1",  
“no\_of\_entries”: “0”,  
“status”: “Connected”  
},  
{  
“brick”: " storage3-storage:/gluster\_bricks/brick4/1",  
“no\_of\_entries”: “0”,  
“status”: “Connected”  
},  
{  
“brick”: " storage4-storage:/gluster\_bricks/arbiter2/1",  
“no\_of\_entries”: “0”,  
“status”: “Connected”  
},  
{  
“brick”: " storage3-storage:/gluster\_bricks/brick3/1"  
},  
{  
“no\_of\_entries”: “6”,  
“status”: “Connected”  
},  
{  
“brick”: " storage4-storage:/gluster\_bricks/brick4/1"  
},  
{  
“no\_of\_entries”: “6”,  
“status”: “Connected”  
},  
{  
“brick”: " storage5-storage:/gluster\_bricks/arbiter2/1",  
“no\_of\_entries”: “0”,  
“status”: “Connected”  
},  
{  
“brick”: " storage4-storage:/gluster\_bricks/brick3/1",  
“no\_of\_entries”: “0”,  
“status”: “Connected”  
},  
{  
“brick”: " storage5-storage:/gluster\_bricks/brick4/1",  
“no\_of\_entries”: “0”,  
“status”: “Connected”  
},  
{  
“brick”: " storage6-storage:/gluster\_bricks/arbiter2/1",  
“no\_of\_entries”: “0”,  
“status”: “Connected”  
},  
{  
“brick”: " storage5-storage:/gluster\_bricks/brick3/1",  
“no\_of\_entries”: “0”,  
“status”: “Connected”  
},  
{  
“brick”: " storage6-storage:/gluster\_bricks/brick4/1",  
“no\_of\_entries”: “0”,  
“status”: “Connected”  
},  
{  
“brick”: " storage7-storage:/gluster\_bricks/arbiter2/1",  
“no\_of\_entries”: “0”,  
“status”: “Connected”  
},  
{  
“brick”: " storage6-storage:/gluster\_bricks/brick3/1",  
“no\_of\_entries”: “0”,  
“status”: “Connected”  
},  
{  
“brick”: " storage7-storage:/gluster\_bricks/brick4/1",  
“no\_of\_entries”: “0”,  
“status”: “Connected”  
},  
{  
“brick”: " storage8-storage:/gluster\_bricks/arbiter2/1",  
“no\_of\_entries”: “0”,  
“status”: “Connected”  
},  
{  
“brick”: " storage7-storage:/gluster\_bricks/brick3/1",  
“no\_of\_entries”: “0”,  
“status”: “Connected”  
},  
{  
“brick”: " storage8-storage:/gluster\_bricks/brick4/1",  
“no\_of\_entries”: “0”,  
“status”: “Connected”  
},  
{  
“brick”: " storage9-storage:/gluster\_bricks/arbiter2/1",  
“no\_of\_entries”: “0”,  
“status”: “Connected”  
},  
{  
“brick”: " storage8-storage:/gluster\_bricks/brick3/1",  
“no\_of\_entries”: “0”,  
“status”: “Connected”  
},  
{  
“brick”: " storage9-storage:/gluster\_bricks/brick4/1",  
“no\_of\_entries”: “0”,  
“status”: “Connected”  
},  
{  
“brick”: " storage1-storage:/gluster\_bricks/arbiter2/1",  
“no\_of\_entries”: “0”,  
“status”: “Connected”  
},  
{  
“brick”: " storage9-storage:/gluster\_bricks/brick3/1",  
“no\_of\_entries”: “0”,  
“status”: “Connected”  
},  
{  
“brick”: " storage1-storage:/gluster\_bricks/brick4/1",  
“no\_of\_entries”: “0”,  
“status”: “Connected”  
},  
{  
“brick”: " storage2-storage:/gluster\_bricks/arbiter2/1",  
“no\_of\_entries”: “0”,  
“status”: “Connected”  
}  
],  
“rebalance”: “”,  
“status\_filter”: “self-heal”,  
“volume”: “home”  
}  
}  
}

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [December 28, 2020, 6:21pm UTC](https://forum.ansible.com/t/selecting-attributes-from-a-registered-variable/34374/4 "2020-12-28T18:21:07Z")

</div>

> I register the variable from the "gluster\_heal\_info" module and here's the printout of it. I can shorten it if this is  
> too long or too much to look at. Basically, I'm trying to gather the "no\_of\_entries" attribute from each gluster brick  
> and sum them together and make sure that it equals zero. That means that no healing is going on, so I can then shut down  
> the glusterd service and to a "yum update".
> 
> Thanks,  
> Harry

Hello Harry,

I think the problem is that not all dictionary entries carry a "no\_of\_entries" value.  
This is likely to cause the undefined variable error.

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![harry\_devine](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/harry_devine/32/5293_2.png) [@harry\_devine](https://forum.ansible.com/u/harry_devine)\
**Post date:** [December 28, 2020, 6:24pm UTC](https://forum.ansible.com/t/selecting-attributes-from-a-registered-variable/34374/5 "2020-12-28T18:24:41Z")

</div>

I understand that, but the variable that I register DOES return that as per my last email. But when I try to select it to use it, I get that error. Here’s my playbook for illustration:

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [December 28, 2020, 6:42pm UTC](https://forum.ansible.com/t/selecting-attributes-from-a-registered-variable/34374/6 "2020-12-28T18:42:55Z")

</div>

Just to be sure, what is the output of the var (not the msg)?

I.e.:

- debug: var=home\_heal\_status

---

<div class="post-metadata">

**Author:** ![harry\_devine](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/harry_devine/32/5293_2.png) [@harry\_devine](https://forum.ansible.com/u/harry_devine)\
**Post date:** [December 28, 2020, 6:48pm UTC](https://forum.ansible.com/t/selecting-attributes-from-a-registered-variable/34374/7 "2020-12-28T18:48:55Z")

</div>

It appears that some healing on the gluster volume is going on, which could by why some of the attributes appear to be missing. But no\_of\_entries is there on most.

storage1 | SUCCESS =\> {  
“home\_heal\_status”: {  
“ansible\_facts”: {  
“discovered\_interpreter\_python”: “/usr/bin/python”  
},  
“changed”: false,  
“failed”: false,  
“glusterfs”: {  
“heal\_info”: [  
{  
“brick”: " storage1-storage:/gluster\_bricks/brick3/1",  
“no\_of\_entries”: “0”,  
“status”: “Connected”  
},  
{  
“brick”: " storage2-storage:/gluster\_bricks/brick4/1",  
“no\_of\_entries”: “0”,  
“status”: “Connected”  
},  
{  
“brick”: " storage3-storage:/gluster\_bricks/arbiter2/1",  
“no\_of\_entries”: “0”,  
“status”: “Connected”  
},  
{  
“brick”: " storage2-storage:/gluster\_bricks/brick3/1",  
“no\_of\_entries”: “0”,  
“status”: “Connected”  
},  
{  
“brick”: " storage3-storage:/gluster\_bricks/brick4/1",  
“no\_of\_entries”: “0”,  
“status”: “Connected”  
},  
{  
“brick”: " storage4-storage:/gluster\_bricks/arbiter2/1",  
“no\_of\_entries”: “0”,  
“status”: “Connected”  
},  
{  
“brick”: " storage3-storage:/gluster\_bricks/brick3/1"  
},  
{  
“no\_of\_entries”: “6”,  
“status”: “Connected”  
},  
{  
“brick”: " storage4-storage:/gluster\_bricks/brick4/1"  
},  
{  
“no\_of\_entries”: “6”,  
“status”: “Connected”  
},  
{  
“brick”: " storage5-storage:/gluster\_bricks/arbiter2/1",  
“no\_of\_entries”: “0”,  
“status”: “Connected”  
},  
{  
“brick”: " storage4-storage:/gluster\_bricks/brick3/1",  
“no\_of\_entries”: “0”,  
“status”: “Connected”  
},  
{  
“brick”: " storage5-storage:/gluster\_bricks/brick4/1",  
“no\_of\_entries”: “0”,  
“status”: “Connected”  
},  
{  
“brick”: " storage6-storage:/gluster\_bricks/arbiter2/1",  
“no\_of\_entries”: “0”,  
“status”: “Connected”  
},  
{  
“brick”: " storage5-storage:/gluster\_bricks/brick3/1",  
“no\_of\_entries”: “0”,  
“status”: “Connected”  
},  
{  
“brick”: " storage6-storage:/gluster\_bricks/brick4/1",  
“no\_of\_entries”: “0”,  
“status”: “Connected”  
},  
{  
“brick”: " storage7-storage:/gluster\_bricks/arbiter2/1",  
“no\_of\_entries”: “0”,  
“status”: “Connected”  
},  
{  
“brick”: " storage6-storage:/gluster\_bricks/brick3/1",  
“no\_of\_entries”: “0”,  
“status”: “Connected”  
},  
{  
“brick”: " storage7-storage:/gluster\_bricks/brick4/1",  
“no\_of\_entries”: “0”,  
“status”: “Connected”  
},  
{  
“brick”: " storage8-storage:/gluster\_bricks/arbiter2/1",  
“no\_of\_entries”: “0”,  
“status”: “Connected”  
},  
{  
“brick”: " storage7-storage:/gluster\_bricks/brick3/1",  
“no\_of\_entries”: “0”,  
“status”: “Connected”  
},  
{  
“brick”: " storage8-storage:/gluster\_bricks/brick4/1",  
“no\_of\_entries”: “0”,  
“status”: “Connected”  
},  
{  
“brick”: " storage9-storage:/gluster\_bricks/arbiter2/1",  
“no\_of\_entries”: “0”,  
“status”: “Connected”  
},  
{  
“brick”: " storage8-storage:/gluster\_bricks/brick3/1",  
“no\_of\_entries”: “0”,  
“status”: “Connected”  
},  
{  
“brick”: " storage9-storage:/gluster\_bricks/brick4/1",  
“no\_of\_entries”: “0”,  
“status”: “Connected”  
},  
{  
“brick”: " storage1-storage:/gluster\_bricks/arbiter2/1",  
“no\_of\_entries”: “0”,  
“status”: “Connected”  
},  
{  
“brick”: " storage9-storage:/gluster\_bricks/brick3/1",  
“no\_of\_entries”: “0”,  
“status”: “Connected”  
},  
{  
“brick”: " storage1-storage:/gluster\_bricks/brick4/1",  
“no\_of\_entries”: “0”,  
“status”: “Connected”  
},  
{  
“brick”: " storage2-storage:/gluster\_bricks/arbiter2/1",  
“no\_of\_entries”: “0”,  
“status”: “Connected”  
}  
],  
“rebalance”: “”,  
“status\_filter”: “self-heal”,  
“volume”: “home”  
}  
}  
}

Harry

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [December 28, 2020, 7:03pm UTC](https://forum.ansible.com/t/selecting-attributes-from-a-registered-variable/34374/8 "2020-12-28T19:03:04Z")

</div>

This would evaluate to 12 in your example:

"{{ home\_heal\_status|from\_json|json\_query('glusterfs.heal\_info.no\_of\_entries')|map('int')|  
sum }}"

is that what you want?

---

<div class="post-metadata">

**Author:** ![harry\_devine](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/harry_devine/32/5293_2.png) [@harry\_devine](https://forum.ansible.com/u/harry_devine)\
**Post date:** [December 28, 2020, 7:25pm UTC](https://forum.ansible.com/t/selecting-attributes-from-a-registered-variable/34374/9 "2020-12-28T19:25:26Z")

</div>

It is, but I can’t get that to evaluate:

“msg”: “Unexpected templating type error occurred on ({{ home\_heal\_status|from\_json|json\_query(‘glusterfs.heal\_info.no\_of\_entries’)|map(‘int’)|sum }}): the JSON object must be str, bytes or bytearray, not ‘dict’”

Here’s what I tried:

- name: Print /home status  
debug:  
msg: “{{ home\_heal\_status|from\_json|json\_query(‘glusterfs.heal\_info.no\_of\_entries’)|map(‘int’)|sum }}”

Thanks,  
Harry

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [December 28, 2020, 9:51pm UTC](https://forum.ansible.com/t/selecting-attributes-from-a-registered-variable/34374/10 "2020-12-28T21:51:29Z")

</div>

Ah sorry , remove the from\_json bit

---

<div class="post-metadata">

**Author:** ![harry\_devine](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/harry_devine/32/5293_2.png) [@harry\_devine](https://forum.ansible.com/u/harry_devine)\
**Post date:** [December 29, 2020, 7:32pm UTC](https://forum.ansible.com/t/selecting-attributes-from-a-registered-variable/34374/11 "2020-12-29T19:32:53Z")

</div>

OK, that worked in the debug message. Just trying to figure out how to use it in the “until” statement. The main goal is to have the sum be 0 and then continue on. If its not 0 after a certain number of tries, then we fail out of the playbook.

Thanks,  
Harry

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [December 29, 2020, 7:42pm UTC](https://forum.ansible.com/t/selecting-attributes-from-a-registered-variable/34374/12 "2020-12-29T19:42:32Z")

</div>

> OK, that worked in the debug message. Just trying to figure out how to use it in the "until" statement. The main goal  
> is to have the sum be 0 and then continue on. If its not 0 after a certain number of tries, then we fail out of the  
> playbook.
> 
> Thanks,  
> Harry

My guess would be

&nbsp;&nbsp;&nbsp;until: home\_heal\_status|json\_query('glusterfs.heal\_info.no\_of\_entries')|map('int')|sum == 0

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke
