From 82765637b9d315d5e895b3ff312fb8df8575300c Mon Sep 17 00:00:00 2001 From: Willem van Ketwich Date: Thu, 10 Aug 2017 01:56:30 +1000 Subject: [PATCH] AWS EC2 ASG - Replace ELBs correctly and remove existing ELBs when specified as an empty list (#27830) * fixes for edge cases - load_balancers has not been specified - don't want to delete existing elbs, wanted elbs aren't a superset of has_elbs (eg. 1 elb existing, adding another), specifying load_balancers: [] to delete existing elbs --- lib/ansible/modules/cloud/amazon/ec2_asg.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/ansible/modules/cloud/amazon/ec2_asg.py b/lib/ansible/modules/cloud/amazon/ec2_asg.py index 3bc276e40a2..1a5922c2b56 100644 --- a/lib/ansible/modules/cloud/amazon/ec2_asg.py +++ b/lib/ansible/modules/cloud/amazon/ec2_asg.py @@ -800,6 +800,7 @@ def create_autoscaling_group(connection, module): # Update load balancers if they are specified and one or more already exists elif as_group['LoadBalancerNames']: + change_load_balancers = load_balancers is not None # Get differences if not load_balancers: load_balancers = list() @@ -807,7 +808,7 @@ def create_autoscaling_group(connection, module): has_elbs = set(as_group['LoadBalancerNames']) # check if all requested are already existing - if has_elbs.issuperset(wanted_elbs): + if has_elbs - wanted_elbs and change_load_balancers: # if wanted contains less than existing, then we need to delete some elbs_to_detach = has_elbs.difference(wanted_elbs) if elbs_to_detach: @@ -816,7 +817,7 @@ def create_autoscaling_group(connection, module): AutoScalingGroupName=group_name, LoadBalancerNames=list(elbs_to_detach) ) - if wanted_elbs.issuperset(has_elbs): + if wanted_elbs - has_elbs: # if has contains less than wanted, then we need to add some elbs_to_attach = wanted_elbs.difference(has_elbs) if elbs_to_attach: