mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-25 06:10:22 -07:00
Disassociate EC2 VPC subnets from route tables before deletion (#20114)
* Disassociate subnets from route tables before deletion If a route table still has subnets associated with it, it will fail to delete: ``` "msg": "The routeTable 'rtb-abcd1234' has dependencies and cannot be deleted." ``` Avoid this by disassociating subnets before route table deletion * Fix ec2_vpc_route_table flake8 complaints
This commit is contained in:
parent
950ff3f24a
commit
da1c13705d
1 changed files with 17 additions and 10 deletions
|
@ -13,6 +13,12 @@
|
||||||
# You should have received a copy of the GNU General Public License
|
# You should have received a copy of the GNU General Public License
|
||||||
# along with this library. If not, see <http://www.gnu.org/licenses/>.
|
# along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
import re
|
||||||
|
|
||||||
|
from ansible.module_utils.basic import AnsibleModule
|
||||||
|
from ansible.module_utils.ec2 import AnsibleAWSError, connect_to_aws, ec2_argument_spec, get_aws_connection_info
|
||||||
|
|
||||||
|
|
||||||
ANSIBLE_METADATA = {'status': ['stableinterface'],
|
ANSIBLE_METADATA = {'status': ['stableinterface'],
|
||||||
'supported_by': 'committer',
|
'supported_by': 'committer',
|
||||||
'version': '1.0'}
|
'version': '1.0'}
|
||||||
|
@ -28,7 +34,9 @@ author: Robert Estelle (@erydo), Rob White (@wimnat)
|
||||||
options:
|
options:
|
||||||
lookup:
|
lookup:
|
||||||
description:
|
description:
|
||||||
- "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."
|
- "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
|
required: false
|
||||||
default: tag
|
default: tag
|
||||||
choices: [ 'tag', 'id' ]
|
choices: [ 'tag', 'id' ]
|
||||||
|
@ -62,7 +70,8 @@ options:
|
||||||
required: true
|
required: true
|
||||||
tags:
|
tags:
|
||||||
description:
|
description:
|
||||||
- "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."
|
- "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."
|
||||||
required: false
|
required: false
|
||||||
default: null
|
default: null
|
||||||
aliases: [ "resource_tags" ]
|
aliases: [ "resource_tags" ]
|
||||||
|
@ -111,8 +120,6 @@ EXAMPLES = '''
|
||||||
|
|
||||||
'''
|
'''
|
||||||
|
|
||||||
import re
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import boto.ec2
|
import boto.ec2
|
||||||
import boto.vpc
|
import boto.vpc
|
||||||
|
@ -123,9 +130,6 @@ except ImportError:
|
||||||
if __name__ != '__main__':
|
if __name__ != '__main__':
|
||||||
raise
|
raise
|
||||||
|
|
||||||
from ansible.module_utils.basic import AnsibleModule
|
|
||||||
from ansible.module_utils.ec2 import AnsibleAWSError, connect_to_aws, ec2_argument_spec, get_aws_connection_info
|
|
||||||
|
|
||||||
|
|
||||||
class AnsibleRouteTableException(Exception):
|
class AnsibleRouteTableException(Exception):
|
||||||
pass
|
pass
|
||||||
|
@ -142,6 +146,7 @@ class AnsibleTagCreationException(AnsibleRouteTableException):
|
||||||
class AnsibleSubnetSearchException(AnsibleRouteTableException):
|
class AnsibleSubnetSearchException(AnsibleRouteTableException):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
CIDR_RE = re.compile('^(\d{1,3}\.){3}\d{1,3}\/\d{1,2}$')
|
CIDR_RE = re.compile('^(\d{1,3}\.){3}\d{1,3}\/\d{1,2}$')
|
||||||
SUBNET_RE = re.compile('^subnet-[A-z0-9]+$')
|
SUBNET_RE = re.compile('^subnet-[A-z0-9]+$')
|
||||||
ROUTE_TABLE_RE = re.compile('^rtb-[A-z0-9]+$')
|
ROUTE_TABLE_RE = re.compile('^rtb-[A-z0-9]+$')
|
||||||
|
@ -218,7 +223,7 @@ def find_igw(vpc_conn, vpc_id):
|
||||||
|
|
||||||
if not igw:
|
if not igw:
|
||||||
raise AnsibleIgwSearchException('No IGW found for VPC {0}'.
|
raise AnsibleIgwSearchException('No IGW found for VPC {0}'.
|
||||||
format(vpc_id))
|
format(vpc_id))
|
||||||
elif len(igw) == 1:
|
elif len(igw) == 1:
|
||||||
return igw[0].id
|
return igw[0].id
|
||||||
else:
|
else:
|
||||||
|
@ -266,6 +271,7 @@ def get_route_table_by_id(vpc_conn, vpc_id, route_table_id):
|
||||||
|
|
||||||
return route_table
|
return route_table
|
||||||
|
|
||||||
|
|
||||||
def get_route_table_by_tags(vpc_conn, vpc_id, tags):
|
def get_route_table_by_tags(vpc_conn, vpc_id, tags):
|
||||||
|
|
||||||
count = 0
|
count = 0
|
||||||
|
@ -462,6 +468,8 @@ def ensure_route_table_absent(connection, module):
|
||||||
if route_table is None:
|
if route_table is None:
|
||||||
return {'changed': False}
|
return {'changed': False}
|
||||||
|
|
||||||
|
# disassociate subnets before deleting route table
|
||||||
|
ensure_subnet_associations(connection, vpc_id, route_table, [], module.check_mode)
|
||||||
try:
|
try:
|
||||||
connection.delete_route_table(route_table.id, dry_run=module.check_mode)
|
connection.delete_route_table(route_table.id, dry_run=module.check_mode)
|
||||||
except EC2ResponseError as e:
|
except EC2ResponseError as e:
|
||||||
|
@ -483,8 +491,7 @@ def get_route_table_info(route_table):
|
||||||
route_table_info = { 'id': route_table.id,
|
route_table_info = { 'id': route_table.id,
|
||||||
'routes': routes,
|
'routes': routes,
|
||||||
'tags': route_table.tags,
|
'tags': route_table.tags,
|
||||||
'vpc_id': route_table.vpc_id
|
'vpc_id': route_table.vpc_id }
|
||||||
}
|
|
||||||
|
|
||||||
return route_table_info
|
return route_table_info
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue