Convert ec2_vpc_subnet module to boto3 and add map_public option. (#23783)
This commit is contained in:
parent
338bf0fd3f
commit
7bb3467db9
1 changed files with 101 additions and 97 deletions
|
@ -25,7 +25,8 @@ short_description: Manage subnets in AWS virtual private clouds
|
||||||
description:
|
description:
|
||||||
- Manage subnets in AWS virtual private clouds
|
- Manage subnets in AWS virtual private clouds
|
||||||
version_added: "2.0"
|
version_added: "2.0"
|
||||||
author: Robert Estelle (@erydo)
|
author: Robert Estelle (@erydo), Brad Davidson (@brandond)
|
||||||
|
requirements: [ boto3 ]
|
||||||
options:
|
options:
|
||||||
az:
|
az:
|
||||||
description:
|
description:
|
||||||
|
@ -54,6 +55,12 @@ options:
|
||||||
- "VPC ID of the VPC in which to create the subnet."
|
- "VPC ID of the VPC in which to create the subnet."
|
||||||
required: false
|
required: false
|
||||||
default: null
|
default: null
|
||||||
|
map_public:
|
||||||
|
description:
|
||||||
|
- "Specify true to indicate that instances launched into the subnet should be assigned public IP address by default."
|
||||||
|
required: false
|
||||||
|
default: false
|
||||||
|
version_added: "2.4"
|
||||||
extends_documentation_fragment:
|
extends_documentation_fragment:
|
||||||
- aws
|
- aws
|
||||||
- ec2
|
- ec2
|
||||||
|
@ -82,119 +89,115 @@ EXAMPLES = '''
|
||||||
import time
|
import time
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import boto.ec2
|
import boto3
|
||||||
import boto.vpc
|
from botocore.exceptions import ClientError, NoCredentialsError
|
||||||
from boto.exception import EC2ResponseError
|
HAS_BOTO3 = True
|
||||||
HAS_BOTO = True
|
|
||||||
except ImportError:
|
except ImportError:
|
||||||
HAS_BOTO = False
|
HAS_BOTO3 = False
|
||||||
if __name__ != '__main__':
|
|
||||||
raise
|
|
||||||
|
|
||||||
from ansible.module_utils.basic import AnsibleModule
|
from ansible.module_utils.basic import AnsibleModule
|
||||||
from ansible.module_utils.ec2 import AnsibleAWSError, connect_to_aws, ec2_argument_spec, get_aws_connection_info
|
from ansible.module_utils.ec2 import (ansible_dict_to_boto3_filter_list,
|
||||||
|
ansible_dict_to_boto3_tag_list,
|
||||||
|
ec2_argument_spec, boto3_conn,
|
||||||
class AnsibleVPCSubnetException(Exception):
|
boto3_tag_list_to_ansible_dict,
|
||||||
pass
|
camel_dict_to_snake_dict,
|
||||||
|
get_aws_connection_info)
|
||||||
|
|
||||||
class AnsibleVPCSubnetCreationException(AnsibleVPCSubnetException):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
class AnsibleVPCSubnetDeletionException(AnsibleVPCSubnetException):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
class AnsibleTagCreationException(AnsibleVPCSubnetException):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
def get_subnet_info(subnet):
|
def get_subnet_info(subnet):
|
||||||
|
if 'Subnets' in subnet:
|
||||||
|
return [get_subnet_info(s) for s in subnet['Subnets']]
|
||||||
|
elif 'Subnet' in subnet:
|
||||||
|
subnet = camel_dict_to_snake_dict(subnet['Subnet'])
|
||||||
|
else:
|
||||||
|
subnet = camel_dict_to_snake_dict(subnet)
|
||||||
|
|
||||||
subnet_info = {'id': subnet.id,
|
if 'tags' in subnet:
|
||||||
'availability_zone': subnet.availability_zone,
|
subnet['tags'] = boto3_tag_list_to_ansible_dict(subnet['tags'])
|
||||||
'available_ip_address_count': subnet.available_ip_address_count,
|
else:
|
||||||
'cidr_block': subnet.cidr_block,
|
subnet['tags'] = dict()
|
||||||
'default_for_az': subnet.defaultForAz,
|
|
||||||
'map_public_ip_on_launch': subnet.mapPublicIpOnLaunch,
|
|
||||||
'state': subnet.state,
|
|
||||||
'tags': subnet.tags,
|
|
||||||
'vpc_id': subnet.vpc_id
|
|
||||||
}
|
|
||||||
|
|
||||||
return subnet_info
|
if 'subnet_id' in subnet:
|
||||||
|
subnet['id'] = subnet['subnet_id']
|
||||||
|
del subnet['subnet_id']
|
||||||
|
|
||||||
|
return subnet
|
||||||
|
|
||||||
|
|
||||||
def subnet_exists(vpc_conn, subnet_id):
|
def subnet_exists(conn, subnet_id):
|
||||||
filters = {'subnet-id': subnet_id}
|
filters = ansible_dict_to_boto3_filter_list({'subnet-id': subnet_id})
|
||||||
subnet = vpc_conn.get_all_subnets(filters=filters)
|
subnets = get_subnet_info(conn.describe_subnets(Filters=filters))
|
||||||
if subnet and subnet[0].state == "available":
|
if len(subnets) > 0 and 'state' in subnets[0] and subnets[0]['state'] == "available":
|
||||||
return subnet[0]
|
return subnets[0]
|
||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def create_subnet(vpc_conn, vpc_id, cidr, az, check_mode):
|
def create_subnet(conn, module, vpc_id, cidr, az, check_mode):
|
||||||
try:
|
try:
|
||||||
new_subnet = vpc_conn.create_subnet(vpc_id, cidr, az, dry_run=check_mode)
|
new_subnet = get_subnet_info(conn.create_subnet(VpcId=vpc_id, CidrBlock=cidr, AvailabilityZone=az, DryRun=check_mode))
|
||||||
# Sometimes AWS takes its time to create a subnet and so using
|
# Sometimes AWS takes its time to create a subnet and so using
|
||||||
# new subnets's id to do things like create tags results in
|
# new subnets's id to do things like create tags results in
|
||||||
# exception. boto doesn't seem to refresh 'state' of the newly
|
# exception. boto doesn't seem to refresh 'state' of the newly
|
||||||
# created subnet, i.e.: it's always 'pending'.
|
# created subnet, i.e.: it's always 'pending'.
|
||||||
subnet = False
|
subnet = False
|
||||||
while subnet is False:
|
while subnet is False:
|
||||||
subnet = subnet_exists(vpc_conn, new_subnet.id)
|
subnet = subnet_exists(conn, new_subnet['id'])
|
||||||
time.sleep(0.1)
|
time.sleep(0.1)
|
||||||
except EC2ResponseError as e:
|
except ClientError as e:
|
||||||
if e.error_code == "DryRunOperation":
|
if e.response['Error']['Code'] == "DryRunOperation":
|
||||||
subnet = None
|
subnet = None
|
||||||
elif e.error_code == "InvalidSubnet.Conflict":
|
|
||||||
raise AnsibleVPCSubnetCreationException("%s: the CIDR %s conflicts with another subnet with the VPC ID %s." % (e.error_code, cidr, vpc_id))
|
|
||||||
else:
|
else:
|
||||||
raise AnsibleVPCSubnetCreationException(
|
module.fail_json(msg=e.message, exception=traceback.format_exc(),
|
||||||
'Unable to create subnet {0}, error: {1}'.format(cidr, e))
|
**camel_dict_to_snake_dict(e.response))
|
||||||
|
|
||||||
return subnet
|
return subnet
|
||||||
|
|
||||||
|
|
||||||
def get_resource_tags(vpc_conn, resource_id):
|
def ensure_tags(conn, module, subnet, tags, add_only, check_mode):
|
||||||
return dict((t.name, t.value) for t in
|
|
||||||
vpc_conn.get_all_tags(filters={'resource-id': resource_id}))
|
|
||||||
|
|
||||||
|
|
||||||
def ensure_tags(vpc_conn, resource_id, tags, add_only, check_mode):
|
|
||||||
try:
|
try:
|
||||||
cur_tags = get_resource_tags(vpc_conn, resource_id)
|
cur_tags = subnet['tags']
|
||||||
if cur_tags == tags:
|
|
||||||
return {'changed': False, 'tags': cur_tags}
|
|
||||||
|
|
||||||
to_delete = dict((k, cur_tags[k]) for k in cur_tags if k not in tags)
|
to_delete = dict((k, cur_tags[k]) for k in cur_tags if k not in tags)
|
||||||
if to_delete and not add_only:
|
if to_delete and not add_only:
|
||||||
vpc_conn.delete_tags(resource_id, to_delete, dry_run=check_mode)
|
conn.delete_tags(Resources=[subnet['id']], Tags=ansible_dict_to_boto3_tag_list(to_delete), DryRun=check_mode)
|
||||||
|
|
||||||
to_add = dict((k, tags[k]) for k in tags if k not in cur_tags or cur_tags[k] != tags[k])
|
to_add = dict((k, tags[k]) for k in tags if k not in cur_tags or cur_tags[k] != tags[k])
|
||||||
if to_add:
|
if to_add:
|
||||||
vpc_conn.create_tags(resource_id, to_add, dry_run=check_mode)
|
conn.create_tags(Resources=[subnet['id']], Tags=ansible_dict_to_boto3_tag_list(to_add), DryRun=check_mode)
|
||||||
|
|
||||||
latest_tags = get_resource_tags(vpc_conn, resource_id)
|
except ClientError as e:
|
||||||
return {'changed': True, 'tags': latest_tags}
|
if e.response['Error']['Code'] != "DryRunOperation":
|
||||||
except EC2ResponseError as e:
|
module.fail_json(msg=e.message, exception=traceback.format_exc(),
|
||||||
raise AnsibleTagCreationException(
|
**camel_dict_to_snake_dict(e.response))
|
||||||
'Unable to update tags for {0}, error: {1}'.format(resource_id, e))
|
|
||||||
|
|
||||||
|
|
||||||
def get_matching_subnet(vpc_conn, vpc_id, cidr):
|
def ensure_map_public(conn, module, subnet, map_public, check_mode):
|
||||||
subnets = vpc_conn.get_all_subnets(filters={'vpc_id': vpc_id})
|
if check_mode:
|
||||||
return next((s for s in subnets if s.cidr_block == cidr), None)
|
return
|
||||||
|
|
||||||
|
try:
|
||||||
|
conn.modify_subnet_attribute(SubnetId=subnet['id'], MapPublicIpOnLaunch={'Value': map_public})
|
||||||
|
except ClientError as e:
|
||||||
|
module.fail_json(msg=e.message, exception=traceback.format_exc(),
|
||||||
|
**camel_dict_to_snake_dict(e.response))
|
||||||
|
|
||||||
|
|
||||||
def ensure_subnet_present(vpc_conn, vpc_id, cidr, az, tags, check_mode):
|
def get_matching_subnet(conn, vpc_id, cidr):
|
||||||
subnet = get_matching_subnet(vpc_conn, vpc_id, cidr)
|
filters = ansible_dict_to_boto3_filter_list({'vpc-id': vpc_id, 'cidr-block': cidr})
|
||||||
|
subnets = get_subnet_info(conn.describe_subnets(Filters=filters))
|
||||||
|
if len(subnets) > 0:
|
||||||
|
return subnets[0]
|
||||||
|
else:
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def ensure_subnet_present(conn, module, vpc_id, cidr, az, tags, map_public, check_mode):
|
||||||
|
subnet = get_matching_subnet(conn, vpc_id, cidr)
|
||||||
changed = False
|
changed = False
|
||||||
if subnet is None:
|
if subnet is None:
|
||||||
subnet = create_subnet(vpc_conn, vpc_id, cidr, az, check_mode)
|
subnet = create_subnet(conn, module, vpc_id, cidr, az, check_mode)
|
||||||
changed = True
|
changed = True
|
||||||
# Subnet will be None when check_mode is true
|
# Subnet will be None when check_mode is true
|
||||||
if subnet is None:
|
if subnet is None:
|
||||||
|
@ -202,32 +205,33 @@ def ensure_subnet_present(vpc_conn, vpc_id, cidr, az, tags, check_mode):
|
||||||
'changed': changed,
|
'changed': changed,
|
||||||
'subnet': {}
|
'subnet': {}
|
||||||
}
|
}
|
||||||
|
if map_public != subnet['map_public_ip_on_launch']:
|
||||||
if tags != subnet.tags:
|
ensure_map_public(conn, module, subnet, map_public, check_mode)
|
||||||
ensure_tags(vpc_conn, subnet.id, tags, False, check_mode)
|
subnet['map_public_ip_on_launch'] = map_public
|
||||||
subnet.tags = tags
|
|
||||||
changed = True
|
changed = True
|
||||||
|
|
||||||
subnet_info = get_subnet_info(subnet)
|
if tags != subnet['tags']:
|
||||||
|
ensure_tags(conn, module, subnet, tags, False, check_mode)
|
||||||
|
subnet['tags'] = tags
|
||||||
|
changed = True
|
||||||
|
|
||||||
return {
|
return {
|
||||||
'changed': changed,
|
'changed': changed,
|
||||||
'subnet': subnet_info
|
'subnet': subnet
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def ensure_subnet_absent(vpc_conn, vpc_id, cidr, check_mode):
|
def ensure_subnet_absent(conn, module, vpc_id, cidr, check_mode):
|
||||||
subnet = get_matching_subnet(vpc_conn, vpc_id, cidr)
|
subnet = get_matching_subnet(conn, vpc_id, cidr)
|
||||||
if subnet is None:
|
if subnet is None:
|
||||||
return {'changed': False}
|
return {'changed': False}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
vpc_conn.delete_subnet(subnet.id, dry_run=check_mode)
|
conn.delete_subnet(SubnetId=subnet['id'], DryRun=check_mode)
|
||||||
return {'changed': True}
|
return {'changed': True}
|
||||||
except EC2ResponseError as e:
|
except ClientError as e:
|
||||||
raise AnsibleVPCSubnetDeletionException(
|
module.fail_json(msg=e.message, exception=traceback.format_exc(),
|
||||||
'Unable to delete subnet {0}, error: {1}'
|
**camel_dict_to_snake_dict(e.response))
|
||||||
.format(subnet.cidr_block, e))
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
@ -238,22 +242,20 @@ def main():
|
||||||
cidr=dict(default=None, required=True),
|
cidr=dict(default=None, required=True),
|
||||||
state=dict(default='present', choices=['present', 'absent']),
|
state=dict(default='present', choices=['present', 'absent']),
|
||||||
tags=dict(default={}, required=False, type='dict', aliases=['resource_tags']),
|
tags=dict(default={}, required=False, type='dict', aliases=['resource_tags']),
|
||||||
vpc_id=dict(default=None, required=True)
|
vpc_id=dict(default=None, required=True),
|
||||||
|
map_public=dict(default=False, required=False, type='bool')
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True)
|
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True)
|
||||||
|
|
||||||
if not HAS_BOTO:
|
if not HAS_BOTO3:
|
||||||
module.fail_json(msg='boto is required for this module')
|
module.fail_json(msg='boto3 is required for this module')
|
||||||
|
|
||||||
region, ec2_url, aws_connect_params = get_aws_connection_info(module)
|
region, ec2_url, aws_connect_params = get_aws_connection_info(module, boto3=True)
|
||||||
|
|
||||||
if region:
|
if region:
|
||||||
try:
|
connection = boto3_conn(module, conn_type='client', resource='ec2', region=region, endpoint=ec2_url, **aws_connect_params)
|
||||||
connection = connect_to_aws(boto.vpc, region, **aws_connect_params)
|
|
||||||
except (boto.exception.NoAuthHandlerFound, AnsibleAWSError) as e:
|
|
||||||
module.fail_json(msg=str(e))
|
|
||||||
else:
|
else:
|
||||||
module.fail_json(msg="region must be specified")
|
module.fail_json(msg="region must be specified")
|
||||||
|
|
||||||
|
@ -262,16 +264,18 @@ def main():
|
||||||
cidr = module.params.get('cidr')
|
cidr = module.params.get('cidr')
|
||||||
az = module.params.get('az')
|
az = module.params.get('az')
|
||||||
state = module.params.get('state')
|
state = module.params.get('state')
|
||||||
|
map_public = module.params.get('map_public')
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if state == 'present':
|
if state == 'present':
|
||||||
result = ensure_subnet_present(connection, vpc_id, cidr, az, tags,
|
result = ensure_subnet_present(connection, module, vpc_id, cidr, az, tags, map_public,
|
||||||
check_mode=module.check_mode)
|
check_mode=module.check_mode)
|
||||||
elif state == 'absent':
|
elif state == 'absent':
|
||||||
result = ensure_subnet_absent(connection, vpc_id, cidr,
|
result = ensure_subnet_absent(connection, module, vpc_id, cidr,
|
||||||
check_mode=module.check_mode)
|
check_mode=module.check_mode)
|
||||||
except AnsibleVPCSubnetException as e:
|
except ClientError as e:
|
||||||
module.fail_json(msg=str(e))
|
module.fail_json(msg=e.message, exception=traceback.format_exc(),
|
||||||
|
**camel_dict_to_snake_dict(e.response))
|
||||||
|
|
||||||
module.exit_json(**result)
|
module.exit_json(**result)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue