From 64c4f0a48872830ea2e94f96b77d590e38c35bfa Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Tue, 3 May 2022 20:53:51 +0000 Subject: [PATCH] Update ec2_instance find_instances to fix incompatibility with Python 3.8 (#767) (#800) [PR #767/0ca065cf backport][stable-2] Update ec2_instance find_instances to fix incompatibility with Python 3.8 This is a backport of PR #767 as merged into main (0ca065c). 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: Alina Buzachis Reviewed-by: Jill R --- .../709-ec_2_instance-python-3-8-compatibility.yml | 3 +++ plugins/modules/ec2_instance.py | 9 +++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/709-ec_2_instance-python-3-8-compatibility.yml diff --git a/changelogs/fragments/709-ec_2_instance-python-3-8-compatibility.yml b/changelogs/fragments/709-ec_2_instance-python-3-8-compatibility.yml new file mode 100644 index 00000000000..293f89179b2 --- /dev/null +++ b/changelogs/fragments/709-ec_2_instance-python-3-8-compatibility.yml @@ -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). diff --git a/plugins/modules/ec2_instance.py b/plugins/modules/ec2_instance.py index 1a2d5ddd666..c746dbc13f9 100644 --- a/plugins/modules/ec2_instance.py +++ b/plugins/modules/ec2_instance.py @@ -1457,6 +1457,8 @@ 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: @@ -1464,13 +1466,16 @@ def find_instances(ids=None, filters=None): 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