From 425d35ef0fbc8655919de1ae28db94c5738d14f9 Mon Sep 17 00:00:00 2001 From: Matt Clay Date: Wed, 3 May 2017 10:25:01 +0800 Subject: [PATCH] Fix ec2_elb_lb error handling. (#24183) - Use `fail_json` for `get_all_load_balancers` unhandled exception. - Do not mask error message on failure of `enable_zones`. - Do not mask error message on failure of `disable_zones`. Also added/removed blank lines to improve PEP 8 compliance. --- .../modules/cloud/amazon/ec2_elb_lb.py | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/ansible/modules/cloud/amazon/ec2_elb_lb.py b/lib/ansible/modules/cloud/amazon/ec2_elb_lb.py index 5d0eaccf4c..0c4f3c5018 100644 --- a/lib/ansible/modules/cloud/amazon/ec2_elb_lb.py +++ b/lib/ansible/modules/cloud/amazon/ec2_elb_lb.py @@ -403,12 +403,14 @@ except ImportError: HAS_BOTO = False import time +import traceback import random from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.ec2 import ec2_argument_spec, connect_to_aws, AnsibleAWSError from ansible.module_utils.ec2 import get_aws_connection_info + def _throttleable_operation(max_retries): def _operation_wrapper(op): def _do_op(*args, **kwargs): @@ -477,7 +479,12 @@ class ElbManager(object): self.changed = False self.status = 'gone' self.elb_conn = self._get_elb_connection() - self.elb = self._get_elb() + + try: + self.elb = self._get_elb() + except boto.exception.BotoServerError as e: + module.fail_json(msg='unable to get all load balancers: %s' % e.message, exception=traceback.format_exc()) + self.ec2_conn = self._get_ec2_connection() @_throttleable_operation(_THROTTLING_RETRIES) @@ -832,20 +839,15 @@ class ElbManager(object): try: self.elb.enable_zones(zones) except boto.exception.BotoServerError as e: - if "Invalid Availability Zone" in e.error_message: - self.module.fail_json(msg=e.error_message) - else: - self.module.fail_json(msg="an unknown server error occurred, please try again later") + self.module.fail_json(msg='unable to enable zones: %s' % e.message, exception=traceback.format_exc()) + self.changed = True def _disable_zones(self, zones): try: self.elb.disable_zones(zones) except boto.exception.BotoServerError as e: - if "Invalid Availability Zone" in e.error_message: - self.module.fail_json(msg=e.error_message) - else: - self.module.fail_json(msg="an unknown server error occurred, please try again later") + self.module.fail_json(msg='unable to disable zones: %s' % e.message, exception=traceback.format_exc()) self.changed = True def _attach_subnets(self, subnets): @@ -1321,7 +1323,6 @@ def main(): except boto.exception.NoAuthHandlerFound as e: module.fail_json(msg = str(e)) - elb_man = ElbManager(module, name, listeners, purge_listeners, zones, purge_zones, security_group_ids, health_check, subnets, purge_subnets, scheme, @@ -1331,7 +1332,6 @@ def main(): region=region, instance_ids=instance_ids, purge_instance_ids=purge_instance_ids, **aws_connect_params) - # check for unsupported attributes for this version of boto if cross_az_load_balancing and not elb_man._check_attribute_support('cross_zone_load_balancing'): module.fail_json(msg="You must install boto >= 2.18.0 to use the cross_az_load_balancing attribute")