From f769305bd4e96893323a965e3fa7ed68ef7dd40a Mon Sep 17 00:00:00 2001 From: autotune Date: Fri, 1 Jan 2016 20:27:47 -0800 Subject: [PATCH] 1113 fix ec2 subnet tags --- cloud/amazon/ec2_vpc.py | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/cloud/amazon/ec2_vpc.py b/cloud/amazon/ec2_vpc.py index b28ef37c223..fcb262adee3 100644 --- a/cloud/amazon/ec2_vpc.py +++ b/cloud/amazon/ec2_vpc.py @@ -46,9 +46,10 @@ options: choices: [ "yes", "no" ] subnets: description: - - 'A dictionary array of subnets to add of the form: { cidr: ..., az: ... , resource_tags: ... }. Where az is the desired availability zone of the subnet, but it is not required. Tags (i.e.: resource_tags) is also optional and use dictionary form: { "Environment":"Dev", "Tier":"Web", ...}. All VPC subnets not in this list will be removed. As of 1.8, if the subnets parameter is not specified, no existing subnets will be modified.' + - 'A dictionary array of subnets to add of the form: { cidr: ..., az: ... , resource_tags: ... }. Where az is the desired availability zone of the subnet, but it is not required. Tags (i.e.: resource_tags) is also optional and use dictionary form: { "Environment":"Dev", "Tier":"Web", ...}. All VPC subnets not in this list will be removed as well. As of 1.8, if the subnets parameter is not specified, no existing subnets will be modified.' required: false default: null + resource_tags: See resource_tags for VPC below. The main difference is subnet tags not specified here will be deleted. vpc_id: description: - A VPC id to terminate when state=absent @@ -406,9 +407,39 @@ def create_vpc(module, vpc_conn): # First add all new subnets for subnet in subnets: add_subnet = True + subnet_tags_current = True + new_subnet_tags = subnet.get('resource_tags', None) + subnet_tags_delete = [] + for csn in current_subnets: if subnet['cidr'] == csn.cidr_block: add_subnet = False + + # Check if AWS subnet tags are in playbook subnet tags + subnet_tags_extra = (set(csn.tags.items()).issubset(set(new_subnet_tags.items()))) + # Check if subnet tags in playbook are in AWS subnet tags + subnet_tags_current = (set(new_subnet_tags.items()).issubset(set(csn.tags.items()))) + if subnet_tags_extra is False: + try: + for item in csn.tags.items(): + if item not in new_subnet_tags.items(): + subnet_tags_delete.append(item) + + subnet_tags_delete = [key[0] for key in subnet_tags_delete] + delete_subnet_tag = vpc_conn.delete_tags(csn.id, subnet_tags_delete) + changed = True + except EC2ResponseError, e: + module.fail_json(msg='Unable to delete resource tag, error {0}'.format(e)) + # Add new subnet tags if not current + subnet_tags_current = (set(new_subnet_tags.items()).issubset(set(csn.tags.items()))) + if subnet_tags_current is not True: + try: + changed = True + create_subnet_tag = vpc_conn.create_tags(csn.id, new_subnet_tags) + + except EC2ResponseError, e: + module.fail_json(msg='Unable to create resource tag, error: {0}'.format(e)) + if add_subnet: try: new_subnet = vpc_conn.create_subnet(vpc.id, subnet['cidr'], subnet.get('az', None))