Creating a RDS subnet group using with_items + registered vpc variable

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

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}}”

It’s quite frustrating that the ‘rds_subnet_group’ produces too few debug info:

`

< TASK: debug var=rds_sg_creation >