Skip to content

Commit

Permalink
Handle non-existing tags removal
Browse files Browse the repository at this point in the history
  • Loading branch information
mandar242 committed Mar 2, 2022
1 parent 3cbda28 commit 6d1837b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
11 changes: 7 additions & 4 deletions plugins/modules/ec2_asg.py
Original file line number Diff line number Diff line change
Expand Up @@ -1264,17 +1264,20 @@ def create_autoscaling_group(connection):
have_tags = [have_tag for have_tag in have_tags if have_tag['Key'] != dead_tag]

if remove_tags:
existing_tags = describe_autoscaling_tags(connection)
dead_tags = []
for dead_tag in want_tag_keyvals:
dead_tags.append(dict(
ResourceId=as_group['AutoScalingGroupName'], ResourceType='auto-scaling-group', Key=dead_tag))
if dead_tag in existing_tags:
dead_tags.append(dict(
ResourceId=as_group['AutoScalingGroupName'], ResourceType='auto-scaling-group', Key=dead_tag, Value=existing_tags[dead_tag]))
if dead_tags:
connection.delete_tags(Tags=dead_tags)

zipped = zip(have_tags, want_tags)
if len(have_tags) != len(want_tags) or not all(x == y for x, y in zipped):
changed = True
connection.create_or_update_tags(Tags=asg_tags)
if not remove_tags:
changed = True
connection.create_or_update_tags(Tags=asg_tags)

# Handle load balancer attachments/detachments
# Attach load balancers if they are specified but none currently exist
Expand Down
24 changes: 23 additions & 1 deletion tests/integration/targets/ec2_asg/tasks/tag_operations.yml
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,35 @@
- '"autoscaling:CreateOrUpdateTags" in add_result.resource_actions'
- '"autoscaling:DescribeTags" in info_result.resource_actions'

- name: Remove Non-existing tags
ec2_asg:
name: "{{ resource_prefix }}-asg-tag-test"
tags:
- key10: 'val_a'
- key20: 'val_b'
remove_tags: true
register: remove_result

- name: get asg tags existing
ec2_asg:
name: "{{ resource_prefix }}-asg-tag-test"
list_tags: true
register: info_result

- assert:
that:
- remove_result is not changed
- info_result.AutoScalingGroup_Tags | length == 4
- '"autoscaling:DeleteTags" not in remove_result.resource_actions'
- '"autoscaling:DescribeTags" in info_result.resource_actions'

# can provide a key-val pair or just the tag key of the tag to be delete
- name: Remove 2 tags - key2, key4
ec2_asg:
name: "{{ resource_prefix }}-asg-tag-test"
tags:
- key2: 'val2'
- key4: 'val4'
- key4:
remove_tags: true
register: remove_result

Expand Down

0 comments on commit 6d1837b

Please sign in to comment.