# map()...What am I doing wrong here?

**URL:** <https://forum.ansible.com/t/map-what-am-i-doing-wrong-here/27342>\
**Category:** Ansible Project\
**Created:** [April 22, 2018, 11:30pm UTC](https://forum.ansible.com/t/map-what-am-i-doing-wrong-here/27342 "2018-04-22T23:30:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gregory\_Mirsky](https://avatars.discourse-cdn.com/v4/letter/g/c89c15/32.png) [@Gregory\_Mirsky](https://forum.ansible.com/u/Gregory_Mirsky)\
**Post date:** [April 22, 2018, 11:30pm UTC](https://forum.ansible.com/t/map-what-am-i-doing-wrong-here/27342/1 "2018-04-22T23:30:15Z")

</div>

I’ve been trying to extract the values for subnet\_id from the dictionary subnet\_facts but keep getting undefined (3 times instead of the values) as a return value. I should be getting [subnet-fd6bfa95, subnet-7c8bb031, subnet-7c8bb031]

Any ideas?

Below is the local host testbed playbook i’ve been using trying to get this jinja filter to work.

---

<div class="post-metadata">

**Author:** ![flowerysong1](https://avatars.discourse-cdn.com/v4/letter/f/ecae2f/32.png) [@flowerysong1](https://forum.ansible.com/u/flowerysong1)\
**Post date:** [April 23, 2018, 2:25am UTC](https://forum.ansible.com/t/map-what-am-i-doing-wrong-here/27342/2 "2018-04-23T02:25:37Z")

</div>

> I’ve been trying to extract the values for subnet\_id from the dictionary subnet\_facts but keep getting undefined (3 times instead of the values) as a return value. I should be getting [subnet-fd6bfa95, subnet-7c8bb031, subnet-7c8bb031]
> 
> Any ideas?
> 
> Below is the local host testbed playbook i’ve been using trying to get this jinja filter to work.

‘subnets’ is a list (observe the .)

> > “subnets”: [  
> > {  
> > “assign\_ipv6\_address\_on\_creation”: false,  
> > “availability\_zone”: “us-east-2b”,  
> > “available\_ip\_address\_count”: 251,  
> > “cidr\_block”: “[10.0.2.0/24](http://10.0.2.0/24)”,  
> > “default\_for\_az”: false,  
> > “id”: “subnet-6ad01910”,  
> > “ipv6\_cidr\_block\_association\_set”: ,  
> > “map\_public\_ip\_on\_launch”: true,  
> > “state”: “available”,  
> > “subnet\_id”: “subnet-6ad01910”,  
> > “tags”: {  
> > “DeleteTag”: “[test-g...@xxxx.com](mailto:test-g...@xxxx.com)”,  
> > “Name”: “test-subnet-us-east-2b-public”,  
> > “SubnetType”: “public”  
> > },  
> > “vpc\_id”: “vpc-1227b97a”  
> > }  
> > ]

> > var: “{{ subnet\_facts | selectattr(‘[results.subnets.id](http://results.subnets.id)’) | list }}”

Here, you appear to be trying to get the ‘id’ attribute of ‘subnets’. It doesn’t have one, because it’s a list.

`  
var: “subnet\_facts.results | map(attribute=‘subnets’) | flatten | map(attribute=‘id’) | list”

`

(The `flatten` filter is new in Ansible 2.5; on older versions you’d need to use the `flatten` lookup.)

---

<div class="post-metadata">

**Author:** ![Gregory\_Mirsky](https://avatars.discourse-cdn.com/v4/letter/g/c89c15/32.png) [@Gregory\_Mirsky](https://forum.ansible.com/u/Gregory_Mirsky)\
**Post date:** [April 23, 2018, 2:09pm UTC](https://forum.ansible.com/t/map-what-am-i-doing-wrong-here/27342/3 "2018-04-23T14:09:51Z")

</div>

Thank you so much! This worked perfectly!

ok: [localhost] =\> {

“subnet\_facts.results | map(attribute=‘subnets’) | flatten | map(attribute=‘id’) | list”: [

“subnet-fd6bfa95”,

“subnet-6ad01910”,

“subnet-7c8bb031”

]

}
