Creating a RDS subnet group using with_items + registered vpc variable

1,355 views
Skip to first unread message

Timo Ahokas

unread,
Sep 25, 2014, 4:36:17 AM9/25/14
to ansible...@googlegroups.com
Hi,

I'm trying to use the rds_subnet_group module to create a subnet group with all the subnets defined in my VPC, but I'm having trouble finding a way of using the subnet ids from a registered vpc variable.

So, first I create a VPC & register the response:

  - name: Create VPC
    local_action: 
      module: ec2_vpc
      state: present
      subnets: ...
      route_tables: ...
    register: vpc

This works well and the result looks like:

TASK: [debug var=vpc] ********************************************************* 
ok: [localhost] => {
    "vpc": {
        "changed": true, 
        "invocation": {
            "module_args": "", 
            "module_name": "ec2_vpc"
        }, 
        "subnets": [
            {
                "az": "eu-west-11", 
                "cidr": "10.55.81.0/24", 
                "id": "subnet-d1d21ea6", 
                "resource_tags": {
                    "Environment": "test", 
                    "Name": "test_euw1a_app", 
                }
            },
            { ... } ] }

Now I'd like to use the subnet ids (subnets.id) in this response when creating a RDS subnet with all of them attached:

  - name: Create DB subnet group 
    local_action:
      module: rds_subnet_group
      state: present
      name: "rds-sg"
      description: "rds-sg"
      subnets: 
        - "{{ item.id }}"
    with_items: vpc.subnets

The problem is that if I have the with_item on the same level of local_action it seems to make N calls to rds_subnet_group (each with one of the ids) with fail with:
msg: DB Subnet Group doesn't meet availability zone coverage requirement. Please add subnets to cover at least 2 availability zones. Current coverage: 1

So I'd like to add all the subnets in one go (single call with all subnet ids defined for the "subnets" parameter). With_items doesn't seem to work when it's nested on the same level with the other params (subnets/state) - instead I get the error: 
fatal: [localhost -> 127.0.0.1] => One or more undefined variables: 'item' is undefined

What would be the best way to use all the vpc.subnets.id values in a list and supply those to rds_subnet_group.subnets in one go?

Best regards,
Timo


Timo Ahokas

unread,
Sep 25, 2014, 8:20:41 AM9/25/14
to ansible...@googlegroups.com
Found the answer luckily by searching the groups. 

Documented here if someone stumbles on the same thing (no need for with_items):

subnets: "{{ vpc.subnets | map(attribute='id') | list}}"

--
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/5a4543b8-d054-4c14-99a6-965ffb41d582%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Dan Vaida

unread,
Dec 4, 2014, 11:01:37 AM12/4/14
to ansible...@googlegroups.com
It's quite frustrating that the 'rds_subnet_group' produces too few debug info:
< TASK: debug var=rds_sg_creation >
 --------------------------------------- 
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||


ok: [localhost] => {
    "rds_sg_creation": {
        "changed": false, 
        "invocation": {
            "module_args": "", 
            "module_name": "rds_subnet_group"
        }
    }
}
Reply all
Reply to author
Forward
0 new messages