Changed resource_tags to tags to match other modules (resource_tags still an alias) Added get_subnet_info method to return more subnet info - matches same method in ec2_vpc_subnet_facts module Rework of tags - will now only apply the tags listed in the module. Existing tags not listed will be removed (desired state!)
This commit is contained in:
parent
3497f67d28
commit
2a4704e21d
1 changed files with 55 additions and 26 deletions
|
@ -34,7 +34,7 @@ options:
|
||||||
default: null
|
default: null
|
||||||
tags:
|
tags:
|
||||||
description:
|
description:
|
||||||
- "A dictionary array of resource tags of the form: { tag1: value1, tag2: value2 }. This module identifies a subnet by CIDR and will update the subnet's tags to match. Tags not in this list will be ignored."
|
- "A dict of tags to apply to the subnet. Any tags currently applied to the subnet and not present here will be removed."
|
||||||
required: false
|
required: false
|
||||||
default: null
|
default: null
|
||||||
aliases: [ 'resource_tags' ]
|
aliases: [ 'resource_tags' ]
|
||||||
|
@ -103,24 +103,49 @@ class AnsibleTagCreationException(AnsibleVPCSubnetException):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def get_subnet_info(subnet):
|
||||||
|
|
||||||
|
subnet_info = { 'id': subnet.id,
|
||||||
|
'availability_zone': subnet.availability_zone,
|
||||||
|
'available_ip_address_count': subnet.available_ip_address_count,
|
||||||
|
'cidr_block': subnet.cidr_block,
|
||||||
|
'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
|
||||||
|
|
||||||
def subnet_exists(vpc_conn, subnet_id):
|
def subnet_exists(vpc_conn, subnet_id):
|
||||||
filters = {'subnet-id': subnet_id}
|
filters = {'subnet-id': subnet_id}
|
||||||
return len(vpc_conn.get_all_subnets(filters=filters)) > 0
|
subnet = vpc_conn.get_all_subnets(filters=filters)
|
||||||
|
if subnet[0].state == "available":
|
||||||
|
return subnet[0]
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
def create_subnet(vpc_conn, vpc_id, cidr, az):
|
def create_subnet(vpc_conn, vpc_id, cidr, az, check_mode):
|
||||||
try:
|
try:
|
||||||
new_subnet = vpc_conn.create_subnet(vpc_id, cidr, az)
|
new_subnet = vpc_conn.create_subnet(vpc_id, cidr, az, dry_run=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'.
|
||||||
while not subnet_exists(vpc_conn, new_subnet.id):
|
subnet = False
|
||||||
|
while subnet is False:
|
||||||
|
subnet = subnet_exists(vpc_conn, new_subnet.id)
|
||||||
time.sleep(0.1)
|
time.sleep(0.1)
|
||||||
except EC2ResponseError as e:
|
except EC2ResponseError as e:
|
||||||
|
if e.error_code == "DryRunOperation":
|
||||||
|
subnet = None
|
||||||
|
else:
|
||||||
raise AnsibleVPCSubnetCreationException(
|
raise AnsibleVPCSubnetCreationException(
|
||||||
'Unable to create subnet {0}, error: {1}'.format(cidr, e))
|
'Unable to create subnet {0}, error: {1}'.format(cidr, e))
|
||||||
return new_subnet
|
|
||||||
|
return subnet
|
||||||
|
|
||||||
|
|
||||||
def get_resource_tags(vpc_conn, resource_id):
|
def get_resource_tags(vpc_conn, resource_id):
|
||||||
|
@ -158,10 +183,18 @@ def ensure_subnet_present(vpc_conn, vpc_id, cidr, az, tags, check_mode):
|
||||||
subnet = get_matching_subnet(vpc_conn, vpc_id, cidr)
|
subnet = get_matching_subnet(vpc_conn, vpc_id, cidr)
|
||||||
changed = False
|
changed = False
|
||||||
if subnet is None:
|
if subnet is None:
|
||||||
if check_mode:
|
subnet = create_subnet(vpc_conn, vpc_id, cidr, az, check_mode)
|
||||||
return {'changed': True, 'subnet_id': None, 'subnet': {}}
|
changed = True
|
||||||
|
# Subnet will be None when check_mode is true
|
||||||
|
if subnet is None:
|
||||||
|
return {
|
||||||
|
'changed': changed,
|
||||||
|
'subnet': {}
|
||||||
|
}
|
||||||
|
|
||||||
subnet = create_subnet(vpc_conn, vpc_id, cidr, az)
|
if tags != subnet.tags:
|
||||||
|
ensure_tags(vpc_conn, subnet.id, tags, False, check_mode)
|
||||||
|
subnet.tags = tags
|
||||||
changed = True
|
changed = True
|
||||||
|
|
||||||
if tags is not None:
|
if tags is not None:
|
||||||
|
@ -171,16 +204,11 @@ def ensure_subnet_present(vpc_conn, vpc_id, cidr, az, tags, check_mode):
|
||||||
changed = changed or tag_result['changed']
|
changed = changed or tag_result['changed']
|
||||||
else:
|
else:
|
||||||
tags = get_resource_tags(vpc_conn, subnet.id)
|
tags = get_resource_tags(vpc_conn, subnet.id)
|
||||||
|
subnet_info = get_subnet_info(subnet)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
'changed': changed,
|
'changed': changed,
|
||||||
'subnet_id': subnet.id,
|
'subnet': subnet_info
|
||||||
'subnet': {
|
|
||||||
'tags': tags,
|
|
||||||
'cidr': subnet.cidr_block,
|
|
||||||
'az': subnet.availability_zone,
|
|
||||||
'id': subnet.id,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -202,13 +230,14 @@ def ensure_subnet_absent(vpc_conn, vpc_id, cidr, check_mode):
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
argument_spec = ec2_argument_spec()
|
argument_spec = ec2_argument_spec()
|
||||||
argument_spec.update({
|
argument_spec.update(
|
||||||
'vpc_id': {'required': True},
|
dict(
|
||||||
'resource_tags': {'type': 'dict', 'required': False},
|
az = dict(default=None, required=False),
|
||||||
'cidr': {'required': True},
|
cidr = dict(default=None, required=True),
|
||||||
'az': {},
|
state = dict(default='present', choices=['present', 'absent']),
|
||||||
'state': {'choices': ['present', 'absent'], 'default': 'present'},
|
tags = dict(default=None, required=False, type='dict', aliases=['resource_tags']),
|
||||||
})
|
vpc_id = dict(default=None, required=True)
|
||||||
|
)
|
||||||
module = AnsibleModule(
|
module = AnsibleModule(
|
||||||
argument_spec=argument_spec,
|
argument_spec=argument_spec,
|
||||||
supports_check_mode=True,
|
supports_check_mode=True,
|
||||||
|
@ -230,7 +259,7 @@ def main():
|
||||||
module.fail_json(msg=str(e))
|
module.fail_json(msg=str(e))
|
||||||
|
|
||||||
vpc_id = module.params.get('vpc_id')
|
vpc_id = module.params.get('vpc_id')
|
||||||
tags = module.params.get('resource_tags')
|
tags = module.params.get('tags')
|
||||||
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', 'present')
|
state = module.params.get('state', 'present')
|
||||||
|
|
Loading…
Reference in a new issue