diff --git a/changelogs/fragments/s3_bucket_delete_nonexistent_bucket.yml b/changelogs/fragments/s3_bucket_delete_nonexistent_bucket.yml new file mode 100644 index 0000000000..b6ce312a2f --- /dev/null +++ b/changelogs/fragments/s3_bucket_delete_nonexistent_bucket.yml @@ -0,0 +1,3 @@ +--- +bugfixes: + - Handle error paginating object versions when bucket does not exist (https://github.com/ansible/ansible/issues/49393) diff --git a/lib/ansible/modules/cloud/amazon/s3_bucket.py b/lib/ansible/modules/cloud/amazon/s3_bucket.py index 0e6ff39023..91166ea869 100644 --- a/lib/ansible/modules/cloud/amazon/s3_bucket.py +++ b/lib/ansible/modules/cloud/amazon/s3_bucket.py @@ -120,7 +120,7 @@ import time from ansible.module_utils.six.moves.urllib.parse import urlparse from ansible.module_utils.six import string_types from ansible.module_utils.basic import to_text -from ansible.module_utils.aws.core import AnsibleAWSModule +from ansible.module_utils.aws.core import AnsibleAWSModule, is_boto3_error_code from ansible.module_utils.ec2 import compare_policies, ec2_argument_spec, boto3_tag_list_to_ansible_dict, ansible_dict_to_boto3_tag_list from ansible.module_utils.ec2 import get_aws_connection_info, boto3_conn, AWSRetry @@ -447,10 +447,13 @@ def paginated_list(s3_client, **pagination_params): def paginated_versions_list(s3_client, **pagination_params): - pg = s3_client.get_paginator('list_object_versions') - for page in pg.paginate(**pagination_params): - # We have to merge the Versions and DeleteMarker lists here, as DeleteMarkers can still prevent a bucket deletion - yield [(data['Key'], data['VersionId']) for data in (page.get('Versions', []) + page.get('DeleteMarkers', []))] + try: + pg = s3_client.get_paginator('list_object_versions') + for page in pg.paginate(**pagination_params): + # We have to merge the Versions and DeleteMarker lists here, as DeleteMarkers can still prevent a bucket deletion + yield [(data['Key'], data['VersionId']) for data in (page.get('Versions', []) + page.get('DeleteMarkers', []))] + except is_boto3_error_code('NoSuchBucket'): + yield [] def destroy_bucket(s3_client, module):