1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
|
---
- vars:
withmeta_data:
something: exists
version: "1.0.2"
metacopy_data:
name: metacopy
version: "1.0.3"
another_metadata:
another: meta
mode: copy
block:
- name: define bucket name used for tests
ansible.builtin.set_fact:
copy_bucket:
src: "{{ bucket_name }}-copysrc"
dst: "{{ bucket_name }}-copydst"
- name: create bucket source
amazon.aws.s3_bucket:
name: "{{ copy_bucket.src }}"
state: present
- name: create bucket destination
amazon.aws.s3_bucket:
name: "{{ copy_bucket.dst }}"
state: present
- name: Create content
ansible.builtin.set_fact:
content: "{{ lookup('password', '/dev/null chars=ascii_letters,digits,hexdigits,punctuation') }}"
- name: Put a content in the source bucket
amazon.aws.s3_object:
bucket: "{{ copy_bucket.src }}"
mode: put
content: "{{ content }}"
object: source.txt
tags:
ansible_release: 2.0.0
ansible_team: cloud
retries: 3
delay: 3
register: put_result
until:
- '"not found" not in put_result.msg'
ignore_errors: true
- name: Copy the content of the source bucket into dest bucket
amazon.aws.s3_object:
bucket: "{{ copy_bucket.dst }}"
mode: copy
object: destination.txt
copy_src:
bucket: "{{ copy_bucket.src }}"
object: source.txt
retries: 3
delay: 3
register: put_result
until:
- '"not found" not in put_result.msg'
ignore_errors: true
- name: Get the content copied into {{ copy_bucket.dst }}
amazon.aws.s3_object:
bucket: "{{ copy_bucket.dst }}"
mode: getstr
object: destination.txt
register: copy_content
- name: assert that the content is matching with the source
ansible.builtin.assert:
that:
- content == copy_content.contents
- name: Get the download url for object copied into {{ copy_bucket.dst }}
amazon.aws.s3_object:
bucket: "{{ copy_bucket.dst }}"
mode: geturl
object: destination.txt
register: copy_url
- name: assert that tags are the same in the destination bucket
ansible.builtin.assert:
that:
- put_result.tags == copy_url.tags
- name: Copy the same content from the source bucket into dest bucket (idempotency)
amazon.aws.s3_object:
bucket: "{{ copy_bucket.dst }}"
mode: copy
object: destination.txt
copy_src:
bucket: "{{ copy_bucket.src }}"
object: source.txt
register: copy_idempotency
- name: assert that no change was made
ansible.builtin.assert:
that:
- copy_idempotency is not changed
- copy_idempotency.msg == 'ETag from source and destination are the same'
- name: Copy object with tags
amazon.aws.s3_object:
bucket: "{{ copy_bucket.dst }}"
mode: copy
object: destination.txt
tags:
ansible_release: 2.0.1
copy_src:
bucket: "{{ copy_bucket.src }}"
object: source.txt
register: copy_result
- name: assert that tags were updated
ansible.builtin.assert:
that:
- copy_result is changed
- copy_result.tags['ansible_release'] == '2.0.1'
- name: Copy object with tags (idempotency)
amazon.aws.s3_object:
bucket: "{{ copy_bucket.dst }}"
mode: copy
object: destination.txt
tags:
ansible_release: 2.0.1
copy_src:
bucket: "{{ copy_bucket.src }}"
object: source.txt
register: copy_result
- name: assert that no change was made
ansible.builtin.assert:
that:
- copy_result is not changed
- name: Copy from unexisting key should not succeed
amazon.aws.s3_object:
bucket: "{{ copy_bucket.dst }}"
mode: copy
object: missing_key.txt
copy_src:
bucket: "{{ copy_bucket.src }}"
object: this_key_does_not_exist.txt
register: result
- name: Validate result when copying missing key
ansible.builtin.assert:
that:
- result is not changed
- result.msg == "Key this_key_does_not_exist.txt does not exist in bucket "+copy_bucket.src+"."
# Copy with metadata
- name: Set fact for bucket name
ansible.builtin.set_fact:
bucket_name: "{{ copy_bucket.dst }}"
- name: Create test bucket
amazon.aws.s3_bucket:
name: "{{ bucket_name }}"
state: present
- name: Create test object
amazon.aws.s3_object:
bucket: "{{ bucket_name }}"
object: nometa
mode: put
content: "some content"
- name: Copy and add metadata
amazon.aws.s3_object:
bucket: "{{ bucket_name }}"
object: metacopy
mode: copy
copy_src:
bucket: "{{ bucket_name }}"
object: nometa
metadata: "{{ metacopy_data }}"
- name: Create test object with metadata
amazon.aws.s3_object:
bucket: "{{ bucket_name }}"
object: withmeta
mode: put
content: "another content"
metadata: "{{ withmeta_data }}"
- name: Copy and preserve metadata
amazon.aws.s3_object:
bucket: "{{ bucket_name }}"
object: copywithmeta
mode: copy
copy_src:
bucket: "{{ bucket_name }}"
object: withmeta
- name: Get objects info
amazon.aws.s3_object_info:
bucket_name: "{{ bucket_name }}"
object_name: "{{ item }}"
loop:
- nometa
- metacopy
- withmeta
- copywithmeta
register: obj_info
- assert:
that:
- obj_info.results | selectattr('item', 'equalto', 'nometa') | map(attribute='object_info.0.object_data.metadata') | first == {}
- obj_info.results | selectattr('item', 'equalto', 'withmeta') | map(attribute='object_info.0.object_data.metadata') | first == withmeta_data
- obj_info.results | selectattr('item', 'equalto', 'metacopy') | map(attribute='object_info.0.object_data.metadata') | first == metacopy_data
- obj_info.results | selectattr('item', 'equalto', 'copywithmeta') | map(attribute='object_info.0.object_data.metadata') | first == withmeta_data
# Validate copy idempotency with metadata
- name: Copy same object including metadata (check_mode=true)
amazon.aws.s3_object:
bucket: "{{ bucket_name }}"
mode: copy
object: copywithmeta
copy_src:
bucket: "{{ bucket_name }}"
object: withmeta
metadata: "{{ another_metadata }}"
register: copy_with_metadata_checkmode
check_mode: true
- name: Get objects info
amazon.aws.s3_object_info:
bucket_name: "{{ bucket_name }}"
object_name: copywithmeta
register: obj_info
- name: Validate that objects module reported change but metadata was not updated
ansible.builtin.assert:
that:
- copy_with_metadata_checkmode is changed
- obj_info.object_info.0.object_data.metadata == withmeta_data
- name: Copy same object including metadata
amazon.aws.s3_object:
bucket: "{{ bucket_name }}"
mode: copy
object: copywithmeta
copy_src:
bucket: "{{ bucket_name }}"
object: withmeta
metadata: "{{ another_metadata }}"
register: copy_with_metadata
- name: Get objects info
amazon.aws.s3_object_info:
bucket_name: "{{ bucket_name }}"
object_name: copywithmeta
register: obj_info
- name: Validate that objects module reported change and metadata was updated
ansible.builtin.assert:
that:
- copy_with_metadata is changed
- obj_info.object_info.0.object_data.metadata == another_metadata
always:
- ansible.builtin.include_tasks: delete_bucket.yml
with_items:
- "{{ copy_bucket.dst }}"
- "{{ copy_bucket.src }}"
|