/
main.yml
823 lines (711 loc) · 26.7 KB
/
main.yml
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
---
# Test suite for ec2_ami
- module_defaults:
group/aws:
aws_region: "{{ aws_region }}"
access_key: "{{ aws_access_key }}"
secret_key: "{{ aws_secret_key }}"
session_token: "{{ security_token | default(omit) }}"
collections:
- amazon.aws
block:
# ============================================================
# SETUP: vpc, ec2 key pair, subnet, security group, ec2 instance, snapshot
- name: create a VPC to work in
amazon.aws.ec2_vpc_net:
cidr_block: "{{ vpc_cidr }}"
state: present
name: "{{ ec2_ami_name }}_setup"
resource_tags:
Name: "{{ ec2_ami_name }}_setup"
register: setup_vpc
- name: create a key pair to use for creating an ec2 instance
amazon.aws.ec2_key:
name: "{{ ec2_ami_name }}_setup"
state: present
register: setup_key
- name: create a subnet to use for creating an ec2 instance
amazon.aws.ec2_vpc_subnet:
az: "{{ availability_zone }}"
tags: "{{ ec2_ami_name }}_setup"
vpc_id: "{{ setup_vpc.vpc.id }}"
cidr: "{{ subnet_cidr }}"
state: present
resource_tags:
Name: "{{ ec2_ami_name }}_setup"
register: setup_subnet
- name: create a security group to use for creating an ec2 instance
amazon.aws.ec2_security_group:
name: "{{ ec2_ami_name }}_setup"
description: created by Ansible integration tests
state: present
vpc_id: "{{ setup_vpc.vpc.id }}"
register: setup_sg
- name: provision ec2 instance to create an image
amazon.aws.ec2_instance:
state: running
key_name: "{{ setup_key.key.name }}"
instance_type: t2.micro
image_id: "{{ ec2_ami_id }}"
tags:
"{{ec2_ami_name}}_instance_setup": integration_tests
security_group: "{{ setup_sg.group_id }}"
vpc_subnet_id: "{{ setup_subnet.subnet.id }}"
volumes:
- device_name: /dev/sdc
virtual_name: ephemeral1
wait: true
register: setup_instance
- name: Store EC2 Instance ID
ansible.builtin.set_fact:
ec2_instance_id: "{{ setup_instance.instances[0].instance_id }}"
- name: take a snapshot of the instance to create an image
amazon.aws.ec2_snapshot:
instance_id: "{{ ec2_instance_id }}"
device_name: "{{ ec2_ami_root_disk }}"
state: present
register: setup_snapshot
# note: the current CI supported instance types (t2, t3, m1) do not support uefi boot mode + tpm_support
# disabling the task as aws documentation states that support for t3 will be coming soon
# - name: get instance UEFI data
# command: aws ec2 get-instance-uefi-data --instance-id {{ ec2_instance_id }} --region {{ aws_region }}
# environment:
# AWS_ACCESS_KEY_ID: "{{ aws_access_key }}"
# AWS_SECRET_ACCESS_KEY: "{{ aws_secret_key }}"
# AWS_SESSION_TOKEN: "{{ security_token | default('') }}"
# AWS_DEFAULT_REGION: "{{ aws_region }}"
# register: instance_uefi_data_output
# - name: Convert it to an object
# set_fact:
# instance_uefi_data: "{{ instance_uefi_data_output.stdout | from_json }}"
# ============================================================
- name: test clean failure if not providing image_id or name with state=present
amazon.aws.ec2_ami:
instance_id: "{{ ec2_instance_id }}"
state: present
description: "{{ ec2_ami_description }}"
tags:
Name: "{{ ec2_ami_name }}_ami"
wait: true
root_device_name: "{{ ec2_ami_root_disk }}"
register: result
ignore_errors: true
- name: assert error message is helpful
ansible.builtin.assert:
that:
- result.failed
- "result.msg == 'one of the following is required: name, image_id'"
# ============================================================
- name: create an image from the instance (check mode)
amazon.aws.ec2_ami:
instance_id: "{{ ec2_instance_id }}"
state: present
name: "{{ ec2_ami_name }}_ami"
description: "{{ ec2_ami_description }}"
tags:
Name: "{{ ec2_ami_name }}_ami"
wait: true
root_device_name: "{{ ec2_ami_root_disk }}"
check_mode: true
register: check_mode_result
- name: assert that check_mode result is changed
ansible.builtin.assert:
that:
- check_mode_result is changed
- name: create an image from the instance
amazon.aws.ec2_ami:
instance_id: "{{ ec2_instance_id }}"
state: present
name: "{{ ec2_ami_name }}_ami"
description: "{{ ec2_ami_description }}"
tags:
Name: "{{ ec2_ami_name }}_ami"
wait: true
root_device_name: "{{ ec2_ami_root_disk }}"
register: result
- name: set image id fact for deletion later
ansible.builtin.set_fact:
ec2_ami_image_id: "{{ result.image_id }}"
- name: assert that image has been created
ansible.builtin.assert:
that:
- result.changed
- result.image_id.startswith('ami-')
- "'Name' in result.tags and result.tags.Name == ec2_ami_name + '_ami'"
- name: get related snapshot info and ensure the tags have been propagated
amazon.aws.ec2_snapshot_info:
snapshot_ids:
- "{{ result.block_device_mapping[ec2_ami_root_disk].snapshot_id }}"
register: snapshot_result
- name: ensure the tags have been propagated to the snapshot
ansible.builtin.assert:
that:
- "'tags' in snapshot_result.snapshots[0]"
- "'Name' in snapshot_result.snapshots[0].tags and snapshot_result.snapshots[0].tags.Name == ec2_ami_name + '_ami'"
# ============================================================
- name: create an image from the instance with attached devices with no_device true (check mode)
amazon.aws.ec2_ami:
name: "{{ ec2_ami_name }}_no_device_true_ami"
instance_id: "{{ ec2_instance_id }}"
device_mapping:
- device_name: /dev/sda1
volume_size: 10
delete_on_termination: true
volume_type: gp2
- device_name: /dev/sdf
no_device: true
state: present
wait: true
root_device_name: "{{ ec2_ami_root_disk }}"
check_mode: true
register: check_mode_result
- name: assert that check_mode result is changed
ansible.builtin.assert:
that:
- check_mode_result is changed
- name: create an image from the instance with attached devices with no_device true
amazon.aws.ec2_ami:
name: "{{ ec2_ami_name }}_no_device_true_ami"
instance_id: "{{ ec2_instance_id }}"
device_mapping:
- device_name: /dev/sda1
volume_size: 10
delete_on_termination: true
volume_type: gp2
- device_name: /dev/sdf
no_device: true
state: present
wait: true
root_device_name: "{{ ec2_ami_root_disk }}"
register: result_no_device_true
- name: set image id fact for deletion later
ansible.builtin.set_fact:
ec2_ami_no_device_true_image_id: "{{ result_no_device_true.image_id }}"
- name: assert that image with no_device option yes has been created
ansible.builtin.assert:
that:
- result_no_device_true.changed
- "'/dev/sdf' not in result_no_device_true.block_device_mapping"
- name: create an image from the instance with attached devices with no_device false
amazon.aws.ec2_ami:
name: "{{ ec2_ami_name }}_no_device_false_ami"
instance_id: "{{ ec2_instance_id }}"
device_mapping:
- device_name: /dev/sda1
volume_size: 10
delete_on_termination: true
volume_type: gp2
no_device: false
state: present
wait: true
root_device_name: "{{ ec2_ami_root_disk }}"
register: result_no_device_false
- name: set image id fact for deletion later
ansible.builtin.set_fact:
ec2_ami_no_device_false_image_id: "{{ result_no_device_false.image_id }}"
- name: assert that image with no_device option no has been created
ansible.builtin.assert:
that:
- result_no_device_false.changed
- "'/dev/sda1' in result_no_device_false.block_device_mapping"
# ============================================================
- name: gather facts about the image created
amazon.aws.ec2_ami_info:
image_ids: "{{ ec2_ami_image_id }}"
register: ami_facts_result
ignore_errors: true
- name: assert that the right image was found
ansible.builtin.assert:
that:
- ami_facts_result.images[0].image_id == ec2_ami_image_id
# some ec2_ami_info tests to test if the filtering is working fine.
# ============================================================
- name: gather info about the image
amazon.aws.ec2_ami_info:
image_ids: "{{ ec2_region_images[ec2_region] }}"
register: ami_info_result
ignore_errors: true
- name: assert that the right image was found
ansible.builtin.assert:
that:
- ami_info_result.images[0].image_id == ec2_region_images[ec2_region]
# ============================================================
- name: gather info about the image using boolean filter
amazon.aws.ec2_ami_info:
image_ids: "{{ ec2_region_images[ec2_region] }}"
filters:
is-public: true
register: ami_info_result
ignore_errors: true
- name: assert that the right image was found
ansible.builtin.assert:
that:
- ami_info_result.images[0].image_id == ec2_region_images[ec2_region]
# ============================================================
- name: gather info about the image using integer filter
amazon.aws.ec2_ami_info:
image_ids: "{{ ec2_region_images[ec2_region] }}"
filters:
# Amazon owned
owner-id: 137112412989
register: ami_info_result
ignore_errors: true
- name: assert that the right image was found
ansible.builtin.assert:
that:
- ami_info_result.images[0].image_id == ec2_region_images[ec2_region]
# ============================================================
- name: gather info about the image using string filter
amazon.aws.ec2_ami_info:
image_ids: "{{ ec2_region_images[ec2_region] }}"
filters:
name: amzn-ami-hvm-2017.09.0.20170930-x86_64-gp2
register: ami_info_result
ignore_errors: true
- name: assert that the right image was found
ansible.builtin.assert:
that:
- ami_info_result.images[0].image_id == ec2_region_images[ec2_region]
# e2_ami_info filtering tests ends
# ============================================================
- name: delete the image (check mode)
amazon.aws.ec2_ami:
instance_id: "{{ ec2_instance_id }}"
state: absent
delete_snapshot: true
name: "{{ ec2_ami_name }}_ami"
description: "{{ ec2_ami_description }}"
image_id: "{{ result.image_id }}"
tags:
Name: "{{ ec2_ami_name }}_ami"
wait: true
ignore_errors: true
check_mode: true
register: check_mode_result
- name: assert that check_mode result is changed
ansible.builtin.assert:
that:
- check_mode_result is changed
- name: delete the image
amazon.aws.ec2_ami:
instance_id: "{{ ec2_instance_id }}"
state: absent
delete_snapshot: true
name: "{{ ec2_ami_name }}_ami"
description: "{{ ec2_ami_description }}"
image_id: "{{ result.image_id }}"
tags:
Name: "{{ ec2_ami_name }}_ami"
wait: true
ignore_errors: true
register: result
- name: assert that the image has been deleted
ansible.builtin.assert:
that:
- result.changed
- "'image_id' not in result"
- result.snapshots_deleted
# ==============================================================
- name: test removing an ami if no image ID is provided (expected failed=true)
amazon.aws.ec2_ami:
state: absent
register: result
ignore_errors: true
- name: assert that an image ID is required
ansible.builtin.assert:
that:
- result.failed
- "result.msg == 'state is absent but all of the following are missing: image_id'"
# ============================================================
- name: create an image from the snapshot
amazon.aws.ec2_ami:
name: "{{ ec2_ami_name }}_ami"
description: "{{ ec2_ami_description }}"
state: present
launch_permissions:
user_ids: []
tags:
Name: "{{ ec2_ami_name }}_ami"
root_device_name: "{{ ec2_ami_root_disk }}"
device_mapping:
- device_name: "{{ ec2_ami_root_disk }}"
volume_type: gp2
size: 8
delete_on_termination: true
snapshot_id: "{{ setup_snapshot.snapshot_id }}"
register: result
ignore_errors: true
- name: set image id fact for deletion later
ansible.builtin.set_fact:
ec2_ami_image_id: "{{ result.image_id }}"
ec2_ami_snapshot: "{{ result.block_device_mapping[ec2_ami_root_disk].snapshot_id }}"
- name: assert a new ami has been created
ansible.builtin.assert:
that:
- result.changed
- result.image_id.startswith('ami-')
# ============================================================
- name: test default launch permissions idempotence (check mode)
amazon.aws.ec2_ami:
description: "{{ ec2_ami_description }}"
state: present
name: "{{ ec2_ami_name }}_ami"
tags:
Name: "{{ ec2_ami_name }}_ami"
root_device_name: "{{ ec2_ami_root_disk }}"
image_id: "{{ result.image_id }}"
launch_permissions:
user_ids: []
device_mapping:
- device_name: "{{ ec2_ami_root_disk }}"
volume_type: gp2
size: 8
delete_on_termination: true
snapshot_id: "{{ setup_snapshot.snapshot_id }}"
check_mode: true
register: check_mode_result
- name: assert that check_mode result is not changed
ansible.builtin.assert:
that:
- check_mode_result is not changed
- name: test default launch permissions idempotence
amazon.aws.ec2_ami:
description: "{{ ec2_ami_description }}"
state: present
name: "{{ ec2_ami_name }}_ami"
tags:
Name: "{{ ec2_ami_name }}_ami"
root_device_name: "{{ ec2_ami_root_disk }}"
image_id: "{{ result.image_id }}"
launch_permissions:
user_ids: []
device_mapping:
- device_name: "{{ ec2_ami_root_disk }}"
volume_type: gp2
size: 8
delete_on_termination: true
snapshot_id: "{{ setup_snapshot.snapshot_id }}"
register: result
- name: assert a new ami has not been created
ansible.builtin.assert:
that:
- not result.changed
- result.image_id.startswith('ami-')
# ============================================================
- name: add a tag to the AMI
amazon.aws.ec2_ami:
state: present
description: "{{ ec2_ami_description }}"
image_id: "{{ result.image_id }}"
name: "{{ ec2_ami_name }}_ami"
tags:
New: Tag
purge_tags: false
register: result
- name: assert a tag was added
ansible.builtin.assert:
that:
- "'Name' in result.tags and result.tags.Name == ec2_ami_name + '_ami'"
- "'New' in result.tags and result.tags.New == 'Tag'"
- name: use purge_tags to remove a tag from the AMI
amazon.aws.ec2_ami:
state: present
description: "{{ ec2_ami_description }}"
image_id: "{{ result.image_id }}"
name: "{{ ec2_ami_name }}_ami"
tags:
New: Tag
register: result
- name: assert a tag was removed
ansible.builtin.assert:
that:
- "'Name' not in result.tags"
- "'New' in result.tags and result.tags.New == 'Tag'"
# ============================================================
- name: update AMI launch permissions (check mode)
amazon.aws.ec2_ami:
state: present
image_id: "{{ result.image_id }}"
description: "{{ ec2_ami_description }}"
tags:
Name: "{{ ec2_ami_name }}_ami"
launch_permissions:
group_names: [all]
check_mode: true
register: check_mode_result
- name: assert that check_mode result is changed
ansible.builtin.assert:
that:
- check_mode_result is changed
- name: update AMI launch permissions
amazon.aws.ec2_ami:
state: present
image_id: "{{ result.image_id }}"
description: "{{ ec2_ami_description }}"
tags:
Name: "{{ ec2_ami_name }}_ami"
launch_permissions:
group_names: [all]
register: result
- name: assert launch permissions were updated
ansible.builtin.assert:
that:
- result.changed
# ============================================================
- name: modify the AMI description (check mode)
amazon.aws.ec2_ami:
state: present
image_id: "{{ result.image_id }}"
name: "{{ ec2_ami_name }}_ami"
description: "{{ ec2_ami_description }}CHANGED"
tags:
Name: "{{ ec2_ami_name }}_ami"
launch_permissions:
group_names: [all]
check_mode: true
register: check_mode_result
- name: assert that check_mode result is changed
ansible.builtin.assert:
that:
- check_mode_result is changed
- name: modify the AMI description
amazon.aws.ec2_ami:
state: present
image_id: "{{ result.image_id }}"
name: "{{ ec2_ami_name }}_ami"
description: "{{ ec2_ami_description }}CHANGED"
tags:
Name: "{{ ec2_ami_name }}_ami"
launch_permissions:
group_names: [all]
register: result
- name: assert the description changed
ansible.builtin.assert:
that:
- result.changed
# ============================================================
- name: remove public launch permissions
amazon.aws.ec2_ami:
state: present
image_id: "{{ result.image_id }}"
name: "{{ ec2_ami_name }}_ami"
tags:
Name: "{{ ec2_ami_name }}_ami"
launch_permissions:
group_names: []
register: result
- name: assert launch permissions were updated
ansible.builtin.assert:
that:
- result.changed
# ============================================================
- name: delete ami without deleting the snapshot (default is not to delete)
amazon.aws.ec2_ami:
instance_id: "{{ ec2_instance_id }}"
state: absent
name: "{{ ec2_ami_name }}_ami"
image_id: "{{ ec2_ami_image_id }}"
tags:
Name: "{{ ec2_ami_name }}_ami"
wait: true
ignore_errors: true
register: result
- name: assert that the image has been deleted
ansible.builtin.assert:
that:
- result.changed
- "'image_id' not in result"
- name: ensure the snapshot still exists
amazon.aws.ec2_snapshot_info:
snapshot_ids:
- "{{ ec2_ami_snapshot }}"
register: snapshot_result
- name: assert the snapshot wasn't deleted
ansible.builtin.assert:
that:
- snapshot_result.snapshots[0].snapshot_id == ec2_ami_snapshot
- name: delete ami for a second time (check mode)
amazon.aws.ec2_ami:
instance_id: "{{ ec2_instance_id }}"
state: absent
name: "{{ ec2_ami_name }}_ami"
image_id: "{{ ec2_ami_image_id }}"
tags:
Name: "{{ ec2_ami_name }}_ami"
wait: true
check_mode: true
register: check_mode_result
- name: assert that check_mode result is not changed
ansible.builtin.assert:
that:
- check_mode_result is not changed
- name: delete ami for a second time
amazon.aws.ec2_ami:
instance_id: "{{ ec2_instance_id }}"
state: absent
name: "{{ ec2_ami_name }}_ami"
image_id: "{{ ec2_ami_image_id }}"
tags:
Name: "{{ ec2_ami_name }}_ami"
wait: true
register: result
- name: assert that image does not exist
ansible.builtin.assert:
that:
- not result.changed
- not result.failed
# ============================================================
- name: create an image from the snapshot with boot_mode and tpm_support
amazon.aws.ec2_ami:
name: "{{ ec2_ami_name }}_ami-boot-tpm"
description: "{{ ec2_ami_description }}"
state: present
boot_mode: uefi
tpm_support: v2.0
launch_permissions:
user_ids: []
tags:
Name: "{{ ec2_ami_name }}_ami-boot-tpm"
root_device_name: "{{ ec2_ami_root_disk }}"
device_mapping:
- device_name: "{{ ec2_ami_root_disk }}"
volume_type: gp2
size: 8
delete_on_termination: true
snapshot_id: "{{ setup_snapshot.snapshot_id }}"
register: result
ignore_errors: true
- name: set image id fact for deletion later
ansible.builtin.set_fact:
ec2_ami_image_id_boot_tpm: "{{ result.image_id }}"
ec2_ami_snapshot_boot_tpm: "{{ result.block_device_mapping[ec2_ami_root_disk].snapshot_id }}"
- name: gather facts about the image created
amazon.aws.ec2_ami_info:
image_ids: "{{ ec2_ami_image_id_boot_tpm }}"
register: ami_facts_result_boot_tpm
ignore_errors: true
- name: assert that new ami has been created with desired options
ansible.builtin.assert:
that:
- result.changed
- result.image_id.startswith('ami-')
- ami_facts_result_boot_tpm.images[0].image_id | length != 0
- ami_facts_result_boot_tpm.images[0].boot_mode == 'uefi'
- ami_facts_result_boot_tpm.images[0].tpm_support == 'v2.0'
# === Test modify launch permissions org_arns and org_unit_arns=========================
- name: create an image from the instance
amazon.aws.ec2_ami:
instance_id: "{{ ec2_instance_id }}"
state: present
name: "{{ ec2_ami_name }}_permissions"
description: "{{ ec2_ami_description }}"
tags:
Name: "{{ ec2_ami_name }}_permissions"
wait: true
root_device_name: "{{ ec2_ami_root_disk }}"
register: permissions_create_result
- name: modify the AMI launch permissions
amazon.aws.ec2_ami:
state: present
image_id: "{{ permissions_create_result.image_id }}"
name: "{{ ec2_ami_name }}_permissions"
tags:
Name: "{{ ec2_ami_name }}_permissions"
launch_permissions:
org_arns: ["arn:aws:organizations::123456789012:organization/o-123ab4cdef"]
org_unit_arns: ["arn:aws:organizations::123456789012:ou/o-123example/ou-1234-5exampld"]
register: permissions_update_result
- name: Get ami info
amazon.aws.ec2_ami_info:
image_ids: "{{ permissions_create_result.image_id }}"
describe_image_attributes: true
register: permissions_info_result
- name: assert that launch permissions have changed
ansible.builtin.assert:
that:
- permissions_update_result.changed
- "'organization_arn' in permissions_info_result.images[0].launch_permissions[0]"
- permissions_info_result.images[0].launch_permissions[0]['organization_arn'] == 'arn:aws:organizations::123456789012:organization/o-123ab4cdef'
- "'organizational_unit_arn' in permissions_info_result.images[0].launch_permissions[1]"
- permissions_info_result.images[0].launch_permissions[1]['organizational_unit_arn'] == 'arn:aws:organizations::123456789012:ou/o-123example/ou-1234-5exampld'
# ============================================================
always:
# ============================================================
# TEAR DOWN: snapshot, ec2 instance, ec2 key pair, security group, vpc
- name: Announce teardown start
ansible.builtin.debug:
msg: "***** TESTING COMPLETE. COMMENCE TEARDOWN *****"
- name: delete ami
amazon.aws.ec2_ami:
state: absent
image_id: "{{ ec2_ami_image_id_boot_tpm }}"
wait: true
ignore_errors: true
- name: delete ami
amazon.aws.ec2_ami:
state: absent
image_id: "{{ ec2_ami_image_id }}"
name: "{{ ec2_ami_name }}_ami"
wait: true
ignore_errors: true
- name: delete ami
amazon.aws.ec2_ami:
state: absent
image_id: "{{ ec2_ami_no_device_true_image_id }}"
wait: true
ignore_errors: true
- name: delete ami
amazon.aws.ec2_ami:
state: absent
image_id: "{{ ec2_ami_no_device_false_image_id }}"
wait: true
ignore_errors: true
- name: delete ami
amazon.aws.ec2_ami:
state: absent
image_id: "{{ ec2_ami_image_id }}"
name: "{{ ec2_ami_name }}_permissions"
wait: true
ignore_errors: true
- name: remove setup snapshot of ec2 instance
amazon.aws.ec2_snapshot:
state: absent
snapshot_id: "{{ setup_snapshot.snapshot_id }}"
ignore_errors: true
- name: remove setup ec2 instance
amazon.aws.ec2_instance:
state: absent
instance_ids:
- "{{ ec2_instance_id }}"
wait: true
ignore_errors: true
- name: remove setup keypair
amazon.aws.ec2_key:
name: "{{ec2_ami_name}}_setup"
state: absent
ignore_errors: true
- name: remove setup security group
amazon.aws.ec2_security_group:
name: "{{ ec2_ami_name }}_setup"
description: created by Ansible integration tests
state: absent
vpc_id: "{{ setup_vpc.vpc.id }}"
ignore_errors: true
- name: remove setup subnet
amazon.aws.ec2_vpc_subnet:
az: "{{ availability_zone }}"
tags: "{{ec2_ami_name}}_setup"
vpc_id: "{{ setup_vpc.vpc.id }}"
cidr: "{{ subnet_cidr }}"
state: absent
resource_tags:
Name: "{{ ec2_ami_name }}_setup"
ignore_errors: true
- name: remove setup VPC
amazon.aws.ec2_vpc_net:
cidr_block: "{{ vpc_cidr }}"
state: absent
name: "{{ ec2_ami_name }}_setup"
resource_tags:
Name: "{{ ec2_ami_name }}_setup"
ignore_errors: true