# How to extract a specific value from input list ?

**URL:** <https://forum.ansible.com/t/how-to-extract-a-specific-value-from-input-list/36966>\
**Category:** Ansible Project\
**Created:** [August 9, 2022, 6:07am UTC](https://forum.ansible.com/t/how-to-extract-a-specific-value-from-input-list/36966 "2022-08-09T06:07:32Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![dudu.c](https://avatars.discourse-cdn.com/v4/letter/d/278dde/32.png) [@dudu.c](https://forum.ansible.com/u/dudu.c)\
**Post date:** [August 9, 2022, 6:07am UTC](https://forum.ansible.com/t/how-to-extract-a-specific-value-from-input-list/36966/1 "2022-08-09T06:07:32Z")

</div>

I have the following input list

postgres\_create\_users:

- {role: user1 , password: password1 }
- {role: user2, password: password2}
- {role: user3, password: password3}
- {role: user4, password: password4}

As part of J2 template file I need to extract the password of user4 - How this can be done ?

insights.jdbc.password={{ postgres\_create\_users ??? }}

Thank you

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [August 9, 2022, 10:48am UTC](https://forum.ansible.com/t/how-to-extract-a-specific-value-from-input-list/36966/2 "2022-08-09T10:48:59Z")

</div>

> insights.jdbc.password={{ postgres\_create\_users ??? }}

Create a dictionary. The best choice might be the same place the list  
\*postgres\_create\_users\* comes from. For example,

> cat group\_vars/all/postgres\_create\_users.yml

postgres\_create\_users:  
&nbsp;&nbsp;- {role: user1, password: password1}  
&nbsp;&nbsp;- {role: user2, password: password2}  
&nbsp;&nbsp;- {role: user3, password: password3}  
&nbsp;&nbsp;- {role: user4, password: password4}  
pcu\_dict: "{{ postgres\_create\_users|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;items2dict(key\_name='role',  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;value\_name='password') }}"

gives

&nbsp;&nbsp;pcu\_dict:  
&nbsp;&nbsp;&nbsp;&nbsp;user1: password1  
&nbsp;&nbsp;&nbsp;&nbsp;user2: password2  
&nbsp;&nbsp;&nbsp;&nbsp;user3: password3  
&nbsp;&nbsp;&nbsp;&nbsp;user4: password4

The usage is trivial. For example, the template

> cat templates/server.xml.j2

insights.jdbc.password="{{ pcu\_dict.user4 }}"

and the playbook

> cat pb.yml

- hosts: localhost  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ lookup('template', 'server.xml.j2') }}"

gives (abridged)

> ansible-playbook pb.yml

TASK [debug]

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [August 9, 2022, 12:09pm UTC](https://forum.ansible.com/t/how-to-extract-a-specific-value-from-input-list/36966/3 "2022-08-09T12:09:41Z")

</div>

You also can use json\_query.

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [August 9, 2022, 1:01pm UTC](https://forum.ansible.com/t/how-to-extract-a-specific-value-from-input-list/36966/4 "2022-08-09T13:01:10Z")

</div>

> You also can use json\_query.

For example, the template

> cat templates/server.xml.j2

insights.jdbc.password="{{ my\_pswd }}"

and the task

&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ lookup('template', 'server.xml.j2') }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my\_user: user4  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my\_pswd: "{{ postgres\_create\_users|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;json\_query(\_query)|first }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\_query: '[?role==`{{ my_user }}`].password'

gives the same result

TASK [debug]

---

<div class="post-metadata">

**Author:** ![Richard\_Megginson](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/richard_megginson/32/4540_2.png) [@Richard\_Megginson](https://forum.ansible.com/u/Richard_Megginson)\
**Post date:** [August 9, 2022, 1:10pm UTC](https://forum.ansible.com/t/how-to-extract-a-specific-value-from-input-list/36966/5 "2022-08-09T13:10:26Z")

</div>

Here is a way that

- uses only ansible built-ins (note that json\_query is a) in an unsupported collection b) requires python json libraries which may not be available)
- works with every version of jinja2 (EL7 and later controller)

insights.jdbc.password={{ postgres\_create\_users | selectattr(‘role’, ‘match’, ‘^user4$’) | map(attribute=‘password’) | first }}

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [August 9, 2022, 2:12pm UTC](https://forum.ansible.com/t/how-to-extract-a-specific-value-from-input-list/36966/6 "2022-08-09T14:12:48Z")

</div>

Nice!

Walter

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [August 9, 2022, 2:17pm UTC](https://forum.ansible.com/t/how-to-extract-a-specific-value-from-input-list/36966/7 "2022-08-09T14:17:46Z")

</div>

Ansible is an amazing tool. So many creative and powerful ways to massage data and get what we want.
