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 (#767)

Update ec2_instance find_instances to fix incompatibility with Python 3.8

SUMMARY

Update ec2_instance find_instances to use temporary dict. instead of inline changes. Fixes #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: Bikouo Aubin <None>
Reviewed-by: Alina Buzachis <None>
Reviewed-by: Mark Chappell <None>
(cherry picked from commit 0ca065c)
  • Loading branch information
Razique authored and patchback[bot] committed May 3, 2022
1 parent 71b3871 commit dc342cd
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 @@ -1463,20 +1463,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 dc342cd

Please sign in to comment.