Retrieving vpc_subnet_id by resource tags

Let’s say I create an ec2 vpc like this:

tasks:

  • name: create a vpc
    ec2_vpc:
    internet_gateway: True
    resource_tags: { env: production }
    cidr_block: 10.0.0.0/16
    subnets:
  • cidr: 10.0.0.0/24
    resource_tags:
    env: production
    tier: web
  • cidr: 10.0.1.0/24
    resource_tags:
    env: production
    tier: db
    route_tables:
  • subnets:
  • 10.0.0.0/24
  • 10.0.1.0/24
    routes:
  • dest: 0.0.0.0/0
    gw: igw

register: vpc

Later in the play, I want to create an instance inside of subnet 10.0.0.0/24 (web tier):

ec2:
image: “{{ ubuntu_image.ami }}”
instance_type: “{{ instance_type }}”
assign_public_ip: True
key_name: mykey
group: [web, ssh]
instance_tags: { type: web, env: production }
exact_count: “{{ count }}”
count_tag: { type: web }
vpc_subnet_id: “{{ vpc.subnets[0].id}}”

Is vpc.subnets[0] guaranteed to correspond to the first subnet that was created (10.0.0.0/24)? If not, is there a way I can specify “the vpc subnet that corresponds to resource tag {env: production, tier: web}?”

Lorin

Hi Lorin,

It looks like your question is similar to mine: https://groups.google.com/d/msg/ansible-project/Xy3kt0Mr8YY/eB6TqMN0Y3EJ
Didn’t had the time to explore potential fixes yet though…
Perhaps you have some updates from your side.

Cheers,

Dan.