# Use Ansible template to pull out AWS AZ and subnet ID from a list of subnets.

**URL:** <https://forum.ansible.com/t/use-ansible-template-to-pull-out-aws-az-and-subnet-id-from-a-list-of-subnets/30089>\
**Category:** Ansible Project\
**Tags:** aws\
**Created:** [April 10, 2019, 1:59pm UTC](https://forum.ansible.com/t/use-ansible-template-to-pull-out-aws-az-and-subnet-id-from-a-list-of-subnets/30089 "2019-04-10T13:59:36Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Steve\_Button](https://avatars.discourse-cdn.com/v4/letter/s/b5a626/32.png) [@Steve\_Button](https://forum.ansible.com/u/Steve_Button)\
**Post date:** [April 10, 2019, 1:59pm UTC](https://forum.ansible.com/t/use-ansible-template-to-pull-out-aws-az-and-subnet-id-from-a-list-of-subnets/30089/1 "2019-04-10T13:59:36Z")

</div>

Been scratching my head with this one. Even digging into the jmespath docs, but really stuck.

[http://jmespath.org/specification.html#map](http://jmespath.org/specification.html#map)

I have a bunch of subnets, which I get back with :-

- name: Cluster create | Get Private Subnet facts  
ec2\_vpc\_subnet\_facts:  
filters:  
vpc-id: “{{ vpc\_id }}”  
“tag:Name”: “private-\*”  
register: result

- name: Cluster create | debug result of private ec2\_vpc\_subnet\_facts  
debug:  
var: result

This returns with :–

TASK [k8s\_cluster : Cluster create | debug result of private ec2\_vpc\_subnet\_facts] \*\*\*  
ok: [localhost] =\> {  
“result”: {  
“changed”: false,  
“failed”: false,  
“subnets”: [  
{  
“assign\_ipv6\_address\_on\_creation”: false,  
“availability\_zone”: “eu-west-1c”,  
“availability\_zone\_id”: “euw1-az3”,  
“available\_ip\_address\_count”: 46,  
“cidr\_block”: “172.172.172.64/26”,  
“default\_for\_az”: false,  
“id”: “subnet-fefefefe”,  
“ipv6\_cidr\_block\_association\_set”: ,  
“map\_public\_ip\_on\_launch”: false,  
“owner\_id”: “987654321”,  
“state”: “available”,  
“subnet\_arn”: “arn:aws:ec2:eu-west-1:987654321:subnet/subnet-fefefefe”,  
“subnet\_id”: “subnet-fefefefe”,  
“tags”: {  
…  
},  
“vpc\_id”: “vpc-cececece”  
},  
{

(I changed the subnet IDs and VPC ID. )

I’ve been trying something along the line of :-

- set\_fact:  
az\_to\_private\_sub: “{{ result.subnets | map(‘subnet.availability\_zone’: ‘subnets.subnet\_id’) ) }}”

However, no joy.

I managed to extract the subnet IDs, using :-

- set\_fact:  
az\_to\_private\_sub: “{{ result | json\_query(‘subnets[\*].subnet\_id’ | map() ) }}”

but what I’m trying to achieve needs both the AZ and the subnet ID, like this :-

az\_to\_private\_sub:  
eu-west-1a: subnet-aaaaaaaa  
eu-west-1b: subnet-bbbbbbbb  
eu-west-1c: subnet-cccccccc  
az\_to\_public\_sub:  
eu-west-1a: subnet-dddddddd  
eu-west-1b: subnet-eeeeeeee  
eu-west-1c: subnet-ffffffff

Any ideas?
