From f4c60e6c1badd4f8c5963bfaefce7ad29dcc1d82 Mon Sep 17 00:00:00 2001 From: Razique Date: Sat, 2 Apr 2022 11:35:22 -0700 Subject: [PATCH 1/3] * Update ec2_instance find_instances function to use temporary dict. instead of inline changes --- .../709-ec_2_instance-python-3-8-compatibility.yml | 3 +++ plugins/modules/ec2_instance.py | 7 +++++-- 2 files changed, 8 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..b51243e01f9 --- /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 b3d6cad66ba..0ba8f8ff3cb 100644 --- a/plugins/modules/ec2_instance.py +++ b/plugins/modules/ec2_instance.py @@ -1463,6 +1463,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: @@ -1470,13 +1472,14 @@ 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] + 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 From fd362af932040c366275820f92593577715966f7 Mon Sep 17 00:00:00 2001 From: Razique Date: Sat, 2 Apr 2022 11:45:30 -0700 Subject: [PATCH 2/3] * Remove extra space --- .../fragments/709-ec_2_instance-python-3-8-compatibility.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index b51243e01f9..293f89179b2 100644 --- a/changelogs/fragments/709-ec_2_instance-python-3-8-compatibility.yml +++ b/changelogs/fragments/709-ec_2_instance-python-3-8-compatibility.yml @@ -1,3 +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). + - ec2_instance - ec2_instance module broken in Python 3.8 - dict keys modified during iteration (https://github.com/ansible-collections/amazon.aws/issues/709). From 708e8a585f19738e146dcfef2ee9c0cf5508069b Mon Sep 17 00:00:00 2001 From: Razique Date: Wed, 6 Apr 2022 18:10:37 -0700 Subject: [PATCH 3/3] * Add tags to sanitized list per feedback --- plugins/modules/ec2_instance.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/plugins/modules/ec2_instance.py b/plugins/modules/ec2_instance.py index 0ba8f8ff3cb..39bd528162d 100644 --- a/plugins/modules/ec2_instance.py +++ b/plugins/modules/ec2_instance.py @@ -1473,6 +1473,8 @@ def find_instances(ids=None, filters=None): for key in list(filters.keys()): if not key.startswith("tag:"): sanitized_filters[key.replace("_", "-")] = filters[key] + else: + sanitized_filters[key] = filters[key] params = dict(Filters=ansible_dict_to_boto3_filter_list(sanitized_filters)) try: