diff --git a/plugins/modules/ec2_asg.py b/plugins/modules/ec2_asg.py index 7a22803b88b..3e0ff3e1f63 100644 --- a/plugins/modules/ec2_asg.py +++ b/plugins/modules/ec2_asg.py @@ -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 diff --git a/tests/integration/targets/ec2_asg/tasks/tag_operations.yml b/tests/integration/targets/ec2_asg/tasks/tag_operations.yml index 62d4924c8d8..0a53e2cc337 100644 --- a/tests/integration/targets/ec2_asg/tasks/tag_operations.yml +++ b/tests/integration/targets/ec2_asg/tasks/tag_operations.yml @@ -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