# ec2\_vpc\_route\_table and gateways vs peering

**URL:** <https://forum.ansible.com/t/ec2-vpc-route-table-and-gateways-vs-peering/31017>\
**Category:** Ansible Project\
**Created:** [August 10, 2019, 6:20pm UTC](https://forum.ansible.com/t/ec2-vpc-route-table-and-gateways-vs-peering/31017 "2019-08-10T18:20:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Barry\_Kaplan](https://avatars.discourse-cdn.com/v4/letter/b/48db29/32.png) [@Barry\_Kaplan](https://forum.ansible.com/u/Barry_Kaplan)\
**Post date:** [August 10, 2019, 6:20pm UTC](https://forum.ansible.com/t/ec2-vpc-route-table-and-gateways-vs-peering/31017/1 "2019-08-10T18:20:29Z")

</div>

It seems that ec2\_vpc\_route\_table will not purge igw or nat gateways even if purge=true, which is very nice. But it DOES purge routes associated with peering. Shouldn’t peering routes be treated in the same as gateways?

---

<div class="post-metadata">

**Author:** ![Barry\_Kaplan](https://avatars.discourse-cdn.com/v4/letter/b/48db29/32.png) [@Barry\_Kaplan](https://forum.ansible.com/u/Barry_Kaplan)\
**Post date:** [August 10, 2019, 9:52pm UTC](https://forum.ansible.com/t/ec2-vpc-route-table-and-gateways-vs-peering/31017/2 "2019-08-10T21:52:05Z")

</div>

My assumption was incorrect. The module does not make any special considerations for gateways.

Which leaves the problem of retaining certain routes without having to use purge=false. I don’t want to use purge=false because that leaves us to manually clean up the routes. And we need to add routes after the table was created for things like vpc peering. But don’t want the peering routes to get purged if when ec2\_vpc\_route\_table is run again. A bit of circular mess.

What I am doing now is something like this:

`

- name: get existing public routes  
ec2\_vpc\_route\_table\_facts:  
filters:  
“tag:Name”: “{{ ec2\_vpc\_name }}-public”  
register: \_\_public\_route\_table\_routes

- assert:  
that: (\_\_public\_route\_table\_routes.route\_tables | length) \<= 1

- name: create public route table

vars:  
subnets: “{{ \_\_public\_subnets.results | map(attribute=‘subnet.id’) | list }}”  
public\_routes: “{{ \_\_public\_route\_table\_routes.route\_tables[0] | default({‘routes’:}) }}”  
igw\_routes:

- dest: 0.0.0.0/0  
gateway\_id: “{{ \_\_igw.gateway\_id }}”  
peering\_routes: “{{ public\_routes.routes | peering\_routes\_spec() | list }}”  
routes: “{{ igw\_routes | union(peering\_routes) }}”  
ec2\_vpc\_route\_table:  
vpc\_id: “{{ \_\_vpc.vpc.id }}”  
region: “{{ ec2\_region }}”  
tags:  
Name: “{{ ec2\_vpc\_name }}-public”  
cost: “{{ env | cost\_tag }}”  
env: “{{ env }}”  
managed\_by: ansible  
route\_table\_type: public  
subnets: “{{ subnets }}”  
routes: “{{ routes }}”  
register: \_\_public\_route\_table  
`

Where peering\_routes\_spec() is

`def peering_routes_spec(items): results = [] for item in items: vpc_peering_connection_id = item['vpc_peering_connection_id'] if vpc_peering_connection_id != None and vpc_peering_connection_id.startswith('pcx-'): results.append({"dest":item['destination_cidr_block'], "vpc_peering_connection_id": vpc_peering_connection_id}) return results`

What would be really nice is if the module support some type of excludes for the purging.

---

<div class="post-metadata">

**Author:** ![Barry\_Kaplan](https://avatars.discourse-cdn.com/v4/letter/b/48db29/32.png) [@Barry\_Kaplan](https://forum.ansible.com/u/Barry_Kaplan)\
**Post date:** [August 10, 2019, 10:25pm UTC](https://forum.ansible.com/t/ec2-vpc-route-table-and-gateways-vs-peering/31017/3 "2019-08-10T22:25:27Z")

</div>

Something like this in `ensure_routes` where it collects the routes to purge

`  
if purge\_routes\_exclude\_pcx and r.get(‘VpcPeeringConnectionId’) and r[‘VpcPeeringConnectionId’].startswith(‘pcx-’):  
continue  
if purge\_routes\_exclude\_igw and r.get(‘GatewayId’) and r[‘GatewayId’].startswith(‘igw-’):  
continue  
if purge\_routes\_exclude\_nat and r.get(‘GatewayId’) and r[‘GatewayId’].startswith(‘nat-’):  
continue

`
