From 22df46d16895b16c5d4932f33b33fe823b7522a2 Mon Sep 17 00:00:00 2001 From: einarc Date: Fri, 11 Nov 2016 10:10:22 -0800 Subject: [PATCH] Avoid `TypeError` when desired_capacity is not provided to `ec2_asg` module (#5501) Moving the "check if min_size/max_size/desired_capacity..." code to execute BEFORE the desired_capacity code is used in the following operation: num_new_inst_needed = desired_capacity - len(new_instances) Otherwise the following exception occurs when desired_capacity is not specified and you're replacing instances: num_new_inst_needed = desired_capacity - len(new_instances) TypeError: unsupported operand type(s) for -: 'NoneType' and 'int' Stack Trace: An exception occurred during task execution. The full traceback is: Traceback (most recent call last): File "/var/lib/awx/.ansible/tmp/ansible-tmp-1478229985.74-62334493713074/ec2_asg", line 3044, in main() File "/var/lib/awx/.ansible/tmp/ansible-tmp-1478229985.74-62334493713074/ec2_asg", line 3038, in main replace_changed, asg_properties=replace(connection, module) File "/var/lib/awx/.ansible/tmp/ansible-tmp-1478229985.74-62334493713074/ec2_asg", line 2778, in replace num_new_inst_needed = desired_capacity - len(new_instances) TypeError: unsupported operand type(s) for -: 'NoneType' and 'int' fatal: [localhost]: FAILED! => {"changed": false, "failed": true, "invocation": {"module_name": "ec2_asg"}, "module_stderr": "Traceback (most recent call last):\n File \"/var/lib/awx/.ansible/tmp/ansible-tmp-1478229985.74-62334493713074/ec2_asg\", line 3044, in \n main()\n File \"/var/lib/awx/.ansible/tmp/ansible-tmp-1478229985.74-62334493713074/ec2_asg\", line 3038, in main\n replace_changed, asg_properties=replace(connection, module)\n File \"/var/lib/awx/.ansible/tmp/ansible-tmp-1478229985.74-62334493713074/ec2_asg\", line 2778, in replace\n num_new_inst_needed = desired_capacity - len(new_instances)\nTypeError: unsupported operand type(s) for -: 'NoneType' and 'int'\n", "module_stdout": "", "msg": "MODULE FAILURE", "parsed": false} to retry, use: --limit @ --- lib/ansible/modules/cloud/amazon/ec2_asg.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/lib/ansible/modules/cloud/amazon/ec2_asg.py b/lib/ansible/modules/cloud/amazon/ec2_asg.py index 36cb733aec..ee81214608 100644 --- a/lib/ansible/modules/cloud/amazon/ec2_asg.py +++ b/lib/ansible/modules/cloud/amazon/ec2_asg.py @@ -611,6 +611,14 @@ def replace(connection, module): instances = props['instances'] if replace_instances: instances = replace_instances + + #check if min_size/max_size/desired capacity have been specified and if not use ASG values + if min_size is None: + min_size = as_group.min_size + if max_size is None: + max_size = as_group.max_size + if desired_capacity is None: + desired_capacity = as_group.desired_capacity # check to see if instances are replaceable if checking launch configs new_instances, old_instances = get_instances_by_lc(props, lc_check, instances) @@ -633,14 +641,7 @@ def replace(connection, module): if not old_instances: changed = False return(changed, props) - - #check if min_size/max_size/desired capacity have been specified and if not use ASG values - if min_size is None: - min_size = as_group.min_size - if max_size is None: - max_size = as_group.max_size - if desired_capacity is None: - desired_capacity = as_group.desired_capacity + # set temporary settings and wait for them to be reached # This should get overwritten if the number of instances left is less than the batch size.