Debian Codegraph @

ansible_collections/community/aws/tests/integration/targets/cloudfront_distribution/tasks/main.yml

raw
1 - module_defaults:
2 group/aws:
3 access_key: "{{ aws_access_key }}"
4 secret_key: "{{ aws_secret_key }}"
5 session_token: "{{ security_token | default(omit) }}"
6 cloudfront_distribution:
7 alias: "{{ cloudfront_alias | default(omit) }}"
8 viewer_certificate: "{{ cloudfront_viewer_cert | default(omit) }}"
9 collections:
10 - amazon.aws
11
12 block:
13
14 - name: create cloudfront distribution using defaults
15 cloudfront_distribution:
16 origins:
17 - domain_name: "{{ cloudfront_hostname }}-origin.example.com"
18 id: "{{ cloudfront_hostname }}-origin.example.com"
19 default_cache_behavior:
20 target_origin_id: "{{ cloudfront_hostname }}-origin.example.com"
21 state: present
22 purge_origins: true
23 register: cf_distribution
24
25 - set_fact:
26 distribution_id: '{{ cf_distribution.id }}'
27
28 - name: ensure that default value of 'enabled' is 'true'
29 assert:
30 that:
31 - cf_distribution.changed
32 - cf_distribution.enabled
33 # ensure dict keys for deprecation
34 - '"items" in cf_distribution.origins'
35 - '"elements" in cf_distribution.origins'
36
37 - name: ensure that default value of 'ipv6_enabled' is 'false'
38 assert:
39 that:
40 - cf_distribution.changed
41 - not cf_distribution.is_ipv6_enabled
42
43 - name: Update the distribution with tags
44 cloudfront_distribution:
45 state: present
46 distribution_id: "{{ distribution_id }}"
47 tags:
48 property: ansible
49 register: cf_update_tags
50
51 - name: ensure the 'ipv6_enabled' value has not changed
52 assert:
53 that:
54 - cf_update_tags.changed
55 - not cf_update_tags.is_ipv6_enabled
56
57 - name: Update the distribution ipv6_enabled set to true
58 cloudfront_distribution:
59 state: present
60 distribution_id: "{{ distribution_id }}"
61 ipv6_enabled: true
62 register: cf_update_ipv6
63
64 - name: ensure the 'ipv6_enabled' value has changed (new value is true)
65 assert:
66 that:
67 - cf_update_ipv6.changed
68 - cf_update_ipv6.is_ipv6_enabled
69
70 - name: Update the distribution with tags
71 cloudfront_distribution:
72 state: present
73 distribution_id: "{{ distribution_id }}"
74 tags:
75 test: integration
76 register: cf_update_tags
77
78 - name: ensure the 'ipv6_enabled' value has not changed (value remains true)
79 assert:
80 that:
81 - cf_update_tags.changed
82 - cf_update_tags.is_ipv6_enabled
83
84 - name: Test idempotency updating cloudfront_distribution with same value of ipv6_enabled
85 cloudfront_distribution:
86 state: present
87 distribution_id: "{{ distribution_id }}"
88 ipv6_enabled: true
89 register: cf_update_ipv6
90
91 - name: ensure the 'ipv6_enabled' value has changed (new value is true)
92 assert:
93 that:
94 # FixMe : idempotency issues
95 # - not cf_update_ipv6.changed
96 - cf_update_ipv6.is_ipv6_enabled
97
98 - name: Ensure that default value of 'http_version' is 'http2'
99 assert:
100 that:
101 - cf_update_ipv6.http_version == 'http2'
102
103 - name: Update the distribution http_version to http2and3
104 cloudfront_distribution:
105 state: present
106 distribution_id: "{{ distribution_id }}"
107 http_version: http2and3
108 register: cf_update_http_version
109
110 - name: Ensure that default value of 'http_version' is 'http2and3'
111 assert:
112 that:
113 - cf_update_http_version.changed
114 - cf_update_http_version.http_version == 'http2and3'
115
116 # - name: re-run cloudfront distribution with same defaults
117 # cloudfront_distribution:
118 # distribution_id: "{{ distribution_id }}"
119 # origins:
120 # - domain_name: "{{ cloudfront_hostname }}-origin.example.com"
121 # state: present
122 # register: cf_dist_no_update
123
124 # - name: ensure distribution was not updated
125 # assert:
126 # that:
127 # - not cf_dist_no_update.changed
128
129 # - name: re-run cloudfront distribution using distribution id
130 # cloudfront_distribution:
131 # distribution_id: "{{ distribution_id }}"
132 # purge_origins: no
133 # state: present
134 # register: cf_dist_with_id
135
136 # - name: ensure distribution was not updated
137 # assert:
138 # that:
139 # - not cf_dist_with_id.changed
140
141 - name: update origin http port
142 cloudfront_distribution:
143 distribution_id: "{{ distribution_id }}"
144 origins:
145 - domain_name: "{{ cloudfront_hostname }}-origin.example.com"
146 custom_origin_config:
147 http_port: 8080
148 state: present
149 register: update_origin_http_port
150
151 - name: ensure http port was updated
152 assert:
153 that:
154 - update_origin_http_port.changed
155
156 - name: enable origin Origin Shield
157 cloudfront_distribution:
158 distribution_id: "{{ distribution_id }}"
159 origins:
160 - domain_name: "{{ cloudfront_hostname }}-origin.example.com"
161 custom_origin_config:
162 http_port: 8080
163 origin_shield:
164 enabled: true
165 origin_shield_region: '{{ aws_region }}'
166 state: present
167 register: update_origin_origin_shield
168
169 - name: ensure origin Origin Shield was enabled
170 assert:
171 that:
172 - update_origin_origin_shield.changed
173 - update_origin_origin_shield.origins['elements'][0].origin_shield.enabled
174 - update_origin_origin_shield.origins['elements'][0].origin_shield.origin_shield_region == aws_region
175
176 # TODO: fix module idempotency issue
177 # - name: enable origin Origin Shield again to test idempotency
178 # cloudfront_distribution:
179 # distribution_id: "{{ distribution_id }}"
180 # origins:
181 # - domain_name: "{{ cloudfront_hostname }}-origin.example.com"
182 # custom_origin_config:
183 # http_port: 8080
184 # origin_shield:
185 # enabled: true
186 # origin_shield_region: '{{ aws_region }}'
187 # state: present
188 # register: update_origin_origin_shield_idempotency
189
190 # - name: test idempotency for Origin Shield
191 # assert:
192 # that:
193 # - not update_origin_origin_shield_idempotency.changed
194 # - update_origin_origin_shield_idempotency.origins['elements'][0].origin_shield.enabled
195 # - update_origin_origin_shield_idempotency.origins['elements'][0].origin_shield.origin_shield_region == '{{ aws_region }}'
196
197 - name: disable origin Origin Shield
198 cloudfront_distribution:
199 distribution_id: "{{ distribution_id }}"
200 origins:
201 - domain_name: "{{ cloudfront_hostname }}-origin.example.com"
202 custom_origin_config:
203 http_port: 8080
204 origin_shield:
205 enabled: false
206 state: present
207 register: update_origin_origin_shield_disable
208
209 - name: ensure origin Origin Shield was disabled
210 assert:
211 that:
212 - update_origin_origin_shield_disable.changed
213 - not update_origin_origin_shield_disable.origins['elements'][0].origin_shield.enabled
214
215 - name: update restrictions
216 cloudfront_distribution:
217 distribution_id: "{{ distribution_id }}"
218 restrictions:
219 geo_restriction:
220 restriction_type: "whitelist"
221 items:
222 - "US"
223 state: present
224 register: update_restrictions
225
226 - name: ensure restrictions was updated
227 assert:
228 that:
229 - update_restrictions.changed
230
231 - name: set a random comment
232 set_fact:
233 comment: "{{'ABCDEFabcdef123456'|shuffle|join }}"
234
235 - name: update comment
236 cloudfront_distribution:
237 distribution_id: "{{ distribution_id }}"
238 comment: "{{ comment }}"
239 state: present
240 register: cf_comment
241
242 - name: ensure comment was updated
243 assert:
244 that:
245 - cf_comment.changed
246 - 'cf_comment.comment == comment'
247
248 - name: create second origin
249 cloudfront_distribution:
250 distribution_id: "{{ distribution_id }}"
251 origins:
252 - domain_name: "{{ resource_prefix }}2.example.com"
253 id: "{{ resource_prefix }}2.example.com"
254 default_root_object: index.html
255 state: present
256 wait: true
257 register: cf_add_origin
258
259 - name: ensure origin was added
260 assert:
261 that:
262 - cf_add_origin.origins.quantity == 2
263 - cf_add_origin.changed
264 - "cf_add_origin.default_root_object == 'index.html'"
265
266 - name: re-run second origin
267 cloudfront_distribution:
268 distribution_id: "{{ distribution_id }}"
269 origins:
270 - domain_name: "{{ cloudfront_hostname }}-origin.example.com"
271 custom_origin_config:
272 http_port: 8080
273 - domain_name: "{{ resource_prefix }}2.example.com"
274 default_root_object: index.html
275 wait: true
276 state: present
277 register: cf_rerun_second_origin
278
279 - name: ensure nothing changed after re-run
280 assert:
281 that:
282 - cf_rerun_second_origin.origins.quantity == 2
283 # - not cf_rerun_second_origin.changed
284
285 - name: run with origins in reverse order
286 cloudfront_distribution:
287 distribution_id: "{{ distribution_id }}"
288 origins:
289 - domain_name: "{{ resource_prefix }}2.example.com"
290 - domain_name: "{{ cloudfront_hostname }}-origin.example.com"
291 custom_origin_config:
292 http_port: 8080
293 state: present
294 register: cf_rerun_second_origin_reversed
295
296 - name: ensure nothing changed after reversed re-run
297 assert:
298 that:
299 - cf_rerun_second_origin_reversed.origins.quantity == 2
300 # - not cf_rerun_second_origin_reversed.changed
301
302
303 - name: purge first origin
304 cloudfront_distribution:
305 distribution_id: "{{ distribution_id }}"
306 origins:
307 - domain_name: "{{ resource_prefix }}2.example.com"
308 default_cache_behavior:
309 target_origin_id: "{{ resource_prefix }}2.example.com"
310 purge_origins: true
311 state: present
312 register: cf_purge_origin
313
314 - name: ensure origin was removed
315 assert:
316 that:
317 - cf_purge_origin.origins.quantity == 1
318 - cf_purge_origin.changed
319
320 - name: update default_root_object of existing distribution
321 cloudfront_distribution:
322 distribution_id: "{{ distribution_id }}"
323 origins:
324 - domain_name: "{{ resource_prefix }}2.example.com"
325 default_root_object: index.php
326 state: present
327 register: cf_update_default_root_object
328
329 - name: update default_root_object of existing distribution with retries
330 cloudfront_distribution:
331 distribution_id: "{{ distribution_id }}"
332 origins:
333 - domain_name: "{{ resource_prefix }}2.example.com"
334 default_root_object: index.php
335 state: present
336 retries: 3
337 delay: 3
338 register: cf_update_default_root_object
339
340 - name: ensure origin was updated
341 assert:
342 that:
343 - "cf_update_default_root_object.default_root_object == 'index.php'"
344 - cf_update_default_root_object.changed
345
346 - name: add tags to existing distribution
347 cloudfront_distribution:
348 distribution_id: "{{ distribution_id }}"
349 origins:
350 - domain_name: "{{ resource_prefix }}2.example.com"
351 tags:
352 ATag: tag1
353 Another: tag
354 default_root_object: index.php
355 state: present
356 register: cf_add_tags
357
358 - name: ensure tags were added
359 assert:
360 that:
361 - cf_add_tags.changed
362 - cf_add_tags.tags|length == 2
363
364 - name: delete distribution
365 cloudfront_distribution:
366 distribution_id: "{{ distribution_id }}"
367 enabled: false
368 wait: true
369 state: absent
370
371 - name: create cloudfront distribution with tags and as disabled
372 cloudfront_distribution:
373 enabled: false
374 origins:
375 - domain_name: "{{ resource_prefix }}2.example.com"
376 id: "{{ resource_prefix }}2.example.com"
377 tags:
378 ATag: tag1
379 Another: tag
380 state: present
381 register: cf_second_distribution
382
383 - set_fact:
384 distribution_id: '{{ cf_second_distribution.id }}'
385
386 - name: ensure that the value of 'enabled' is 'false'
387 assert:
388 that:
389 - cf_second_distribution.changed
390 - not cf_second_distribution.enabled
391
392 - name: ensure tags were set on creation
393 assert:
394 that:
395 - cf_second_distribution.changed
396 - cf_second_distribution.tags|length == 2
397 - "'ATag' in cf_second_distribution.tags"
398 - "'Another' in cf_second_distribution.tags"
399
400 - name: re-run create distribution with same tags and purge_tags
401 cloudfront_distribution:
402 distribution_id: "{{ distribution_id }}"
403 origins:
404 - domain_name: "{{ resource_prefix }}2.example.com"
405 id: "{{ resource_prefix }}2.example.com"
406 tags:
407 ATag: tag1
408 Another: tag
409 purge_tags: true
410 state: present
411 register: rerun_with_purge_tags
412
413 - name: ensure that re-running didn't change
414 assert:
415 that:
416 # - not rerun_with_purge_tags.changed
417 - rerun_with_purge_tags.tags|length == 2
418
419 - name: add new tag to distribution
420 cloudfront_distribution:
421 distribution_id: "{{ distribution_id }}"
422 origins:
423 - domain_name: "{{ resource_prefix }}2.example.com"
424 tags:
425 Third: thing
426 purge_tags: false
427 state: present
428 register: update_with_new_tag
429
430 - name: ensure tags are correct
431 assert:
432 that:
433 - update_with_new_tag.changed
434 - "'Third' in update_with_new_tag.tags"
435 - "'Another' in update_with_new_tag.tags"
436 - "'ATag' in update_with_new_tag.tags"
437 - update_with_new_tag.tags|length == 3
438
439 - name: create some cache behaviors
440 cloudfront_distribution:
441 distribution_id: "{{ distribution_id }}"
442 origins:
443 - domain_name: "{{ resource_prefix }}2.example.com"
444 cache_behaviors: "{{ cloudfront_test_cache_behaviors }}"
445 state: present
446 register: add_cache_behaviors
447
448 - name: reverse some cache behaviors
449 cloudfront_distribution:
450 distribution_id: "{{ distribution_id }}"
451 origins:
452 - domain_name: "{{ resource_prefix }}2.example.com"
453 cache_behaviors: "{{ cloudfront_test_cache_behaviors|reverse|list }}"
454 state: present
455 register: reverse_cache_behaviors
456
457 - name: check that reversing cache behaviors changes nothing when purge_cache_behaviors unset
458 assert:
459 that:
460 # - not reverse_cache_behaviors.changed
461 - reverse_cache_behaviors.cache_behaviors.elements|length == 2
462
463 - name: reverse some cache behaviors properly
464 cloudfront_distribution:
465 distribution_id: "{{ distribution_id }}"
466 origins:
467 - domain_name: "{{ resource_prefix }}2.example.com"
468 cache_behaviors: "{{ cloudfront_test_cache_behaviors|reverse|list }}"
469 purge_cache_behaviors: true
470 state: present
471 register: reverse_cache_behaviors_with_purge
472
473 - name: check that reversing cache behaviors changes nothing when purge_cache_behaviors unset
474 assert:
475 that:
476 - reverse_cache_behaviors_with_purge.changed
477 - reverse_cache_behaviors_with_purge.cache_behaviors.elements|length == 2
478
479 - name: update origin that changes target id (failure expected)
480 cloudfront_distribution:
481 distribution_id: "{{ distribution_id }}"
482 origins:
483 - domain_name: "{{ resource_prefix }}3.example.com"
484 id: "{{ resource_prefix }}3.example.com"
485 purge_origins: true
486 state: present
487 register: remove_origin_in_use
488 ignore_errors: true
489
490 - name: check that removing in use origin fails
491 assert:
492 that:
493 - remove_origin_in_use.failed
494
495 # FIXME: This currently fails due to AWS side problems
496 # not clear whether to hope they fix or prevent this issue from happening
497 #- name: update origin and update cache behavior to point to new origin
498 # cloudfront_distribution:
499 # origins:
500 # - domain_name: "{{ resource_prefix }}3.example.com"
501 # id: "{{ resource_prefix }}3.example.com"
502 # cache_behaviors:
503 # - path_pattern: /test/path
504 # target_origin_id: "{{ resource_prefix }}3.example.com"
505 # - path_pattern: /another/path
506 # target_origin_id: "{{ resource_prefix }}3.example.com"
507 # state: present
508 # register: update_cache_behaviors in use
509
510 - name: create an s3 bucket for next test
511 # note that although public-read allows reads that we want to stop with origin_access_identity,
512 # we also need to test without origin_access_identity and it's hard to change bucket perms later
513 s3_bucket:
514 name: "{{ resource_prefix }}-bucket"
515 state: present
516
517 - name: update origin to point to the s3 bucket
518 cloudfront_distribution:
519 distribution_id: "{{ distribution_id }}"
520 origins:
521 - domain_name: "{{ resource_prefix }}-bucket.s3.amazonaws.com"
522 id: "{{ resource_prefix }}3.example.com"
523 s3_origin_access_identity_enabled: true
524 state: present
525 register: update_origin_to_s3
526
527 - name: check that s3 origin access is in result
528 assert:
529 that:
530 - item.s3_origin_config.origin_access_identity.startswith('origin-access-identity/cloudfront/')
531 when: "'s3_origin_config' in item"
532 loop: "{{ update_origin_to_s3.origins['elements'] }}"
533
534 - name: update origin to remove s3 origin access identity
535 cloudfront_distribution:
536 distribution_id: "{{ distribution_id }}"
537 origins:
538 - domain_name: "{{ resource_prefix }}-bucket.s3.amazonaws.com"
539 id: "{{ resource_prefix }}3.example.com"
540 s3_origin_access_identity_enabled: false
541 state: present
542 register: update_origin_to_s3_without_origin_access
543
544 - name: check that s3 origin access is not in result
545 assert:
546 that:
547 - not item.s3_origin_config.origin_access_identity
548 when: "'s3_origin_config' in item"
549 loop: "{{ update_origin_to_s3_without_origin_access.origins['elements'] }}"
550
551 - name: delete the s3 bucket
552 s3_bucket:
553 name: "{{ resource_prefix }}-bucket"
554 state: absent
555
556 - name: check that custom_origin_config can't be used with origin_access_identity enabled
557 cloudfront_distribution:
558 distribution_id: "{{ distribution_id }}"
559 origins:
560 - domain_name: "{{ resource_prefix }}-bucket.s3.amazonaws.com"
561 id: "{{ resource_prefix }}3.example.com"
562 s3_origin_access_identity_enabled: true
563 custom_origin_config:
564 origin_protocol_policy: 'http-only'
565 state: present
566 register: update_origin_to_s3_with_origin_access_and_with_custom_origin_config
567 ignore_errors: true
568
569 - name: check that custom origin with origin access identity fails
570 # "s3 origin domains and custom_origin_config are mutually exclusive"
571 assert:
572 that:
573 - update_origin_to_s3_with_origin_access_and_with_custom_origin_config.failed
574
575 - name: check that custom_origin_config can't be used with an region-agnostic S3 domain
576 cloudfront_distribution:
577 distribution_id: "{{ distribution_id }}"
578 origins:
579 - domain_name: "{{ resource_prefix }}-bucket.s3.{{ aws_region }}.amazonaws.com"
580 id: "{{ resource_prefix }}3.example.com"
581 custom_origin_config:
582 http_port: 8080
583 state: present
584 register: update_origin_to_s3_with_origin_access_and_with_custom_origin_config
585 ignore_errors: true
586
587 - name: check that custom origin with region-agnostic S3 domain fails
588 # "s3 origin domains and custom_origin_config are mutually exclusive"
589 assert:
590 that:
591 - update_origin_to_s3_with_origin_access_and_with_custom_origin_config.failed
592
593 - name: check that custom_origin_config can't be used with an region-aware S3 domain
594 cloudfront_distribution:
595 distribution_id: "{{ distribution_id }}"
596 origins:
597 - domain_name: "{{ resource_prefix }}-bucket.s3.amazonaws.com"
598 id: "{{ resource_prefix }}3.example.com"
599 custom_origin_config:
600 http_port: 8080
601 state: present
602 register: update_origin_to_s3_with_origin_access_and_with_custom_origin_config
603 ignore_errors: true
604
605 - name: check that custom origin with region-aware S3 domain fails
606 # "s3 origin domains and custom_origin_config are mutually exclusive"
607 assert:
608 that:
609 - update_origin_to_s3_with_origin_access_and_with_custom_origin_config.failed
610
611 - name: create cloudfront distribution origin access identity
612 cloudfront_origin_access_identity:
613 state: present
614 comment: "this is a sample origin access identity"
615 register: _origin_access_id
616
617 - set_fact:
618 origin_access_identity: 'origin-access-identity/cloudfront/{{ _origin_access_id.cloud_front_origin_access_identity.id }}'
619
620 - name: Update distribution to use specific access identity
621 cloudfront_distribution:
622 distribution_id: "{{ distribution_id }}"
623 origins:
624 - id: "{{ resource_prefix }}"
625 domain_name: "{{ resource_prefix }}.s3.amazonaws.com"
626 s3_origin_access_identity_enabled: true
627 s3_origin_config:
628 origin_access_identity: '{{ origin_access_identity }}'
629 register: result
630
631 - name: check that custom origin uses the provided origin_access_identity
632 assert:
633 that:
634 - result.changed
635 - result.origins['quantity'] > 0
636 - result.origins['elements'] | selectattr('s3_origin_config', 'defined') | map(attribute='s3_origin_config') | selectattr('origin_access_identity', 'eq', origin_access_identity) | list | length == 1
637
638 - name: update distribution to use cache_policy_id and origin_request_policy_id
639 cloudfront_distribution:
640 distribution_id: "{{ distribution_id }}"
641 default_cache_behavior:
642 cache_policy_id: "658327ea-f89d-4fab-a63d-7e88639e58f6"
643 origin_request_policy_id: "88a5eaf4-2fd4-4709-b370-b4c650ea3fcf"
644 state: present
645 register: update_distribution_with_cache_policies
646
647 - name: ensure that the cache_policy_id and origin_request_policy_id was set
648 assert:
649 that:
650 - update_distribution_with_cache_policies.changed
651 - update_distribution_with_cache_policies.default_cache_behavior.cache_policy_id == '658327ea-f89d-4fab-a63d-7e88639e58f6'
652 - update_distribution_with_cache_policies.default_cache_behavior.origin_request_policy_id == '88a5eaf4-2fd4-4709-b370-b4c650ea3fcf'
653
654 always:
655 # TEARDOWN STARTS HERE
656 - name: delete the s3 bucket
657 s3_bucket:
658 name: "{{ resource_prefix }}-bucket"
659 state: absent
660 force: true
661 ignore_errors: true
662
663 - name: clean up cloudfront distribution
664 cloudfront_distribution:
665 distribution_id: "{{ item }}"
666 enabled: false
667 wait: true
668 state: absent
669 register: delete_distribution
670 ignore_errors: true
671 async: 1000
672 poll: 0
673 with_items:
674 - '{{ cf_second_distribution.id }}'
675 - '{{ cf_distribution.id }}'
676
677 - name: Wait for cloudfront to be deleted
678 async_status:
679 jid: "{{ item.ansible_job_id }}"
680 register: _delete
681 until: _delete.finished
682 retries: 100
683 delay: 5
684 loop: "{{ delete_distribution.results }}"
685 ignore_errors: true