# How to extract one attribute from a list of dictionaries

**URL:** <https://forum.ansible.com/t/how-to-extract-one-attribute-from-a-list-of-dictionaries/32748>\
**Category:** Ansible Project\
**Created:** [April 21, 2020, 3:39pm UTC](https://forum.ansible.com/t/how-to-extract-one-attribute-from-a-list-of-dictionaries/32748 "2020-04-21T15:39:46Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![actionmystique](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/actionmystique/32/1678_2.png) [@actionmystique](https://forum.ansible.com/u/actionmystique)\
**Post date:** [April 21, 2020, 3:39pm UTC](https://forum.ansible.com/t/how-to-extract-one-attribute-from-a-list-of-dictionaries/32748/1 "2020-04-21T15:39:46Z")

</div>

Let’s suppose the following list of dictionaries:

`

- domain\_definition:
- name: server11  
cluster: cluster1  
port: ‘8080’
- name: server12  
cluster: cluster2  
port: ‘8090’
- name: server21  
cluster: cluster3  
port: ‘9080’
- name: server22  
cluster: cluster4  
port: ‘9090’

`

The aim is to extract all `name: 'value'` from `domain_definition` into a list:

`

- name: server11
- name: server12
- name: server21
- name: server22

`

I tried for instance the following:

`

- set\_fact:  
domain\_definition\_names\_list: “{{ [‘name’] |  
intersect(domain\_definition.keys()|list) }}”

`  
but it fails:

`

fatal: [localhost]: FAILED! =\> {  
“msg”: "The task includes an option with an undefined variable. The error was: ‘list object’ has no attribute ‘keys’…}

`

Any suggestion?

---

<div class="post-metadata">

**Author:** ![actionmystique](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/actionmystique/32/1678_2.png) [@actionmystique](https://forum.ansible.com/u/actionmystique)\
**Post date:** [April 21, 2020, 3:41pm UTC](https://forum.ansible.com/t/how-to-extract-one-attribute-from-a-list-of-dictionaries/32748/2 "2020-04-21T15:41:30Z")

</div>

I’m using:

- ansible 2.9.7
- python 3.8.2

---

<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:** [April 21, 2020, 3:54pm UTC](https://forum.ansible.com/t/how-to-extract-one-attribute-from-a-list-of-dictionaries/32748/3 "2020-04-21T15:54:26Z")

</div>

just to make sure - this is a list of list of dictionaries.  
If this is your source of truth, and you intend to have a list of dicts, then it could look like this:

domain\_definition:

- name: server11  
cluster: cluster1  
port: ‘8080’
- name: server12  
cluster: cluster2  
port: ‘8090’
- name: server21  
cluster: cluster3  
port: ‘9080’
- name: server22  
cluster: cluster4  
port: ‘9090’

This will make a difference when manipulating the data.

---

<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:** [April 21, 2020, 4:04pm UTC](https://forum.ansible.com/t/how-to-extract-one-attribute-from-a-list-of-dictionaries/32748/4 "2020-04-21T16:04:17Z")

</div>

More accurately, the data structure you posted isn’t a variable, it’s an item from a “upper” list variable - which we don’t know the name of.

An example:

---

<div class="post-metadata">

**Author:** ![actionmystique](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/actionmystique/32/1678_2.png) [@actionmystique](https://forum.ansible.com/u/actionmystique)\
**Post date:** [April 21, 2020, 4:12pm UTC](https://forum.ansible.com/t/how-to-extract-one-attribute-from-a-list-of-dictionaries/32748/5 "2020-04-21T16:12:46Z")

</div>

That’s what I meant (just a typo in the structure).

---

<div class="post-metadata">

**Author:** ![actionmystique](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/actionmystique/32/1678_2.png) [@actionmystique](https://forum.ansible.com/u/actionmystique)\
**Post date:** [April 21, 2020, 4:35pm UTC](https://forum.ansible.com/t/how-to-extract-one-attribute-from-a-list-of-dictionaries/32748/6 "2020-04-21T16:35:22Z")

</div>

Thanks. 🙂

---

<div class="post-metadata">

**Author:** ![SandeepK](https://avatars.discourse-cdn.com/v4/letter/s/e19adc/32.png) [@SandeepK](https://forum.ansible.com/u/SandeepK)\
**Post date:** [April 23, 2020, 6:15pm UTC](https://forum.ansible.com/t/how-to-extract-one-attribute-from-a-list-of-dictionaries/32748/7 "2020-04-23T18:15:28Z")

</div>

Can we give the same information in hosts file and still work?

---

<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:** [April 24, 2020, 4:34am UTC](https://forum.ansible.com/t/how-to-extract-one-attribute-from-a-list-of-dictionaries/32748/8 "2020-04-24T04:34:05Z")

</div>

what do you mean. Examples with code?

---

<div class="post-metadata">

**Author:** ![actionmystique](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/actionmystique/32/1678_2.png) [@actionmystique](https://forum.ansible.com/u/actionmystique)\
**Post date:** [April 24, 2020, 8:01am UTC](https://forum.ansible.com/t/how-to-extract-one-attribute-from-a-list-of-dictionaries/32748/9 "2020-04-24T08:01:30Z")

</div>

Yes, of course: you can define the variable anywhere, including in the hosts file.  
There are 2 differences though:

- the [precedence](https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#variable-precedence-where-should-i-put-a-variable).
- the way the variables are defined: the inventory file is not a yaml file (ini format). Refer to this [post](https://stackoverflow.com/questions/29856738/how-to-define-hash-dict-in-ansible-inventory-file) for how to define your dictionary(ies) in the hosts file.

---

<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:** [April 24, 2020, 8:05am UTC](https://forum.ansible.com/t/how-to-extract-one-attribute-from-a-list-of-dictionaries/32748/10 "2020-04-24T08:05:44Z")

</div>

> Yes, of course: you can define the variable anywhere, including in the hosts file.  
> There are 2 differences though:
> 
> &nbsp;&nbsp;\* the precedence  
> &nbsp;&nbsp;&nbsp;&nbsp;\<[https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_variables.html#variable-precedence-where-should-i-put-a-variable&gt;\](https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#variable-precedence-where-should-i-put-a-variable&gt;%5C).  
> &nbsp;&nbsp;\* the way the variables are defined: the inventory file is not a yaml file (ini format). Refer to this post  
> &nbsp;&nbsp;&nbsp;&nbsp;\<[https://stackoverflow.com/questions/29856738/how-to-define-hash-dict-in-ansible-inventory-file&gt](https://stackoverflow.com/questions/29856738/how-to-define-hash-dict-in-ansible-inventory-file&gt); for how to define  
> &nbsp;&nbsp;&nbsp;&nbsp;your dictionary(ies) in the hosts file.

The second statement is correct only if you refer to the host file (defaults to /etc/ansible/host). The inventory for  
your playbook can be written completely in YAML.

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

---

<div class="post-metadata">

**Author:** ![SandeepK](https://avatars.discourse-cdn.com/v4/letter/s/e19adc/32.png) [@SandeepK](https://forum.ansible.com/u/SandeepK)\
**Post date:** [April 24, 2020, 2:54pm UTC](https://forum.ansible.com/t/how-to-extract-one-attribute-from-a-list-of-dictionaries/32748/11 "2020-04-24T14:54:19Z")

</div>

Thank you guys that helps!
