Skip to content

Commit

Permalink
Update ec2_instance find_instances to fix incompatibility with Python…
Browse files Browse the repository at this point in the history
… 3.8 (ansible-collections#767) (ansible-collections#800)

[PR ansible-collections#767/0ca065cf backport][stable-2] Update ec2_instance find_instances to fix incompatibility with Python 3.8

This is a backport of PR ansible-collections#767 as merged into main (0ca065c).
SUMMARY

Update ec2_instance find_instances to use temporary dict. instead of inline changes. Fixes ansible-collections#709

ISSUE TYPE


Bugfix Pull Request

COMPONENT NAME

ec2_instance
ADDITIONAL INFORMATION
Current ec2_module iterates over a filters dictionary to perform hyphen to underscore substitution while popping keys in the process. This is not compatible with Python 3.8. I have simply created a second dictionary, I can also create a shallow copy via the copy() module if the team prefers.

Reviewed-by: Alina Buzachis <None>
Reviewed-by: Jill R <None>
  • Loading branch information
patchback[bot] authored May 3, 2022
1 parent da36ce4 commit 64c4f0a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
bugfixes:
- ec2_instance - ec2_instance module broken in Python 3.8 - dict keys modified during iteration (https://github.com/ansible-collections/amazon.aws/issues/709).
9 changes: 7 additions & 2 deletions plugins/modules/ec2_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -1457,20 +1457,25 @@ def change_network_attachments(instance, params):

def find_instances(ids=None, filters=None):
paginator = client.get_paginator('describe_instances')
sanitized_filters = dict()

if ids:
params = dict(InstanceIds=ids)
elif filters is None:
module.fail_json(msg="No filters provided when they were required")
else:
for key in list(filters.keys()):
if not key.startswith("tag:"):
filters[key.replace("_", "-")] = filters.pop(key)
params = dict(Filters=ansible_dict_to_boto3_filter_list(filters))
sanitized_filters[key.replace("_", "-")] = filters[key]
else:
sanitized_filters[key] = filters[key]
params = dict(Filters=ansible_dict_to_boto3_filter_list(sanitized_filters))

try:
results = _describe_instances(**params)
except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e:
module.fail_json_aws(e, msg="Could not describe instances")

retval = list(results)
return retval

Expand Down

0 comments on commit 64c4f0a

Please sign in to comment.