# ec2 elb set subnets from vpc return value

**URL:** <https://forum.ansible.com/t/ec2-elb-set-subnets-from-vpc-return-value/16117>\
**Category:** Ansible Project\
**Created:** [September 4, 2014, 7:41pm UTC](https://forum.ansible.com/t/ec2-elb-set-subnets-from-vpc-return-value/16117 "2014-09-04T19:41:42Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![itarchmerc](https://avatars.discourse-cdn.com/v4/letter/i/ecb155/32.png) [@itarchmerc](https://forum.ansible.com/u/itarchmerc)\
**Post date:** [September 4, 2014, 7:41pm UTC](https://forum.ansible.com/t/ec2-elb-set-subnets-from-vpc-return-value/16117/1 "2014-09-04T19:41:42Z")

</div>

I have a playbook that creates a VPC with multiple subnets which works fine. I then have a playbook that tries to create an ELB and I need to set the subnets to 2 of the subnets returned from the vpc creation. There are 6 total subnets within the VPC, but two of them have resource\_tags set to tier:elb. I only want the 2 subnets with those tags.

I saw a [post](https://github.com/ansible/ansible/pull/7051) that said the following would work, but I get an invalid subnet ID error. (look for the comments from @coop)

subnets: “{{ vpc.subnets | join(‘,’ attribute=‘id’) }}”

My playbook is almost exactly the same as @coop in the post above, but I have 6 subnets instead of 1. I feel like I should be able to use something like the above with a when clause to only get subnets tagged as elb. Any help is appreciated.

---

<div class="post-metadata">

**Author:** ![James\_Cammarata](https://avatars.discourse-cdn.com/v4/letter/j/7c8e57/32.png) [@James\_Cammarata](https://forum.ansible.com/u/James_Cammarata)\
**Post date:** [September 5, 2014, 3:14am UTC](https://forum.ansible.com/t/ec2-elb-set-subnets-from-vpc-return-value/16117/2 "2014-09-05T03:14:39Z")

</div>

Hi. From the look of the above, you’re using the “complex args” structure to pass the params to the module. In this case, the list should not be a comma-separated list but should instead just be the array of IDs. For example:

subnets: “{{vpc.subnets | map(attribute=‘id’) | list}}”

---

<div class="post-metadata">

**Author:** ![itarchmerc](https://avatars.discourse-cdn.com/v4/letter/i/ecb155/32.png) [@itarchmerc](https://forum.ansible.com/u/itarchmerc)\
**Post date:** [September 5, 2014, 12:32pm UTC](https://forum.ansible.com/t/ec2-elb-set-subnets-from-vpc-return-value/16117/3 "2014-09-05T12:32:02Z")

</div>

That worked perfectly to get the list of subnets. Thank you!

The only issue I have now is limiting the returned values. The syntax below is returning all 6 subnets in my vpc, but I only want the 2 subnets that I gave a resource tag of tier=elb. I’m trying to use the when clause with the ec2\_elb\_lb module, but I can’t get the syntax right there. I tried:

when: {{ vpc.subnets | map(attribute=‘tier’) | list }} == ‘elb’

This generates a syntax error each time. What should the format be in a when clause and/or is this the right way to approach limiting the results, or should I be using with\_items or with\_dict? Thanks for the help!

---

<div class="post-metadata">

**Author:** ![itarchmerc](https://avatars.discourse-cdn.com/v4/letter/i/ecb155/32.png) [@itarchmerc](https://forum.ansible.com/u/itarchmerc)\
**Post date:** [September 5, 2014, 2:45pm UTC](https://forum.ansible.com/t/ec2-elb-set-subnets-from-vpc-return-value/16117/4 "2014-09-05T14:45:13Z")

</div>

I got one step further. Using this debug statement, I was able to get the list of resource\_tags:

debug: msg=“{{vpc.subnets | map(attribute=‘resource\_tags’) | map(attribute=‘tier’) | list}}”

I’m not sure if this is the right way to go about getting this information, so please let me know if there is a better way.

Unfortunately, I’m still having difficulty having the task only pull the subnet id for the subnets tagged as tier=elb. Can I use something after the pipe instead of list to limit the results, or do I need to use with\_items, with\_dict? Thanks.

---

<div class="post-metadata">

**Author:** ![James\_Cammarata](https://avatars.discourse-cdn.com/v4/letter/j/7c8e57/32.png) [@James\_Cammarata](https://forum.ansible.com/u/James_Cammarata)\
**Post date:** [September 5, 2014, 8:07pm UTC](https://forum.ansible.com/t/ec2-elb-set-subnets-from-vpc-return-value/16117/5 "2014-09-05T20:07:01Z")

</div>

You could use the selectattr() filter, however it looks like the ‘equalto’ test is new (it doesn’t work in my version of jinja2, which is 2.7.x). But it would looks something like this:

- debug: msg=“{{vpc.subnets | map(attribute=‘resource\_tags’) | selectattr(‘tier’, ‘equalto’, ‘elb’) | list}}”

---

<div class="post-metadata">

**Author:** ![itarchmerc](https://avatars.discourse-cdn.com/v4/letter/i/ecb155/32.png) [@itarchmerc](https://forum.ansible.com/u/itarchmerc)\
**Post date:** [September 5, 2014, 9:34pm UTC](https://forum.ansible.com/t/ec2-elb-set-subnets-from-vpc-return-value/16117/6 "2014-09-05T21:34:18Z")

</div>

OK…getting closer. I had to upgrade Jinja2 to version 2.8-dev in order to get the equalto test. Now the debug statement executes without error; however, this doesn’t return the information I need. The statement below gives me all resource\_tags if the tier key equals “elb”. What I really need is the subnet id value if the resource\_tag has the tier key equal to “elb”. I feel like another level of nesting is required. Any ideas on how to get the subnet id? Thank you James for all your help so far!

“vpc”: {  
“changed”: false,  
“invocation”: {  
“module\_args”: “”,  
“module\_name”: “ec2\_vpc”  
},  
“subnets”: [  
{  
“az”: “us-east-1c”,  
“cidr”: “10.0.0.x/26”,  
“id”: “subnet-123456789”, -----\> I need this value  
“resource\_tags”: {  
“Name”: “elb1”,  
“tier”: “elb”  
}  
},  
{  
“az”: “us-east-1a”,  
“cidr”: “10.0.0.x/26”,  
“id”: “subnet-abcdefgh”, ----\> and this  
“resource\_tags”: {  
“Name”: “elb2”,  
“tier”: “elb”  
}  
}

---

<div class="post-metadata">

**Author:** ![itarchmerc](https://avatars.discourse-cdn.com/v4/letter/i/ecb155/32.png) [@itarchmerc](https://forum.ansible.com/u/itarchmerc)\
**Post date:** [September 6, 2014, 3:27am UTC](https://forum.ansible.com/t/ec2-elb-set-subnets-from-vpc-return-value/16117/7 "2014-09-06T03:27:37Z")

</div>

Rather than trying to do this with a one line jinja2 statement, could I use with\_items and when? I tried this, but it generates an error.

- name: create elb  
local\_action:  
module: ec2\_elb\_lb  
name: “elb1”  
scheme: internet-facing  
state: present  
subnets: “{{ item.id }}”  
security\_group\_ids: “{{ elb\_sg.group\_id }}”  
region: us-east-1  
listeners:
- protocol: http  
load\_balancer\_port: 80  
instance\_port: 80  
with\_items: vpc.subnets  
when: vpc.subnets.resource\_tags.tier == ‘elb’  
register: elb

I get this error:

error while evaluating conditional: vpc.subnets.resource\_tags.tier == ‘elb’

I’m hoping this method can be used, but I’m not sure of the format for the conditional.

---

<div class="post-metadata">

**Author:** ![Steven\_Ringo](https://avatars.discourse-cdn.com/v4/letter/s/bc79bd/32.png) [@Steven\_Ringo](https://forum.ansible.com/u/Steven_Ringo)\
**Post date:** [September 25, 2014, 2:34am UTC](https://forum.ansible.com/t/ec2-elb-set-subnets-from-vpc-return-value/16117/8 "2014-09-25T02:34:24Z")

</div>

Hi,

Try this:

- ec2\_elb\_lb:  
name: csds-elb-sydney-dev  
foo: bar  
subnets:

- “{{ item.id }}”  
with\_items:

- “{{ csds\_vpc.subnets }}”  
when: item.resource\_tags[‘Tier’] == ‘elb’ and  
item.resource\_tags[‘Application’] == ‘MyApp’

It works, but feels hacky, alas.

Steve

---

<div class="post-metadata">

**Author:** ![Stefan\_Nietert](https://avatars.discourse-cdn.com/v4/letter/s/e36b37/32.png) [@Stefan\_Nietert](https://forum.ansible.com/u/Stefan_Nietert)\
**Post date:** [January 12, 2015, 12:05am UTC](https://forum.ansible.com/t/ec2-elb-set-subnets-from-vpc-return-value/16117/9 "2015-01-12T00:05:22Z")

</div>

When doing this I get:

“One or more undefined variables: ‘item’ is undefined”

It did work for you?

---

<div class="post-metadata">

**Author:** ![vaida.dan](https://avatars.discourse-cdn.com/v4/letter/v/5daacb/32.png) [@vaida.dan](https://forum.ansible.com/u/vaida.dan)\
**Post date:** [January 13, 2015, 7:44pm UTC](https://forum.ansible.com/t/ec2-elb-set-subnets-from-vpc-return-value/16117/10 "2015-01-13T19:44:27Z")

</div>

Try with csds\_vpc.subnets[0].id

---

<div class="post-metadata">

**Author:** ![Gary\_Morse](https://avatars.discourse-cdn.com/v4/letter/g/85f322/32.png) [@Gary\_Morse](https://forum.ansible.com/u/Gary_Morse)\
**Post date:** [March 23, 2015, 3:57pm UTC](https://forum.ansible.com/t/ec2-elb-set-subnets-from-vpc-return-value/16117/11 "2015-03-23T15:57:31Z")

</div>

This works, but you need to step the Tab back one

- ec2\_elb\_lb:  
name: csds-elb-sydney-dev  
foo: bar  
subnets:

- “{{ [item.id](http://item.id/) }}”  
with\_items:

- “{{ csds\_vpc.subnets }}”  
when: item.resource\_tags[‘Tier’] == ‘elb’ anditem.resource\_tags[‘Application’] == ‘MyApp’
