From 44b2859338a430b1e92e575e72a7cb04a21c73d5 Mon Sep 17 00:00:00 2001 From: Sloane Hertel Date: Wed, 29 Mar 2017 20:18:23 -0400 Subject: [PATCH] ec2: fix instids and res_list being referenced before assigned - fixes #22692 (#23053) * add else statement to ensure instids is set set res_list to None to avoid UnboundLocalError and fix iteration over a nonetype by adding an empty tuple * make res_list empty tuple by default and check for instids before setting tags (fails otherwise) --- lib/ansible/modules/cloud/amazon/ec2.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/ansible/modules/cloud/amazon/ec2.py b/lib/ansible/modules/cloud/amazon/ec2.py index 9a2fa59ab3..05c1d5231a 100644 --- a/lib/ansible/modules/cloud/amazon/ec2.py +++ b/lib/ansible/modules/cloud/amazon/ec2.py @@ -1227,12 +1227,15 @@ def create_instances(module, ec2, vpc, override_count=None): # Now we have to do the intermediate waiting if wait: instids = await_spot_requests(module, ec2, res, count) + else: + instids = [] except boto.exception.BotoServerError as e: module.fail_json(msg="Instance creation failed => %s: %s" % (e.error_code, e.error_message)) # wait here until the instances are up num_running = 0 wait_timeout = time.time() + wait_timeout + res_list = () while wait_timeout > time.time() and num_running < len(instids): try: res_list = ec2.get_all_instances(instids) @@ -1275,7 +1278,7 @@ def create_instances(module, ec2, vpc, override_count=None): inst.modify_attribute('disableApiTermination', True) # Leave this as late as possible to try and avoid InvalidInstanceID.NotFound - if instance_tags: + if instance_tags and instids: try: ec2.create_tags(instids, instance_tags) except boto.exception.EC2ResponseError as e: