2014-11-14 01:38:52 +01:00
#!/usr/bin/python
#
2015-06-11 06:07:04 +02:00
# This is a free software: you can redistribute it and/or modify
2014-11-14 01:38:52 +01:00
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
2015-06-11 06:07:04 +02:00
# This Ansible library is distributed in the hope that it will be useful,
2014-11-14 01:38:52 +01:00
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
2015-06-11 06:07:04 +02:00
# along with this library. If not, see <http://www.gnu.org/licenses/>.
2014-11-14 01:38:52 +01:00
DOCUMENTATION = '''
- - -
module : ec2_vpc_route_table
2015-06-11 06:07:04 +02:00
short_description : Manage route tables for AWS virtual private clouds
2014-11-14 01:38:52 +01:00
description :
2015-06-11 06:07:04 +02:00
- Manage route tables for AWS virtual private clouds
version_added : " 2.0 "
2015-08-30 22:25:05 +02:00
author : Robert Estelle ( @erydo ) , Rob White ( @wimnat )
2014-11-14 01:38:52 +01:00
options :
2015-08-30 22:25:05 +02:00
lookup :
2014-11-14 01:38:52 +01:00
description :
2015-08-30 22:25:05 +02:00
- " Look up route table by either tags or by route table ID. Non-unique tag lookup will fail. If no tags are specifed then no lookup for an existing route table is performed and a new route table will be created. To change tags of a route table, you must look up by id. "
required : false
default : tag
choices : [ ' tag ' , ' id ' ]
propagating_vgw_ids :
2014-11-14 01:38:52 +01:00
description :
2015-08-30 22:25:05 +02:00
- " Enable route propagation from virtual gateways specified by ID. "
2015-10-14 20:14:33 +02:00
default : None
2014-11-14 01:38:52 +01:00
required : false
2015-08-30 22:25:05 +02:00
route_table_id :
2014-11-14 01:38:52 +01:00
description :
2015-08-30 22:25:05 +02:00
- " The ID of the route table to update or delete. "
2014-11-14 01:38:52 +01:00
required : false
default : null
routes :
description :
2016-07-25 21:48:27 +02:00
- " List of routes in the route table.
Routes are specified as dicts containing the keys ' dest ' and one of ' gateway_id ' ,
' instance_id ' , ' interface_id ' , or ' vpc_peering_connection_id ' .
If ' gateway_id ' is specified , you can refer to the VPC ' s IGW by using the value ' igw ' . Routes are required for present states. "
required : false
default : None
2015-08-30 22:25:05 +02:00
state :
description :
- " Create or destroy the VPC route table "
required : false
default : present
choices : [ ' present ' , ' absent ' ]
2014-11-14 01:38:52 +01:00
subnets :
description :
2015-08-30 22:25:05 +02:00
- " An array of subnets to add to this route table. Subnets may be specified by either subnet ID, Name tag, or by a CIDR such as ' 10.0.0.0/24 ' . "
2014-11-14 01:38:52 +01:00
required : true
2015-08-30 22:25:05 +02:00
tags :
2014-12-01 21:56:04 +01:00
description :
2015-09-21 21:28:42 +02:00
- " A dictionary of resource tags of the form: { tag1: value1, tag2: value2 }. Tags are used to uniquely identify route tables within a VPC when the route_table_id is not supplied. "
2014-12-01 21:56:04 +01:00
required : false
2015-08-30 22:25:05 +02:00
default : null
aliases : [ " resource_tags " ]
vpc_id :
2014-11-14 01:38:52 +01:00
description :
2015-08-30 22:25:05 +02:00
- " VPC ID of the VPC in which to create the route table. "
required : true
2015-10-08 05:17:50 +02:00
extends_documentation_fragment :
- aws
- ec2
2014-11-14 01:38:52 +01:00
'''
EXAMPLES = '''
2015-06-11 06:07:04 +02:00
# Note: These examples do not set authentication details, see the AWS Guide for details.
2014-11-14 01:38:52 +01:00
# Basic creation example:
- name : Set up public subnet route table
2015-08-30 22:25:05 +02:00
ec2_vpc_route_table :
2014-11-14 01:38:52 +01:00
vpc_id : vpc - 1245678
region : us - west - 1
2015-08-30 22:25:05 +02:00
tags :
2014-11-14 01:38:52 +01:00
Name : Public
subnets :
2015-12-07 18:15:24 +01:00
- " {{ jumpbox_subnet.subnet.id }} "
- " {{ frontend_subnet.subnet.id }} "
2015-08-30 22:25:05 +02:00
- " {{ vpn_subnet.subnet_id }} "
2014-11-14 01:38:52 +01:00
routes :
- dest : 0.0 .0 .0 / 0
2015-08-30 22:25:05 +02:00
gateway_id : " {{ igw.gateway_id }} "
2014-11-14 01:38:52 +01:00
register : public_route_table
- name : Set up NAT - protected route table
2015-08-30 22:25:05 +02:00
ec2_vpc_route_table :
2014-11-14 01:38:52 +01:00
vpc_id : vpc - 1245678
region : us - west - 1
2015-08-30 22:25:05 +02:00
tags :
2015-09-21 21:28:42 +02:00
Name : Internal
2014-11-14 01:38:52 +01:00
subnets :
2015-12-07 18:15:24 +01:00
- " {{ application_subnet.subnet.id }} "
2014-12-05 04:10:49 +01:00
- ' Database Subnet '
- ' 10.0.0.0/8 '
2014-11-14 01:38:52 +01:00
routes :
- dest : 0.0 .0 .0 / 0
2015-08-30 22:25:05 +02:00
instance_id : " {{ nat.instance_id }} "
2014-11-14 01:38:52 +01:00
register : nat_route_table
2015-12-01 04:01:57 +01:00
2014-11-14 01:38:52 +01:00
'''
2014-12-01 19:45:50 +01:00
import sys # noqa
2014-12-05 04:10:49 +01:00
import re
2014-11-14 01:38:52 +01:00
try :
import boto . ec2
import boto . vpc
from boto . exception import EC2ResponseError
2014-12-01 19:45:50 +01:00
HAS_BOTO = True
2014-11-14 01:38:52 +01:00
except ImportError :
2014-12-01 19:45:50 +01:00
HAS_BOTO = False
if __name__ != ' __main__ ' :
raise
2014-11-14 01:38:52 +01:00
2014-12-01 19:41:03 +01:00
class AnsibleRouteTableException ( Exception ) :
2014-11-14 01:38:52 +01:00
pass
2015-06-11 06:01:40 +02:00
class AnsibleIgwSearchException ( AnsibleRouteTableException ) :
pass
2014-12-01 19:41:03 +01:00
class AnsibleTagCreationException ( AnsibleRouteTableException ) :
2014-11-14 01:38:52 +01:00
pass
2014-12-05 04:10:49 +01:00
class AnsibleSubnetSearchException ( AnsibleRouteTableException ) :
pass
CIDR_RE = re . compile ( ' ^( \ d { 1,3} \ .) {3} \ d { 1,3} \ / \ d { 1,2}$ ' )
SUBNET_RE = re . compile ( ' ^subnet-[A-z0-9]+$ ' )
ROUTE_TABLE_RE = re . compile ( ' ^rtb-[A-z0-9]+$ ' )
def find_subnets ( vpc_conn , vpc_id , identified_subnets ) :
"""
Finds a list of subnets , each identified either by a raw ID , a unique
' Name ' tag , or a CIDR such as 10.0 .0 .0 / 8.
Note that this function is duplicated in other ec2 modules , and should
potentially be moved into potentially be moved into a shared module_utils
"""
subnet_ids = [ ]
subnet_names = [ ]
subnet_cidrs = [ ]
for subnet in ( identified_subnets or [ ] ) :
if re . match ( SUBNET_RE , subnet ) :
subnet_ids . append ( subnet )
elif re . match ( CIDR_RE , subnet ) :
subnet_cidrs . append ( subnet )
else :
subnet_names . append ( subnet )
subnets_by_id = [ ]
if subnet_ids :
subnets_by_id = vpc_conn . get_all_subnets (
subnet_ids , filters = { ' vpc_id ' : vpc_id } )
for subnet_id in subnet_ids :
if not any ( s . id == subnet_id for s in subnets_by_id ) :
raise AnsibleSubnetSearchException (
' Subnet ID " {0} " does not exist ' . format ( subnet_id ) )
subnets_by_cidr = [ ]
if subnet_cidrs :
subnets_by_cidr = vpc_conn . get_all_subnets (
filters = { ' vpc_id ' : vpc_id , ' cidr ' : subnet_cidrs } )
for cidr in subnet_cidrs :
if not any ( s . cidr_block == cidr for s in subnets_by_cidr ) :
raise AnsibleSubnetSearchException (
' Subnet CIDR " {0} " does not exist ' . format ( subnet_cidr ) )
subnets_by_name = [ ]
if subnet_names :
subnets_by_name = vpc_conn . get_all_subnets (
filters = { ' vpc_id ' : vpc_id , ' tag:Name ' : subnet_names } )
for name in subnet_names :
matching = [ s . tags . get ( ' Name ' ) == name for s in subnets_by_name ]
if len ( matching ) == 0 :
raise AnsibleSubnetSearchException (
' Subnet named " {0} " does not exist ' . format ( name ) )
elif len ( matching ) > 1 :
raise AnsibleSubnetSearchException (
' Multiple subnets named " {0} " ' . format ( name ) )
return subnets_by_id + subnets_by_cidr + subnets_by_name
2015-06-11 06:01:40 +02:00
def find_igw ( vpc_conn , vpc_id ) :
"""
Finds the Internet gateway for the given VPC ID .
Raises an AnsibleIgwSearchException if either no IGW can be found , or more
than one found for the given VPC .
Note that this function is duplicated in other ec2 modules , and should
potentially be moved into potentially be moved into a shared module_utils
"""
igw = vpc_conn . get_all_internet_gateways (
filters = { ' attachment.vpc-id ' : vpc_id } )
if not igw :
2015-08-30 22:25:05 +02:00
raise AnsibleIgwSearchException ( ' No IGW found for VPC {0} ' .
2015-06-11 06:01:40 +02:00
format ( vpc_id ) )
elif len ( igw ) == 1 :
return igw [ 0 ] . id
else :
2015-08-30 22:25:05 +02:00
raise AnsibleIgwSearchException ( ' Multiple IGWs found for VPC {0} ' .
2015-06-11 06:01:40 +02:00
format ( vpc_id ) )
2014-11-14 01:38:52 +01:00
def get_resource_tags ( vpc_conn , resource_id ) :
2014-12-05 04:10:02 +01:00
return dict ( ( t . name , t . value ) for t in
vpc_conn . get_all_tags ( filters = { ' resource-id ' : resource_id } ) )
2014-11-14 01:38:52 +01:00
2014-12-03 19:01:44 +01:00
def tags_match ( match_tags , candidate_tags ) :
return all ( ( k in candidate_tags and candidate_tags [ k ] == v
for k , v in match_tags . iteritems ( ) ) )
2014-12-01 21:18:56 +01:00
def ensure_tags ( vpc_conn , resource_id , tags , add_only , check_mode ) :
2014-11-14 01:38:52 +01:00
try :
cur_tags = get_resource_tags ( vpc_conn , resource_id )
2014-11-14 01:57:15 +01:00
if tags == cur_tags :
2014-11-14 01:38:52 +01:00
return { ' changed ' : False , ' tags ' : cur_tags }
2014-12-05 04:10:02 +01:00
to_delete = dict ( ( k , cur_tags [ k ] ) for k in cur_tags if k not in tags )
2014-11-14 01:38:52 +01:00
if to_delete and not add_only :
2014-12-01 21:18:56 +01:00
vpc_conn . delete_tags ( resource_id , to_delete , dry_run = check_mode )
2014-11-14 01:38:52 +01:00
2014-12-05 04:10:02 +01:00
to_add = dict ( ( k , tags [ k ] ) for k in tags if k not in cur_tags )
2014-11-14 01:38:52 +01:00
if to_add :
2014-12-01 21:18:56 +01:00
vpc_conn . create_tags ( resource_id , to_add , dry_run = check_mode )
2014-11-14 01:38:52 +01:00
latest_tags = get_resource_tags ( vpc_conn , resource_id )
return { ' changed ' : True , ' tags ' : latest_tags }
except EC2ResponseError as e :
2014-12-01 19:41:03 +01:00
raise AnsibleTagCreationException (
' Unable to update tags for {0} , error: {1} ' . format ( resource_id , e ) )
2014-11-14 01:38:52 +01:00
def get_route_table_by_id ( vpc_conn , vpc_id , route_table_id ) :
2015-08-30 22:25:05 +02:00
route_table = None
route_tables = vpc_conn . get_all_route_tables ( route_table_ids = [ route_table_id ] , filters = { ' vpc_id ' : vpc_id } )
if route_tables :
route_table = route_tables [ 0 ]
2015-12-01 04:01:57 +01:00
2015-08-30 22:25:05 +02:00
return route_table
2015-12-01 04:01:57 +01:00
2014-11-14 01:38:52 +01:00
def get_route_table_by_tags ( vpc_conn , vpc_id , tags ) :
2015-12-01 04:01:57 +01:00
2015-08-30 22:25:05 +02:00
count = 0
2015-12-01 04:01:57 +01:00
route_table = None
2014-12-03 19:01:44 +01:00
route_tables = vpc_conn . get_all_route_tables ( filters = { ' vpc_id ' : vpc_id } )
2015-08-30 22:25:05 +02:00
for table in route_tables :
this_tags = get_resource_tags ( vpc_conn , table . id )
2014-12-03 19:01:44 +01:00
if tags_match ( tags , this_tags ) :
2015-08-30 22:25:05 +02:00
route_table = table
count + = 1
2015-12-01 04:01:57 +01:00
2015-08-30 22:25:05 +02:00
if count > 1 :
raise RuntimeError ( " Tags provided do not identify a unique route table " )
2015-12-01 04:01:57 +01:00
else :
2015-08-30 22:25:05 +02:00
return route_table
2014-11-14 01:38:52 +01:00
def route_spec_matches_route ( route_spec , route ) :
key_attr_map = {
' destination_cidr_block ' : ' destination_cidr_block ' ,
' gateway_id ' : ' gateway_id ' ,
' instance_id ' : ' instance_id ' ,
' interface_id ' : ' interface_id ' ,
' vpc_peering_connection_id ' : ' vpc_peering_connection_id ' ,
}
2016-07-25 21:48:27 +02:00
# This is a workaround to catch managed NAT gateways as they do not show
# up in any of the returned values when describing route tables.
# The caveat of doing it this way is that if there was an existing
# route for another nat gateway in this route table there is not a way to
# change to another nat gateway id. Long term solution would be to utilise
# boto3 which is a very big task for this module or to update boto.
if route_spec . get ( ' gateway_id ' ) and ' nat- ' in route_spec [ ' gateway_id ' ] :
if route . destination_cidr_block == route_spec [ ' destination_cidr_block ' ] :
if all ( ( not route . gateway_id , not route . instance_id , not route . interface_id , not route . vpc_peering_connection_id ) ) :
return True
2014-11-14 01:38:52 +01:00
for k in key_attr_map . iterkeys ( ) :
if k in route_spec :
if route_spec [ k ] != getattr ( route , k ) :
return False
return True
def rename_key ( d , old_key , new_key ) :
d [ new_key ] = d [ old_key ]
del d [ old_key ]
def index_of_matching_route ( route_spec , routes_to_match ) :
for i , route in enumerate ( routes_to_match ) :
if route_spec_matches_route ( route_spec , route ) :
return i
2015-08-10 21:10:19 +02:00
def ensure_routes ( vpc_conn , route_table , route_specs , propagating_vgw_ids ,
check_mode ) :
2014-11-14 01:38:52 +01:00
routes_to_match = list ( route_table . routes )
route_specs_to_create = [ ]
for route_spec in route_specs :
i = index_of_matching_route ( route_spec , routes_to_match )
if i is None :
route_specs_to_create . append ( route_spec )
else :
del routes_to_match [ i ]
2015-08-10 21:10:19 +02:00
# NOTE: As of boto==2.38.0, the origin of a route is not available
# (for example, whether it came from a gateway with route propagation
# enabled). Testing for origin == 'EnableVgwRoutePropagation' is more
# correct than checking whether the route uses a propagating VGW.
# The current logic will leave non-propagated routes using propagating
# VGWs in place.
2016-07-25 21:48:27 +02:00
routes_to_delete = [ ]
for r in routes_to_match :
if r . gateway_id :
if r . gateway_id != ' local ' and not r . gateway_id . startswith ( ' vpce- ' ) :
if not propagating_vgw_ids or r . gateway_id not in propagating_vgw_ids :
routes_to_delete . append ( r )
else :
routes_to_delete . append ( r )
2014-11-14 01:38:52 +01:00
2016-07-25 21:48:27 +02:00
changed = bool ( routes_to_delete or route_specs_to_create )
2014-12-01 21:18:56 +01:00
if changed :
2016-07-25 21:48:27 +02:00
for route in routes_to_delete :
2015-10-14 20:14:33 +02:00
try :
2016-07-25 21:48:27 +02:00
vpc_conn . delete_route ( route_table . id ,
route . destination_cidr_block ,
dry_run = check_mode )
2015-10-14 20:14:33 +02:00
except EC2ResponseError as e :
if e . error_code == ' DryRunOperation ' :
pass
2014-11-14 01:38:52 +01:00
2016-07-25 21:48:27 +02:00
for route_spec in route_specs_to_create :
2015-10-14 20:14:33 +02:00
try :
2016-07-25 21:48:27 +02:00
vpc_conn . create_route ( route_table . id ,
dry_run = check_mode ,
* * route_spec )
2015-10-14 20:14:33 +02:00
except EC2ResponseError as e :
if e . error_code == ' DryRunOperation ' :
pass
return { ' changed ' : bool ( changed ) }
2014-11-14 01:38:52 +01:00
def ensure_subnet_association ( vpc_conn , vpc_id , route_table_id , subnet_id ,
check_mode ) :
route_tables = vpc_conn . get_all_route_tables (
filters = { ' association.subnet_id ' : subnet_id , ' vpc_id ' : vpc_id }
)
for route_table in route_tables :
if route_table . id is None :
continue
for a in route_table . associations :
if a . subnet_id == subnet_id :
if route_table . id == route_table_id :
return { ' changed ' : False , ' association_id ' : a . id }
else :
if check_mode :
return { ' changed ' : True }
vpc_conn . disassociate_route_table ( a . id )
association_id = vpc_conn . associate_route_table ( route_table_id , subnet_id )
return { ' changed ' : True , ' association_id ' : association_id }
def ensure_subnet_associations ( vpc_conn , vpc_id , route_table , subnets ,
check_mode ) :
current_association_ids = [ a . id for a in route_table . associations ]
new_association_ids = [ ]
changed = False
for subnet in subnets :
result = ensure_subnet_association (
vpc_conn , vpc_id , route_table . id , subnet . id , check_mode )
changed = changed or result [ ' changed ' ]
if changed and check_mode :
return { ' changed ' : True }
new_association_ids . append ( result [ ' association_id ' ] )
to_delete = [ a_id for a_id in current_association_ids
if a_id not in new_association_ids ]
for a_id in to_delete :
changed = True
2014-12-01 21:18:56 +01:00
vpc_conn . disassociate_route_table ( a_id , dry_run = check_mode )
2014-11-14 01:38:52 +01:00
return { ' changed ' : changed }
2015-08-10 21:25:13 +02:00
def ensure_propagation ( vpc_conn , route_table , propagating_vgw_ids ,
2014-12-01 21:56:04 +01:00
check_mode ) :
2015-08-10 21:25:13 +02:00
# NOTE: As of boto==2.38.0, it is not yet possible to query the existing
# propagating gateways. However, EC2 does support this as shown in its API
# documentation. For now, a reasonable proxy for this is the presence of
# propagated routes using the gateway in the route table. If such a route
# is found, propagation is almost certainly enabled.
2014-12-01 21:56:04 +01:00
changed = False
for vgw_id in propagating_vgw_ids :
2015-08-10 21:25:13 +02:00
for r in list ( route_table . routes ) :
if r . gateway_id == vgw_id :
return { ' changed ' : False }
2015-08-10 20:31:46 +02:00
changed = True
2015-08-10 21:25:13 +02:00
vpc_conn . enable_vgw_route_propagation ( route_table . id ,
2015-08-10 20:31:46 +02:00
vgw_id ,
dry_run = check_mode )
2014-12-01 21:56:04 +01:00
return { ' changed ' : changed }
2015-08-30 22:25:05 +02:00
def ensure_route_table_absent ( connection , module ) :
lookup = module . params . get ( ' lookup ' )
route_table_id = module . params . get ( ' route_table_id ' )
tags = module . params . get ( ' tags ' )
vpc_id = module . params . get ( ' vpc_id ' )
if lookup == ' tag ' :
if tags is not None :
try :
route_table = get_route_table_by_tags ( connection , vpc_id , tags )
except EC2ResponseError as e :
module . fail_json ( msg = e . message )
except RuntimeError as e :
module . fail_json ( msg = e . args [ 0 ] )
else :
route_table = None
elif lookup == ' id ' :
try :
route_table = get_route_table_by_id ( connection , vpc_id , route_table_id )
except EC2ResponseError as e :
module . fail_json ( msg = e . message )
2014-11-14 01:38:52 +01:00
if route_table is None :
return { ' changed ' : False }
2015-08-30 22:25:05 +02:00
try :
2015-10-14 20:14:33 +02:00
connection . delete_route_table ( route_table . id , dry_run = module . check_mode )
2015-08-30 22:25:05 +02:00
except EC2ResponseError as e :
2015-10-14 20:14:33 +02:00
if e . error_code == ' DryRunOperation ' :
pass
else :
module . fail_json ( msg = e . message )
2015-08-30 22:25:05 +02:00
2014-11-14 01:38:52 +01:00
return { ' changed ' : True }
2015-08-30 22:25:05 +02:00
def get_route_table_info ( route_table ) :
# Add any routes to array
routes = [ ]
for route in route_table . routes :
routes . append ( route . __dict__ )
route_table_info = { ' id ' : route_table . id ,
' routes ' : routes ,
' tags ' : route_table . tags ,
' vpc_id ' : route_table . vpc_id
}
return route_table_info
2016-07-25 21:48:27 +02:00
def create_route_spec ( connection , module , vpc_id ) :
routes = module . params . get ( ' routes ' )
2015-08-30 22:25:05 +02:00
for route_spec in routes :
rename_key ( route_spec , ' dest ' , ' destination_cidr_block ' )
2016-07-25 21:48:27 +02:00
if route_spec . get ( ' gateway_id ' ) and route_spec [ ' gateway_id ' ] . lower ( ) == ' igw ' :
2015-08-30 22:25:05 +02:00
igw = find_igw ( connection , vpc_id )
route_spec [ ' gateway_id ' ] = igw
return routes
2016-07-25 21:48:27 +02:00
2015-08-30 22:25:05 +02:00
def ensure_route_table_present ( connection , module ) :
2015-12-01 04:01:57 +01:00
2015-08-30 22:25:05 +02:00
lookup = module . params . get ( ' lookup ' )
2015-10-14 20:14:33 +02:00
propagating_vgw_ids = module . params . get ( ' propagating_vgw_ids ' )
2015-08-30 22:25:05 +02:00
route_table_id = module . params . get ( ' route_table_id ' )
subnets = module . params . get ( ' subnets ' )
tags = module . params . get ( ' tags ' )
vpc_id = module . params . get ( ' vpc_id ' )
try :
2016-07-25 21:48:27 +02:00
routes = create_route_spec ( connection , module , vpc_id )
2015-08-30 22:25:05 +02:00
except AnsibleIgwSearchException as e :
module . fail_json ( msg = e [ 0 ] )
2015-12-01 04:01:57 +01:00
2014-11-14 01:38:52 +01:00
changed = False
tags_valid = False
2015-08-30 22:25:05 +02:00
if lookup == ' tag ' :
if tags is not None :
try :
route_table = get_route_table_by_tags ( connection , vpc_id , tags )
except EC2ResponseError as e :
module . fail_json ( msg = e . message )
except RuntimeError as e :
module . fail_json ( msg = e . args [ 0 ] )
else :
route_table = None
elif lookup == ' id ' :
try :
route_table = get_route_table_by_id ( connection , vpc_id , route_table_id )
except EC2ResponseError as e :
module . fail_json ( msg = e . message )
2015-12-01 04:01:57 +01:00
2015-08-30 22:25:05 +02:00
# If no route table returned then create new route table
2014-11-14 01:38:52 +01:00
if route_table is None :
2015-08-30 22:25:05 +02:00
try :
2015-10-09 03:53:26 +02:00
route_table = connection . create_route_table ( vpc_id , module . check_mode )
2015-08-30 22:25:05 +02:00
changed = True
2015-10-09 03:53:26 +02:00
except EC2ResponseError as e :
if e . error_code == ' DryRunOperation ' :
module . exit_json ( changed = True )
2015-08-30 22:25:05 +02:00
module . fail_json ( msg = e . message )
2015-12-01 04:01:57 +01:00
2015-08-30 22:25:05 +02:00
if routes is not None :
2014-11-14 01:38:52 +01:00
try :
2015-10-14 20:14:33 +02:00
result = ensure_routes ( connection , route_table , routes , propagating_vgw_ids , module . check_mode )
2015-08-30 22:25:05 +02:00
changed = changed or result [ ' changed ' ]
2014-11-14 01:38:52 +01:00
except EC2ResponseError as e :
2015-08-30 22:25:05 +02:00
module . fail_json ( msg = e . message )
2014-11-14 01:38:52 +01:00
2014-12-01 21:56:04 +01:00
if propagating_vgw_ids is not None :
2015-09-02 15:34:56 +02:00
result = ensure_propagation ( connection , route_table ,
2014-12-01 21:56:04 +01:00
propagating_vgw_ids ,
2015-10-14 20:14:33 +02:00
check_mode = module . check_mode )
2014-12-01 21:56:04 +01:00
changed = changed or result [ ' changed ' ]
2015-08-30 22:25:05 +02:00
if not tags_valid and tags is not None :
result = ensure_tags ( connection , route_table . id , tags ,
2015-10-14 20:14:33 +02:00
add_only = True , check_mode = module . check_mode )
2014-11-14 01:38:52 +01:00
changed = changed or result [ ' changed ' ]
if subnets :
associated_subnets = [ ]
try :
2015-08-30 22:25:05 +02:00
associated_subnets = find_subnets ( connection , vpc_id , subnets )
2014-11-14 01:38:52 +01:00
except EC2ResponseError as e :
2014-12-01 19:41:03 +01:00
raise AnsibleRouteTableException (
2014-11-14 01:38:52 +01:00
' Unable to find subnets for route table {0} , error: {1} '
. format ( route_table , e )
)
try :
2015-10-14 20:14:33 +02:00
result = ensure_subnet_associations ( connection , vpc_id , route_table , associated_subnets , module . check_mode )
2014-11-14 01:38:52 +01:00
changed = changed or result [ ' changed ' ]
except EC2ResponseError as e :
2014-12-01 19:41:03 +01:00
raise AnsibleRouteTableException (
2014-11-14 01:38:52 +01:00
' Unable to associate subnets for route table {0} , error: {1} '
. format ( route_table , e )
)
2015-08-30 22:25:05 +02:00
module . exit_json ( changed = changed , route_table = get_route_table_info ( route_table ) )
2014-11-14 01:38:52 +01:00
def main ( ) :
argument_spec = ec2_argument_spec ( )
2015-06-19 01:56:50 +02:00
argument_spec . update (
dict (
2015-08-30 22:25:05 +02:00
lookup = dict ( default = ' tag ' , required = False , choices = [ ' tag ' , ' id ' ] ) ,
2015-06-19 01:56:50 +02:00
propagating_vgw_ids = dict ( default = None , required = False , type = ' list ' ) ,
2015-08-30 22:25:05 +02:00
route_table_id = dict ( default = None , required = False ) ,
2016-07-25 21:48:27 +02:00
routes = dict ( default = [ ] , required = False , type = ' list ' ) ,
2015-08-30 22:25:05 +02:00
state = dict ( default = ' present ' , choices = [ ' present ' , ' absent ' ] ) ,
2015-06-19 01:56:50 +02:00
subnets = dict ( default = None , required = False , type = ' list ' ) ,
2015-08-30 22:25:05 +02:00
tags = dict ( default = None , required = False , type = ' dict ' , aliases = [ ' resource_tags ' ] ) ,
vpc_id = dict ( default = None , required = True )
2015-06-19 01:56:50 +02:00
)
2014-11-14 01:38:52 +01:00
)
2015-12-01 04:01:57 +01:00
2015-06-19 01:56:50 +02:00
module = AnsibleModule ( argument_spec = argument_spec , supports_check_mode = True )
2015-12-01 04:01:57 +01:00
2014-12-01 19:45:50 +01:00
if not HAS_BOTO :
module . fail_json ( msg = ' boto is required for this module ' )
2014-11-14 01:38:52 +01:00
2015-06-19 01:56:50 +02:00
region , ec2_url , aws_connect_params = get_aws_connection_info ( module )
2015-12-01 04:01:57 +01:00
2015-06-19 01:56:50 +02:00
if region :
try :
connection = connect_to_aws ( boto . vpc , region , * * aws_connect_params )
2015-12-01 04:01:57 +01:00
except ( boto . exception . NoAuthHandlerFound , AnsibleAWSError ) , e :
2015-06-19 01:56:50 +02:00
module . fail_json ( msg = str ( e ) )
else :
module . fail_json ( msg = " region must be specified " )
2014-11-14 01:38:52 +01:00
2015-08-30 22:25:05 +02:00
lookup = module . params . get ( ' lookup ' )
2014-11-14 01:38:52 +01:00
route_table_id = module . params . get ( ' route_table_id ' )
state = module . params . get ( ' state ' , ' present ' )
2015-08-30 22:25:05 +02:00
if lookup == ' id ' and route_table_id is None :
module . fail_json ( " You must specify route_table_id if lookup is set to id " )
2014-11-14 01:38:52 +01:00
try :
if state == ' present ' :
2015-08-30 22:25:05 +02:00
result = ensure_route_table_present ( connection , module )
2014-11-14 01:38:52 +01:00
elif state == ' absent ' :
2015-08-30 22:25:05 +02:00
result = ensure_route_table_absent ( connection , module )
2014-12-01 19:41:03 +01:00
except AnsibleRouteTableException as e :
2014-11-14 01:38:52 +01:00
module . fail_json ( msg = str ( e ) )
module . exit_json ( * * result )
from ansible . module_utils . basic import * # noqa
from ansible . module_utils . ec2 import * # noqa
if __name__ == ' __main__ ' :
main ( )