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