# How to access a certain element of a register variable in a loop

**URL:** <https://forum.ansible.com/t/how-to-access-a-certain-element-of-a-register-variable-in-a-loop/22644>\
**Category:** Ansible Project\
**Created:** [September 26, 2016, 3:40pm UTC](https://forum.ansible.com/t/how-to-access-a-certain-element-of-a-register-variable-in-a-loop/22644 "2016-09-26T15:40:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ZillaYT](https://avatars.discourse-cdn.com/v4/letter/z/4da419/32.png) [@ZillaYT](https://forum.ansible.com/u/ZillaYT)\
**Post date:** [September 26, 2016, 3:40pm UTC](https://forum.ansible.com/t/how-to-access-a-certain-element-of-a-register-variable-in-a-loop/22644/1 "2016-09-26T15:40:49Z")

</div>

I discovered the other day that when a register variable is used in a loop, like this

`

- name: Generate pw’s for users  
command: “/some\_path/generate\_hashed\_pw.sh -u {{ item }}”  
register: hashed\_pws  
with\_items:
- joe
- sally
- john  
`

the hashed\_pws register is a hash that contains a results key, which is an array of hashes, like so

`{ "hashed_pws: { "changed" : true, "some_other_key" : "some_other_value", "results" : [{ "item" : "joe", "stdout": "joes_hashed_pw", "some_other_key" : "some_other_value" }, { "item" : "sally", "stdout": "sallys_hashed_pw", "some_other_key" : "some_other_value" }, { "item" : "john", "stdout": "johns_hashed_pw", "some_other_key" : "some_other_value" }] } }`

So what is the syntax to directly access, the “stdout” element in each of the hash elements in the array? In other words, I want:

`

- debug: msg=“Sallys hashed pw is {{ hashed\_pws.results[SOME\_KEY\_TO\_DIRECTLY\_GET\_SALLYS\_STDOUT\_VALUE] }}”  
`

This is probably a python question as much as it is an Ansible question.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [September 26, 2016, 3:45pm UTC](https://forum.ansible.com/t/how-to-access-a-certain-element-of-a-register-variable-in-a-loop/22644/2 "2016-09-26T15:45:35Z")

</div>

I think this is what you want:

- debug: msg=“Sallys hashed pw is {{item.stdout}}”  
with\_items: “{{hashed\_pws.results}}”

---

<div class="post-metadata">

**Author:** ![ZillaYT](https://avatars.discourse-cdn.com/v4/letter/z/4da419/32.png) [@ZillaYT](https://forum.ansible.com/u/ZillaYT)\
**Post date:** [September 26, 2016, 4:34pm UTC](https://forum.ansible.com/t/how-to-access-a-certain-element-of-a-register-variable-in-a-loop/22644/3 "2016-09-26T16:34:33Z")

</div>

That will list ALL the item.stdout values, not just Sally’s.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [September 26, 2016, 5:03pm UTC](https://forum.ansible.com/t/how-to-access-a-certain-element-of-a-register-variable-in-a-loop/22644/4 "2016-09-26T17:03:35Z")

</div>

when: item.item == ‘sally’

---

<div class="post-metadata">

**Author:** ![ZillaYT](https://avatars.discourse-cdn.com/v4/letter/z/4da419/32.png) [@ZillaYT](https://forum.ansible.com/u/ZillaYT)\
**Post date:** [September 26, 2016, 5:07pm UTC](https://forum.ansible.com/t/how-to-access-a-certain-element-of-a-register-variable-in-a-loop/22644/5 "2016-09-26T17:07:04Z")

</div>

This gave me a clue. [Loop through a registered variable with with\_dict in Ansible](http://stackoverflow.com/questions/39174539/loop-through-a-registered-variable-with-with-dict-in-ansible/39175309#39175309), so I did this.

`

- debug: msg=‘{{ item }} hashed value is {{ hashed\_pws.results | selectattr(“item”,“equalto”,item) | map(attribute=“stdout”) | first }}’  
with\_items:
- joe
- sally
- john  
`

---

<div class="post-metadata">

**Author:** ![ishan\_jain](https://avatars.discourse-cdn.com/v4/letter/i/dc4da7/32.png) [@ishan\_jain](https://forum.ansible.com/u/ishan_jain)\
**Post date:** [September 27, 2016, 2:09pm UTC](https://forum.ansible.com/t/how-to-access-a-certain-element-of-a-register-variable-in-a-loop/22644/6 "2016-09-27T14:09:40Z")

</div>

If you know what you are looking for, isn’t this the simplest solution - when: item.item == ‘sally’
