diff --git a/changelogs/fragments/497-s3_sync-add-storage_class.yaml b/changelogs/fragments/497-s3_sync-add-storage_class.yaml new file mode 100644 index 00000000000..bbca03d7357 --- /dev/null +++ b/changelogs/fragments/497-s3_sync-add-storage_class.yaml @@ -0,0 +1,2 @@ +minor_changes: +- s3_sync - new ``storage_class`` feature allowing to specify the storage class when any object is added to an S3 bucket (https://github.com/ansible-collections/community.aws/issues/358). diff --git a/plugins/modules/s3_sync.py b/plugins/modules/s3_sync.py index e0edbea82b0..589dcd5ba3b 100644 --- a/plugins/modules/s3_sync.py +++ b/plugins/modules/s3_sync.py @@ -102,6 +102,22 @@ - Directives are separated by commas. required: false type: str + storage_class: + description: + - Storage class to be associated to each object added to the S3 bucket. + required: false + choices: + - 'STANDARD' + - 'REDUCED_REDUNDANCY' + - 'STANDARD_IA' + - 'ONEZONE_IA' + - 'INTELLIGENT_TIERING' + - 'GLACIER' + - 'DEEP_ARCHIVE' + - 'OUTPOSTS' + default: 'STANDARD' + type: str + version_added: 1.5.0 delete: description: - Remove remote files that exist in bucket but are not present in the file root. @@ -131,6 +147,12 @@ bucket: tedder file_root: roles/s3/files/ +- name: basic upload using the glacier storage class + community.aws.s3_sync: + bucket: tedder + file_root: roles/s3/files/ + storage_class: GLACIER + - name: all the options community.aws.s3_sync: bucket: tedder @@ -142,6 +164,7 @@ file_change_strategy: force permission: public-read cache_control: "public, max-age=31536000" + storage_class: "GLACIER" include: "*" exclude: "*.txt,.*" ''' @@ -470,6 +493,8 @@ def upload_files(s3, bucket, filelist, params): args['ACL'] = params['permission'] if params.get('cache_control'): args['CacheControl'] = params['cache_control'] + if params.get('storage_class'): + args['StorageClass'] = params['storage_class'] # if this fails exception is caught in main() s3.upload_file(entry['fullpath'], bucket, entry['s3_path'], ExtraArgs=args, Callback=None, Config=None) ret.append(entry) @@ -507,7 +532,10 @@ def main(): include=dict(required=False, default="*"), cache_control=dict(required=False, default=''), delete=dict(required=False, type='bool', default=False), - # future options: encoding, metadata, storage_class, retries + storage_class=dict(required=False, default='STANDARD', + choices=['STANDARD', 'REDUCED_REDUNDANCY', 'STANDARD_IA', 'ONEZONE_IA', + 'INTELLIGENT_TIERING', 'GLACIER', 'DEEP_ARCHIVE', 'OUTPOSTS']), + # future options: encoding, metadata, retries ) module = AnsibleAWSModule( diff --git a/tests/integration/targets/s3_sync/tasks/main.yml b/tests/integration/targets/s3_sync/tasks/main.yml index 965e11828bc..4480c9add56 100644 --- a/tests/integration/targets/s3_sync/tasks/main.yml +++ b/tests/integration/targets/s3_sync/tasks/main.yml @@ -44,7 +44,7 @@ dd if=/dev/zero of=test4.txt bs=1M count=10 args: chdir: "{{ output_dir }}/s3_sync" - + - name: Sync files with remote bucket s3_sync: bucket: "{{ resource_prefix }}-testbucket-ansible" @@ -55,6 +55,30 @@ - output is changed # ============================================================ + - name: Create a second s3_bucket + s3_bucket: + name: "{{ resource_prefix }}-testbucket-ansible-2" + state: present + register: output + + - assert: + that: + - output.changed + - output.name == '{{ resource_prefix }}-testbucket-ansible-2' + - not output.requester_pays + + - name: Sync files with remote bucket using glacier storage class + s3_sync: + bucket: "{{ resource_prefix }}-testbucket-ansible-2" + file_root: "{{ output_dir }}/s3_sync" + storage_class: "GLACIER" + register: output + + - assert: + that: + - output is changed + # ============================================================ + - name: Sync files already present s3_sync: bucket: "{{ resource_prefix }}-testbucket-ansible" @@ -74,6 +98,7 @@ - assert: that: - output is not changed + # ============================================================ # DOCUMENTATION EXAMPLES @@ -106,3 +131,4 @@ ignore_errors: yes with_items: - "{{ resource_prefix }}-testbucket-ansible" + - "{{ resource_prefix }}-testbucket-ansible-2"