From 62a5216d6a16c8b93af7612cb7fc11cce79b4435 Mon Sep 17 00:00:00 2001 From: Mark Chappell Date: Tue, 6 Apr 2021 16:51:10 +0200 Subject: [PATCH] ec2_instance_info - Add AWS Retries (#521) * Add retries to ec2_instance_info * changelog --- .../fragments/521-ec2_instance_info-retries.yml | 2 ++ plugins/modules/ec2_instance_info.py | 13 +++++++++---- 2 files changed, 11 insertions(+), 4 deletions(-) create mode 100644 changelogs/fragments/521-ec2_instance_info-retries.yml diff --git a/changelogs/fragments/521-ec2_instance_info-retries.yml b/changelogs/fragments/521-ec2_instance_info-retries.yml new file mode 100644 index 00000000000..0fde1a3701d --- /dev/null +++ b/changelogs/fragments/521-ec2_instance_info-retries.yml @@ -0,0 +1,2 @@ +minor_changes: +- ec2_instance_info - add retries on common AWS failures (https://github.com/ansible-collections/community.aws/pull/521). diff --git a/plugins/modules/ec2_instance_info.py b/plugins/modules/ec2_instance_info.py index be5f1e68892..4c743688627 100644 --- a/plugins/modules/ec2_instance_info.py +++ b/plugins/modules/ec2_instance_info.py @@ -511,17 +511,23 @@ try: import botocore - from botocore.exceptions import ClientError except ImportError: pass # Handled by AnsibleAWSModule from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict from ansible_collections.amazon.aws.plugins.module_utils.core import AnsibleAWSModule +from ansible_collections.amazon.aws.plugins.module_utils.ec2 import AWSRetry from ansible_collections.amazon.aws.plugins.module_utils.ec2 import ansible_dict_to_boto3_filter_list from ansible_collections.amazon.aws.plugins.module_utils.ec2 import boto3_tag_list_to_ansible_dict +@AWSRetry.jittered_backoff() +def _describe_instances(connection, **params): + paginator = connection.get_paginator('describe_instances') + return paginator.paginate(**params).build_full_result() + + def list_ec2_instances(connection, module): instance_ids = module.params.get("instance_ids") @@ -529,9 +535,8 @@ def list_ec2_instances(connection, module): filters = ansible_dict_to_boto3_filter_list(module.params.get("filters")) try: - reservations_paginator = connection.get_paginator('describe_instances') - reservations = reservations_paginator.paginate(InstanceIds=instance_ids, Filters=filters).build_full_result() - except ClientError as e: + reservations = _describe_instances(connection, InstanceIds=instance_ids, Filters=filters) + except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: module.fail_json_aws(e, msg="Failed to list ec2 instances") instances = []