ec2 elb set subnets from vpc return value

588 views
Skip to first unread message

itarchmerc

unread,
Sep 4, 2014, 3:41:43 PM9/4/14
to ansible...@googlegroups.com
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 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.

James Cammarata

unread,
Sep 4, 2014, 11:14:53 PM9/4/14
to ansible...@googlegroups.com
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}}"

The syntax you're using above would work if you were instead using key=value parameters, ie: subnets="{{ vpc.subnets | join(',' attribute='id') }}"


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/a2e130f5-007e-49b3-965f-31810e866efa%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

itarchmerc

unread,
Sep 5, 2014, 8:32:02 AM9/5/14
to ansible...@googlegroups.com
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!

itarchmerc

unread,
Sep 5, 2014, 10:45:13 AM9/5/14
to ansible...@googlegroups.com
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.

James Cammarata

unread,
Sep 5, 2014, 4:07:04 PM9/5/14
to ansible...@googlegroups.com
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}}"



itarchmerc

unread,
Sep 5, 2014, 5:34:18 PM9/5/14
to ansible...@googlegroups.com
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"

itarchmerc

unread,
Sep 5, 2014, 11:27:37 PM9/5/14
to ansible...@googlegroups.com
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.

Steven Ringo

unread,
Sep 24, 2014, 10:34:25 PM9/24/14
to ansible...@googlegroups.com
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

Stefan Nietert

unread,
Jan 11, 2015, 7:05:22 PM1/11/15
to ansible...@googlegroups.com
When doing this I get:

"One or more undefined variables: 'item' is undefined"

It did work for you?

Dan Vaida

unread,
Jan 13, 2015, 2:44:28 PM1/13/15
to ansible...@googlegroups.com
Try with csds_vpc.subnets[0].id

Gary Morse

unread,
Mar 23, 2015, 11:58:40 AM3/23/15
to ansible...@googlegroups.com
This works, but you need to step the Tab back one 
- ec2_elb_lb:
      name: csds-elb-sydney-dev
      foo: bar
      subnets:
        - "{{ item.id }}"
    with_items:
      - "{{ csds_vpc.subnets }}"
    when: item.resource_tags['Tier'] == 'elb' anditem.resource_tags['Application'] == 'MyApp'


This communication (including all attachments) is intended solely for the use of the person(s) to whom it is addressed and should be treated as a confidential AAA communication.  If you are not the intended recipient, any use, distribution, printing, or copying of this email is strictly prohibited.  If you received this email in error, please immediately delete it from your system and notify the originator.  Your cooperation is appreciated.

Reply all
Reply to author
Forward
0 new messages