I have a VPC with some routes and subnets. I want to get ansible to work with it but I can't see a way to link the subnet with the route table ID. I've used the ec2_vpc_route_table_facts, ec2_vpc_subnet_facts and ec2_vpc_net_facts modules but unless I'm missing something, none of them provide tell me which subnet is associated with which route table.
TASK [ec2_vpc_route_table_facts] ***********************************************
ok: [localhost]
TASK [display routes] **********************************************************
ok: [localhost] => {
"msg": {
"changed": false,
"route_tables": [
{
"id": "rtb-83fd25e7",
"routes": [
{
"destination_cidr_block": "172.31.0.0/16",
"gateway_id": "local",
"instance_id": null,
"interface_id": null,
"origin": "CreateRouteTable",
"state": "active",
"vpc_peering_connection_id": null
},
{
"destination_cidr_block": "0.0.0.0/0",
"gateway_id": "igw-6f792c0a",
"instance_id": null,
"interface_id": null,
"origin": "CreateRoute",
"state": "active",
"vpc_peering_connection_id": null
}
],
"tags": {},
"vpc_id": "vpc-e749a683"
},
{
"id": "rtb-abf02bcf",
"routes": [
{
"destination_cidr_block": "172.31.0.0/16",
"gateway_id": "local",
"instance_id": null,
"interface_id": null,
"origin": "CreateRouteTable",
"state": "active",
"vpc_peering_connection_id": null
}
],
"tags": {},
"vpc_id": "vpc-e749a683"
}
]
}
}
TASK [ec2_vpc_subnet_facts] ****************************************************
ok: [localhost]
TASK [display subnets] *********************************************************
ok: [localhost] => {
"msg": {
"changed": false,
"subnets": [
{
"availability_zone": "eu-west-1b",
"available_ip_address_count": 4091,
"cidr_block": "172.31.16.0/20",
"default_for_az": "true",
"id": "subnet-3ac33c4c",
"map_public_ip_on_launch": "true",
"state": "available",
"tags": {},
"vpc_id": "vpc-e749a683"
},
{
"availability_zone": "eu-west-1c",
"available_ip_address_count": 4091,
"cidr_block": "172.31.32.0/20",
"default_for_az": "true",
"id": "subnet-4efbef17",
"map_public_ip_on_launch": "true",
"state": "available",
"tags": {},
"vpc_id": "vpc-e749a683"
},
{
"availability_zone": "eu-west-1a",
"available_ip_address_count": 4091,
"cidr_block": "172.31.0.0/20",
"default_for_az": "true",
"id": "subnet-9a3deafe",
"map_public_ip_on_launch": "true",
"state": "available",
"tags": {},
"vpc_id": "vpc-e749a683"
}
]
}
}
The only way I can see to make this work is to include tags on the route with a sensible value (like the subnet ID). But that would still need someone to manually keep it up to date, there is no way to automate that from Ansible, is there?
I think a well placed filter plugin can help you in this situation. For instance if you know the route table id, you can write a simple python function to retrieve a list of subnet_ids associated with the route table id. You will put this filter in the root of your ansible directory.
Example below.... filter_plugins/example.py
If you want to use this filter, it would be as simple as doing the following..
It looks like this is fixed as of Ansible 2.3 (https://github.com/ansible/ansible/commit/90002e06ae0ab255d71e6976d7e5d23e93850cd3).
Now when you call
ec2_vpc_route_table_facts
, in addition to aroutes
list, each returned route table also has anassociations
list, which contains any associated subnet_id(s)