From 2e1e3562b92cdbfc72bc804ba632f0d48f6a71f6 Mon Sep 17 00:00:00 2001 From: Ryan Brown Date: Fri, 16 Sep 2016 10:59:31 -0400 Subject: [PATCH] Stop sorting of termination_policies in `ec2_asg` (#4883) The AWS API requires that any termination policy list that includes `Default` must end with Default. The attribute sorting caused any list of attributes to be lexically sorted, so a list like `["OldestLaunchConfiguration", "Default"]` would be changed to `["Default", "OldestLaunchConfiguration"]` because default is earlier alphabetically. This caused calls to fail with BotoServerError per #4069 This commit also adds proper tracebacks to all botoservererror fail_json calls. Closes #4069 --- cloud/amazon/ec2_asg.py | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/cloud/amazon/ec2_asg.py b/cloud/amazon/ec2_asg.py index 66261fdbbd7..614e3144c8c 100644 --- a/cloud/amazon/ec2_asg.py +++ b/cloud/amazon/ec2_asg.py @@ -203,6 +203,7 @@ to "replace_instances": import time import logging as log +import traceback from ansible.module_utils.basic import * from ansible.module_utils.ec2 import * @@ -441,7 +442,7 @@ def create_autoscaling_group(connection, module): changed = True return(changed, asg_properties) except BotoServerError as e: - module.fail_json(msg=str(e)) + module.fail_json(msg="Failed to create Autoscaling Group: %s" % str(e), exception=traceback.format_exc(e)) else: as_group = as_groups[0] changed = False @@ -453,14 +454,15 @@ def create_autoscaling_group(connection, module): group_attr = getattr(as_group, attr) # we do this because AWS and the module may return the same list # sorted differently - try: - module_attr.sort() - except: - pass - try: - group_attr.sort() - except: - pass + if attr != 'termination_policies': + try: + module_attr.sort() + except: + pass + try: + group_attr.sort() + except: + pass if group_attr != module_attr: changed = True setattr(as_group, attr, module_attr) @@ -496,7 +498,7 @@ def create_autoscaling_group(connection, module): try: as_group.update() except BotoServerError as e: - module.fail_json(msg=str(e)) + module.fail_json(msg="Failed to update Autoscaling Group: %s" % str(e), exception=traceback.format_exc(e)) if wait_for_instances: wait_for_new_inst(module, connection, group_name, wait_timeout, desired_capacity, 'viable_instances') @@ -505,7 +507,7 @@ def create_autoscaling_group(connection, module): as_group = connection.get_all_groups(names=[group_name])[0] asg_properties = get_properties(as_group) except BotoServerError as e: - module.fail_json(msg=str(e)) + module.fail_json(msg="Failed to read existing Autoscaling Groups: %s" % str(e), exception=traceback.format_exc(e)) return(changed, asg_properties)