| 1 |
--- |
| 2 |
# Test suite for ec2_ami |
| 3 |
- module_defaults: |
| 4 |
group/aws: |
| 5 |
aws_region: "{{ aws_region }}" |
| 6 |
access_key: "{{ aws_access_key }}" |
| 7 |
secret_key: "{{ aws_secret_key }}" |
| 8 |
session_token: "{{ security_token | default(omit) }}" |
| 9 |
block: |
| 10 |
# ============================================================ |
| 11 |
|
| 12 |
# SETUP: vpc, ec2 key pair, subnet, security group, ec2 instance, snapshot |
| 13 |
- name: Create a VPC to work in |
| 14 |
amazon.aws.ec2_vpc_net: |
| 15 |
cidr_block: "{{ vpc_cidr }}" |
| 16 |
state: present |
| 17 |
name: "{{ ec2_ami_name }}_setup" |
| 18 |
resource_tags: |
| 19 |
Name: "{{ ec2_ami_name }}_setup" |
| 20 |
register: setup_vpc |
| 21 |
|
| 22 |
- name: Create a key pair to use for creating an ec2 instance |
| 23 |
amazon.aws.ec2_key: |
| 24 |
name: "{{ ec2_ami_name }}_setup" |
| 25 |
state: present |
| 26 |
register: setup_key |
| 27 |
|
| 28 |
- name: Create a subnet to use for creating an ec2 instance |
| 29 |
amazon.aws.ec2_vpc_subnet: |
| 30 |
az: "{{ availability_zone }}" |
| 31 |
tags: "{{ ec2_ami_name }}_setup" |
| 32 |
vpc_id: "{{ setup_vpc.vpc.id }}" |
| 33 |
cidr: "{{ subnet_cidr }}" |
| 34 |
state: present |
| 35 |
resource_tags: |
| 36 |
Name: "{{ ec2_ami_name }}_setup" |
| 37 |
register: setup_subnet |
| 38 |
|
| 39 |
- name: Create a security group to use for creating an ec2 instance |
| 40 |
amazon.aws.ec2_security_group: |
| 41 |
name: "{{ ec2_ami_name }}_setup" |
| 42 |
description: created by Ansible integration tests |
| 43 |
state: present |
| 44 |
vpc_id: "{{ setup_vpc.vpc.id }}" |
| 45 |
register: setup_sg |
| 46 |
|
| 47 |
- name: Provision ec2 instance to create an image |
| 48 |
amazon.aws.ec2_instance: |
| 49 |
state: running |
| 50 |
key_name: "{{ setup_key.key.name }}" |
| 51 |
instance_type: t2.micro |
| 52 |
image_id: "{{ ec2_ami_id }}" |
| 53 |
tags: |
| 54 |
"{{ec2_ami_name}}_instance_setup": integration_tests |
| 55 |
security_group: "{{ setup_sg.group_id }}" |
| 56 |
vpc_subnet_id: "{{ setup_subnet.subnet.id }}" |
| 57 |
volumes: |
| 58 |
- device_name: /dev/sdc |
| 59 |
virtual_name: ephemeral1 |
| 60 |
wait: true |
| 61 |
register: setup_instance |
| 62 |
|
| 63 |
- name: Store EC2 Instance ID |
| 64 |
ansible.builtin.set_fact: |
| 65 |
ec2_instance_id: "{{ setup_instance.instances[0].instance_id }}" |
| 66 |
|
| 67 |
- name: Take a snapshot of the instance to create an image |
| 68 |
amazon.aws.ec2_snapshot: |
| 69 |
instance_id: "{{ ec2_instance_id }}" |
| 70 |
device_name: "{{ ec2_ami_root_disk }}" |
| 71 |
state: present |
| 72 |
register: setup_snapshot |
| 73 |
|
| 74 |
# note: the current CI supported instance types (t2, t3, m1) do not support uefi boot mode + tpm_support |
| 75 |
# disabling the task as aws documentation states that support for t3 will be coming soon |
| 76 |
# - name: get instance UEFI data |
| 77 |
# command: aws ec2 get-instance-uefi-data --instance-id {{ ec2_instance_id }} --region {{ aws_region }} |
| 78 |
# environment: |
| 79 |
# AWS_ACCESS_KEY_ID: "{{ aws_access_key }}" |
| 80 |
# AWS_SECRET_ACCESS_KEY: "{{ aws_secret_key }}" |
| 81 |
# AWS_SESSION_TOKEN: "{{ security_token | default('') }}" |
| 82 |
# AWS_DEFAULT_REGION: "{{ aws_region }}" |
| 83 |
# register: instance_uefi_data_output |
| 84 |
|
| 85 |
# - name: Convert it to an object |
| 86 |
# set_fact: |
| 87 |
# instance_uefi_data: "{{ instance_uefi_data_output.stdout | from_json }}" |
| 88 |
|
| 89 |
# ============================================================ |
| 90 |
|
| 91 |
- name: Test clean failure if not providing image_id or name with state=present |
| 92 |
amazon.aws.ec2_ami: |
| 93 |
instance_id: "{{ ec2_instance_id }}" |
| 94 |
state: present |
| 95 |
description: "{{ ec2_ami_description }}" |
| 96 |
tags: |
| 97 |
Name: "{{ ec2_ami_name }}_ami" |
| 98 |
wait: true |
| 99 |
root_device_name: "{{ ec2_ami_root_disk }}" |
| 100 |
register: result |
| 101 |
ignore_errors: true |
| 102 |
|
| 103 |
- name: Assert error message is helpful |
| 104 |
ansible.builtin.assert: |
| 105 |
that: |
| 106 |
- result.failed |
| 107 |
- "result.msg == 'one of the following is required: name, image_id'" |
| 108 |
|
| 109 |
# ============================================================ |
| 110 |
|
| 111 |
- name: Create an image from the instance (check mode) |
| 112 |
amazon.aws.ec2_ami: |
| 113 |
instance_id: "{{ ec2_instance_id }}" |
| 114 |
state: present |
| 115 |
name: "{{ ec2_ami_name }}_ami" |
| 116 |
description: "{{ ec2_ami_description }}" |
| 117 |
tags: |
| 118 |
Name: "{{ ec2_ami_name }}_ami" |
| 119 |
wait: true |
| 120 |
root_device_name: "{{ ec2_ami_root_disk }}" |
| 121 |
check_mode: true |
| 122 |
register: check_mode_result |
| 123 |
|
| 124 |
- name: Assert that check_mode result is changed |
| 125 |
ansible.builtin.assert: |
| 126 |
that: |
| 127 |
- check_mode_result is changed |
| 128 |
|
| 129 |
- name: Create an image from the instance |
| 130 |
amazon.aws.ec2_ami: |
| 131 |
instance_id: "{{ ec2_instance_id }}" |
| 132 |
state: present |
| 133 |
name: "{{ ec2_ami_name }}_ami" |
| 134 |
description: "{{ ec2_ami_description }}" |
| 135 |
tags: |
| 136 |
Name: "{{ ec2_ami_name }}_ami" |
| 137 |
wait: true |
| 138 |
root_device_name: "{{ ec2_ami_root_disk }}" |
| 139 |
register: result |
| 140 |
|
| 141 |
- name: Set image id fact for deletion later |
| 142 |
ansible.builtin.set_fact: |
| 143 |
ec2_ami_image_id: "{{ result.image_id }}" |
| 144 |
|
| 145 |
- name: Assert that image has been created |
| 146 |
ansible.builtin.assert: |
| 147 |
that: |
| 148 |
- result.changed |
| 149 |
- result.image_id.startswith('ami-') |
| 150 |
- "'Name' in result.tags and result.tags.Name == ec2_ami_name + '_ami'" |
| 151 |
|
| 152 |
- name: Get related snapshot info and ensure the tags have been propagated |
| 153 |
amazon.aws.ec2_snapshot_info: |
| 154 |
snapshot_ids: |
| 155 |
- "{{ result.block_device_mapping[ec2_ami_root_disk].snapshot_id }}" |
| 156 |
register: snapshot_result |
| 157 |
|
| 158 |
- name: Ensure the tags have been propagated to the snapshot |
| 159 |
ansible.builtin.assert: |
| 160 |
that: |
| 161 |
- "'tags' in snapshot_result.snapshots[0]" |
| 162 |
- "'Name' in snapshot_result.snapshots[0].tags and snapshot_result.snapshots[0].tags.Name == ec2_ami_name + '_ami'" |
| 163 |
|
| 164 |
# ============================================================ |
| 165 |
|
| 166 |
- name: Create an image from the instance with attached devices with no_device true (check mode) |
| 167 |
amazon.aws.ec2_ami: |
| 168 |
name: "{{ ec2_ami_name }}_no_device_true_ami" |
| 169 |
instance_id: "{{ ec2_instance_id }}" |
| 170 |
device_mapping: |
| 171 |
- device_name: /dev/sda1 |
| 172 |
volume_size: 10 |
| 173 |
delete_on_termination: true |
| 174 |
volume_type: gp2 |
| 175 |
- device_name: /dev/sdf |
| 176 |
no_device: true |
| 177 |
state: present |
| 178 |
wait: true |
| 179 |
root_device_name: "{{ ec2_ami_root_disk }}" |
| 180 |
check_mode: true |
| 181 |
register: check_mode_result |
| 182 |
|
| 183 |
- name: Assert that check_mode result is changed |
| 184 |
ansible.builtin.assert: |
| 185 |
that: |
| 186 |
- check_mode_result is changed |
| 187 |
|
| 188 |
- name: Create an image from the instance with attached devices with no_device true |
| 189 |
amazon.aws.ec2_ami: |
| 190 |
name: "{{ ec2_ami_name }}_no_device_true_ami" |
| 191 |
instance_id: "{{ ec2_instance_id }}" |
| 192 |
device_mapping: |
| 193 |
- device_name: /dev/sda1 |
| 194 |
volume_size: 10 |
| 195 |
delete_on_termination: true |
| 196 |
volume_type: gp2 |
| 197 |
- device_name: /dev/sdf |
| 198 |
no_device: true |
| 199 |
state: present |
| 200 |
wait: true |
| 201 |
root_device_name: "{{ ec2_ami_root_disk }}" |
| 202 |
register: result_no_device_true |
| 203 |
|
| 204 |
- name: Set image id fact for deletion later |
| 205 |
ansible.builtin.set_fact: |
| 206 |
ec2_ami_no_device_true_image_id: "{{ result_no_device_true.image_id }}" |
| 207 |
|
| 208 |
- name: Assert that image with no_device option yes has been created |
| 209 |
ansible.builtin.assert: |
| 210 |
that: |
| 211 |
- result_no_device_true.changed |
| 212 |
- "'/dev/sdf' not in result_no_device_true.block_device_mapping" |
| 213 |
|
| 214 |
- name: Create an image from the instance with attached devices with no_device false |
| 215 |
amazon.aws.ec2_ami: |
| 216 |
name: "{{ ec2_ami_name }}_no_device_false_ami" |
| 217 |
instance_id: "{{ ec2_instance_id }}" |
| 218 |
device_mapping: |
| 219 |
- device_name: /dev/sda1 |
| 220 |
volume_size: 10 |
| 221 |
delete_on_termination: true |
| 222 |
volume_type: gp2 |
| 223 |
no_device: false |
| 224 |
state: present |
| 225 |
wait: true |
| 226 |
root_device_name: "{{ ec2_ami_root_disk }}" |
| 227 |
register: result_no_device_false |
| 228 |
|
| 229 |
- name: Set image id fact for deletion later |
| 230 |
ansible.builtin.set_fact: |
| 231 |
ec2_ami_no_device_false_image_id: "{{ result_no_device_false.image_id }}" |
| 232 |
|
| 233 |
- name: Assert that image with no_device option no has been created |
| 234 |
ansible.builtin.assert: |
| 235 |
that: |
| 236 |
- result_no_device_false.changed |
| 237 |
- "'/dev/sda1' in result_no_device_false.block_device_mapping" |
| 238 |
|
| 239 |
# ============================================================ |
| 240 |
|
| 241 |
- name: Gather facts about the image created |
| 242 |
amazon.aws.ec2_ami_info: |
| 243 |
image_ids: "{{ ec2_ami_image_id }}" |
| 244 |
register: ami_facts_result |
| 245 |
ignore_errors: true |
| 246 |
|
| 247 |
- name: Assert that the right image was found |
| 248 |
ansible.builtin.assert: |
| 249 |
that: |
| 250 |
- ami_facts_result.images[0].image_id == ec2_ami_image_id |
| 251 |
|
| 252 |
# some ec2_ami_info tests to test if the filtering is working fine. |
| 253 |
# ============================================================ |
| 254 |
|
| 255 |
- name: Gather info about the image |
| 256 |
amazon.aws.ec2_ami_info: |
| 257 |
image_ids: "{{ ec2_region_images[ec2_region] }}" |
| 258 |
register: ami_info_result |
| 259 |
ignore_errors: true |
| 260 |
|
| 261 |
- name: Assert that the right image was found |
| 262 |
ansible.builtin.assert: |
| 263 |
that: |
| 264 |
- ami_info_result.images[0].image_id == ec2_region_images[ec2_region] |
| 265 |
|
| 266 |
# ============================================================ |
| 267 |
|
| 268 |
- name: Gather info about the image using boolean filter |
| 269 |
amazon.aws.ec2_ami_info: |
| 270 |
image_ids: "{{ ec2_region_images[ec2_region] }}" |
| 271 |
filters: |
| 272 |
is-public: true |
| 273 |
register: ami_info_result |
| 274 |
ignore_errors: true |
| 275 |
|
| 276 |
- name: Assert that the right image was found |
| 277 |
ansible.builtin.assert: |
| 278 |
that: |
| 279 |
- ami_info_result.images[0].image_id == ec2_region_images[ec2_region] |
| 280 |
|
| 281 |
# ============================================================ |
| 282 |
|
| 283 |
- name: Gather info about the image using integer filter |
| 284 |
amazon.aws.ec2_ami_info: |
| 285 |
image_ids: "{{ ec2_region_images[ec2_region] }}" |
| 286 |
filters: |
| 287 |
# Amazon owned |
| 288 |
owner-id: 137112412989 |
| 289 |
register: ami_info_result |
| 290 |
ignore_errors: true |
| 291 |
|
| 292 |
- name: Assert that the right image was found |
| 293 |
ansible.builtin.assert: |
| 294 |
that: |
| 295 |
- ami_info_result.images[0].image_id == ec2_region_images[ec2_region] |
| 296 |
|
| 297 |
# ============================================================ |
| 298 |
|
| 299 |
- name: Gather info about the image using string filter |
| 300 |
amazon.aws.ec2_ami_info: |
| 301 |
image_ids: "{{ ec2_region_images[ec2_region] }}" |
| 302 |
filters: |
| 303 |
name: amzn-ami-hvm-2017.09.0.20170930-x86_64-gp2 |
| 304 |
register: ami_info_result |
| 305 |
ignore_errors: true |
| 306 |
|
| 307 |
- name: Assert that the right image was found |
| 308 |
ansible.builtin.assert: |
| 309 |
that: |
| 310 |
- ami_info_result.images[0].image_id == ec2_region_images[ec2_region] |
| 311 |
|
| 312 |
# e2_ami_info filtering tests ends |
| 313 |
# ============================================================ |
| 314 |
|
| 315 |
- name: Delete the image (check mode) |
| 316 |
amazon.aws.ec2_ami: |
| 317 |
instance_id: "{{ ec2_instance_id }}" |
| 318 |
state: absent |
| 319 |
delete_snapshot: true |
| 320 |
name: "{{ ec2_ami_name }}_ami" |
| 321 |
description: "{{ ec2_ami_description }}" |
| 322 |
image_id: "{{ result.image_id }}" |
| 323 |
tags: |
| 324 |
Name: "{{ ec2_ami_name }}_ami" |
| 325 |
wait: true |
| 326 |
ignore_errors: true |
| 327 |
check_mode: true |
| 328 |
register: check_mode_result |
| 329 |
|
| 330 |
- name: Assert that check_mode result is changed |
| 331 |
ansible.builtin.assert: |
| 332 |
that: |
| 333 |
- check_mode_result is changed |
| 334 |
|
| 335 |
- name: Delete the image |
| 336 |
amazon.aws.ec2_ami: |
| 337 |
instance_id: "{{ ec2_instance_id }}" |
| 338 |
state: absent |
| 339 |
delete_snapshot: true |
| 340 |
name: "{{ ec2_ami_name }}_ami" |
| 341 |
description: "{{ ec2_ami_description }}" |
| 342 |
image_id: "{{ result.image_id }}" |
| 343 |
tags: |
| 344 |
Name: "{{ ec2_ami_name }}_ami" |
| 345 |
wait: true |
| 346 |
ignore_errors: true |
| 347 |
register: result |
| 348 |
|
| 349 |
- name: Assert that the image has been deleted |
| 350 |
ansible.builtin.assert: |
| 351 |
that: |
| 352 |
- result.changed |
| 353 |
- "'image_id' not in result" |
| 354 |
- result.snapshots_deleted |
| 355 |
|
| 356 |
# ============================================================== |
| 357 |
|
| 358 |
- name: Test removing an ami if no image ID is provided (expected failed=true) |
| 359 |
amazon.aws.ec2_ami: |
| 360 |
state: absent |
| 361 |
register: result |
| 362 |
ignore_errors: true |
| 363 |
|
| 364 |
- name: Assert that an image ID is required |
| 365 |
ansible.builtin.assert: |
| 366 |
that: |
| 367 |
- result.failed |
| 368 |
- "result.msg == 'state is absent but all of the following are missing: image_id'" |
| 369 |
|
| 370 |
# ============================================================ |
| 371 |
|
| 372 |
- name: Create an image from the snapshot |
| 373 |
amazon.aws.ec2_ami: |
| 374 |
name: "{{ ec2_ami_name }}_ami" |
| 375 |
description: "{{ ec2_ami_description }}" |
| 376 |
state: present |
| 377 |
launch_permissions: |
| 378 |
user_ids: [] |
| 379 |
tags: |
| 380 |
Name: "{{ ec2_ami_name }}_ami" |
| 381 |
root_device_name: "{{ ec2_ami_root_disk }}" |
| 382 |
device_mapping: |
| 383 |
- device_name: "{{ ec2_ami_root_disk }}" |
| 384 |
volume_type: gp2 |
| 385 |
size: 8 |
| 386 |
delete_on_termination: true |
| 387 |
snapshot_id: "{{ setup_snapshot.snapshot_id }}" |
| 388 |
register: result |
| 389 |
ignore_errors: true |
| 390 |
|
| 391 |
- name: Set image id fact for deletion later |
| 392 |
ansible.builtin.set_fact: |
| 393 |
ec2_ami_image_id: "{{ result.image_id }}" |
| 394 |
ec2_ami_snapshot: "{{ result.block_device_mapping[ec2_ami_root_disk].snapshot_id }}" |
| 395 |
|
| 396 |
- name: Assert a new ami has been created |
| 397 |
ansible.builtin.assert: |
| 398 |
that: |
| 399 |
- result.changed |
| 400 |
- result.image_id.startswith('ami-') |
| 401 |
|
| 402 |
# ============================================================ |
| 403 |
|
| 404 |
- name: Test default launch permissions idempotence (check mode) |
| 405 |
amazon.aws.ec2_ami: |
| 406 |
description: "{{ ec2_ami_description }}" |
| 407 |
state: present |
| 408 |
name: "{{ ec2_ami_name }}_ami" |
| 409 |
tags: |
| 410 |
Name: "{{ ec2_ami_name }}_ami" |
| 411 |
root_device_name: "{{ ec2_ami_root_disk }}" |
| 412 |
image_id: "{{ result.image_id }}" |
| 413 |
launch_permissions: |
| 414 |
user_ids: [] |
| 415 |
device_mapping: |
| 416 |
- device_name: "{{ ec2_ami_root_disk }}" |
| 417 |
volume_type: gp2 |
| 418 |
size: 8 |
| 419 |
delete_on_termination: true |
| 420 |
snapshot_id: "{{ setup_snapshot.snapshot_id }}" |
| 421 |
check_mode: true |
| 422 |
register: check_mode_result |
| 423 |
|
| 424 |
- name: Assert that check_mode result is not changed |
| 425 |
ansible.builtin.assert: |
| 426 |
that: |
| 427 |
- check_mode_result is not changed |
| 428 |
|
| 429 |
- name: Test default launch permissions idempotence |
| 430 |
amazon.aws.ec2_ami: |
| 431 |
description: "{{ ec2_ami_description }}" |
| 432 |
state: present |
| 433 |
name: "{{ ec2_ami_name }}_ami" |
| 434 |
tags: |
| 435 |
Name: "{{ ec2_ami_name }}_ami" |
| 436 |
root_device_name: "{{ ec2_ami_root_disk }}" |
| 437 |
image_id: "{{ result.image_id }}" |
| 438 |
launch_permissions: |
| 439 |
user_ids: [] |
| 440 |
device_mapping: |
| 441 |
- device_name: "{{ ec2_ami_root_disk }}" |
| 442 |
volume_type: gp2 |
| 443 |
size: 8 |
| 444 |
delete_on_termination: true |
| 445 |
snapshot_id: "{{ setup_snapshot.snapshot_id }}" |
| 446 |
register: result |
| 447 |
|
| 448 |
- name: Assert a new ami has not been created |
| 449 |
ansible.builtin.assert: |
| 450 |
that: |
| 451 |
- not result.changed |
| 452 |
- result.image_id.startswith('ami-') |
| 453 |
|
| 454 |
# ============================================================ |
| 455 |
|
| 456 |
- name: Add a tag to the AMI |
| 457 |
amazon.aws.ec2_ami: |
| 458 |
state: present |
| 459 |
description: "{{ ec2_ami_description }}" |
| 460 |
image_id: "{{ result.image_id }}" |
| 461 |
name: "{{ ec2_ami_name }}_ami" |
| 462 |
tags: |
| 463 |
New: Tag |
| 464 |
purge_tags: false |
| 465 |
register: result |
| 466 |
|
| 467 |
- name: Assert a tag was added |
| 468 |
ansible.builtin.assert: |
| 469 |
that: |
| 470 |
- "'Name' in result.tags and result.tags.Name == ec2_ami_name + '_ami'" |
| 471 |
- "'New' in result.tags and result.tags.New == 'Tag'" |
| 472 |
|
| 473 |
- name: Use purge_tags to remove a tag from the AMI |
| 474 |
amazon.aws.ec2_ami: |
| 475 |
state: present |
| 476 |
description: "{{ ec2_ami_description }}" |
| 477 |
image_id: "{{ result.image_id }}" |
| 478 |
name: "{{ ec2_ami_name }}_ami" |
| 479 |
tags: |
| 480 |
New: Tag |
| 481 |
register: result |
| 482 |
|
| 483 |
- name: Assert a tag was removed |
| 484 |
ansible.builtin.assert: |
| 485 |
that: |
| 486 |
- "'Name' not in result.tags" |
| 487 |
- "'New' in result.tags and result.tags.New == 'Tag'" |
| 488 |
|
| 489 |
# ============================================================ |
| 490 |
|
| 491 |
- name: Update AMI launch permissions (check mode) |
| 492 |
amazon.aws.ec2_ami: |
| 493 |
state: present |
| 494 |
image_id: "{{ result.image_id }}" |
| 495 |
description: "{{ ec2_ami_description }}" |
| 496 |
tags: |
| 497 |
Name: "{{ ec2_ami_name }}_ami" |
| 498 |
launch_permissions: |
| 499 |
group_names: [all] |
| 500 |
check_mode: true |
| 501 |
register: check_mode_result |
| 502 |
|
| 503 |
- name: Assert that check_mode result is changed |
| 504 |
ansible.builtin.assert: |
| 505 |
that: |
| 506 |
- check_mode_result is changed |
| 507 |
|
| 508 |
- name: Update AMI launch permissions |
| 509 |
amazon.aws.ec2_ami: |
| 510 |
state: present |
| 511 |
image_id: "{{ result.image_id }}" |
| 512 |
description: "{{ ec2_ami_description }}" |
| 513 |
tags: |
| 514 |
Name: "{{ ec2_ami_name }}_ami" |
| 515 |
launch_permissions: |
| 516 |
group_names: [all] |
| 517 |
register: result |
| 518 |
|
| 519 |
- name: Assert launch permissions were updated |
| 520 |
ansible.builtin.assert: |
| 521 |
that: |
| 522 |
- result.changed |
| 523 |
|
| 524 |
# ============================================================ |
| 525 |
|
| 526 |
- name: Modify the AMI description (check mode) |
| 527 |
amazon.aws.ec2_ami: |
| 528 |
state: present |
| 529 |
image_id: "{{ result.image_id }}" |
| 530 |
name: "{{ ec2_ami_name }}_ami" |
| 531 |
description: "{{ ec2_ami_description }}CHANGED" |
| 532 |
tags: |
| 533 |
Name: "{{ ec2_ami_name }}_ami" |
| 534 |
launch_permissions: |
| 535 |
group_names: [all] |
| 536 |
check_mode: true |
| 537 |
register: check_mode_result |
| 538 |
|
| 539 |
- name: Assert that check_mode result is changed |
| 540 |
ansible.builtin.assert: |
| 541 |
that: |
| 542 |
- check_mode_result is changed |
| 543 |
|
| 544 |
- name: Modify the AMI description |
| 545 |
amazon.aws.ec2_ami: |
| 546 |
state: present |
| 547 |
image_id: "{{ result.image_id }}" |
| 548 |
name: "{{ ec2_ami_name }}_ami" |
| 549 |
description: "{{ ec2_ami_description }}CHANGED" |
| 550 |
tags: |
| 551 |
Name: "{{ ec2_ami_name }}_ami" |
| 552 |
launch_permissions: |
| 553 |
group_names: [all] |
| 554 |
register: result |
| 555 |
|
| 556 |
- name: Assert the description changed |
| 557 |
ansible.builtin.assert: |
| 558 |
that: |
| 559 |
- result.changed |
| 560 |
|
| 561 |
# ============================================================ |
| 562 |
|
| 563 |
- name: Remove public launch permissions |
| 564 |
amazon.aws.ec2_ami: |
| 565 |
state: present |
| 566 |
image_id: "{{ result.image_id }}" |
| 567 |
name: "{{ ec2_ami_name }}_ami" |
| 568 |
tags: |
| 569 |
Name: "{{ ec2_ami_name }}_ami" |
| 570 |
launch_permissions: |
| 571 |
group_names: [] |
| 572 |
register: result |
| 573 |
|
| 574 |
- name: Assert launch permissions were updated |
| 575 |
ansible.builtin.assert: |
| 576 |
that: |
| 577 |
- result.changed |
| 578 |
|
| 579 |
# ============================================================ |
| 580 |
|
| 581 |
- name: Delete ami without deleting the snapshot (default is not to delete) |
| 582 |
amazon.aws.ec2_ami: |
| 583 |
instance_id: "{{ ec2_instance_id }}" |
| 584 |
state: absent |
| 585 |
name: "{{ ec2_ami_name }}_ami" |
| 586 |
image_id: "{{ ec2_ami_image_id }}" |
| 587 |
tags: |
| 588 |
Name: "{{ ec2_ami_name }}_ami" |
| 589 |
wait: true |
| 590 |
ignore_errors: true |
| 591 |
register: result |
| 592 |
|
| 593 |
- name: Assert that the image has been deleted |
| 594 |
ansible.builtin.assert: |
| 595 |
that: |
| 596 |
- result.changed |
| 597 |
- "'image_id' not in result" |
| 598 |
|
| 599 |
- name: Ensure the snapshot still exists |
| 600 |
amazon.aws.ec2_snapshot_info: |
| 601 |
snapshot_ids: |
| 602 |
- "{{ ec2_ami_snapshot }}" |
| 603 |
register: snapshot_result |
| 604 |
|
| 605 |
- name: Assert the snapshot wasn't deleted |
| 606 |
ansible.builtin.assert: |
| 607 |
that: |
| 608 |
- snapshot_result.snapshots[0].snapshot_id == ec2_ami_snapshot |
| 609 |
|
| 610 |
- name: Delete ami for a second time (check mode) |
| 611 |
amazon.aws.ec2_ami: |
| 612 |
instance_id: "{{ ec2_instance_id }}" |
| 613 |
state: absent |
| 614 |
name: "{{ ec2_ami_name }}_ami" |
| 615 |
image_id: "{{ ec2_ami_image_id }}" |
| 616 |
tags: |
| 617 |
Name: "{{ ec2_ami_name }}_ami" |
| 618 |
wait: true |
| 619 |
check_mode: true |
| 620 |
register: check_mode_result |
| 621 |
|
| 622 |
- name: Assert that check_mode result is not changed |
| 623 |
ansible.builtin.assert: |
| 624 |
that: |
| 625 |
- check_mode_result is not changed |
| 626 |
|
| 627 |
- name: Delete ami for a second time |
| 628 |
amazon.aws.ec2_ami: |
| 629 |
instance_id: "{{ ec2_instance_id }}" |
| 630 |
state: absent |
| 631 |
name: "{{ ec2_ami_name }}_ami" |
| 632 |
image_id: "{{ ec2_ami_image_id }}" |
| 633 |
tags: |
| 634 |
Name: "{{ ec2_ami_name }}_ami" |
| 635 |
wait: true |
| 636 |
register: result |
| 637 |
|
| 638 |
- name: Assert that image does not exist |
| 639 |
ansible.builtin.assert: |
| 640 |
that: |
| 641 |
- not result.changed |
| 642 |
- not result.failed |
| 643 |
|
| 644 |
# ============================================================ |
| 645 |
|
| 646 |
- name: Create an image from the snapshot with boot_mode and tpm_support |
| 647 |
amazon.aws.ec2_ami: |
| 648 |
name: "{{ ec2_ami_name }}_ami-boot-tpm" |
| 649 |
description: "{{ ec2_ami_description }}" |
| 650 |
state: present |
| 651 |
boot_mode: uefi |
| 652 |
tpm_support: v2.0 |
| 653 |
launch_permissions: |
| 654 |
user_ids: [] |
| 655 |
tags: |
| 656 |
Name: "{{ ec2_ami_name }}_ami-boot-tpm" |
| 657 |
root_device_name: "{{ ec2_ami_root_disk }}" |
| 658 |
device_mapping: |
| 659 |
- device_name: "{{ ec2_ami_root_disk }}" |
| 660 |
volume_type: gp2 |
| 661 |
size: 8 |
| 662 |
delete_on_termination: true |
| 663 |
snapshot_id: "{{ setup_snapshot.snapshot_id }}" |
| 664 |
register: result |
| 665 |
ignore_errors: true |
| 666 |
|
| 667 |
- name: Set image id fact for deletion later |
| 668 |
ansible.builtin.set_fact: |
| 669 |
ec2_ami_image_id_boot_tpm: "{{ result.image_id }}" |
| 670 |
ec2_ami_snapshot_boot_tpm: "{{ result.block_device_mapping[ec2_ami_root_disk].snapshot_id }}" |
| 671 |
|
| 672 |
- name: Gather facts about the image created |
| 673 |
amazon.aws.ec2_ami_info: |
| 674 |
image_ids: "{{ ec2_ami_image_id_boot_tpm }}" |
| 675 |
register: ami_facts_result_boot_tpm |
| 676 |
ignore_errors: true |
| 677 |
|
| 678 |
- name: Assert that new ami has been created with desired options |
| 679 |
ansible.builtin.assert: |
| 680 |
that: |
| 681 |
- result.changed |
| 682 |
- result.image_id.startswith('ami-') |
| 683 |
- ami_facts_result_boot_tpm.images[0].image_id | length != 0 |
| 684 |
- ami_facts_result_boot_tpm.images[0].boot_mode == 'uefi' |
| 685 |
- ami_facts_result_boot_tpm.images[0].tpm_support == 'v2.0' |
| 686 |
|
| 687 |
# === Test modify launch permissions org_arns and org_unit_arns========================= |
| 688 |
|
| 689 |
- name: Create an image from the instance |
| 690 |
amazon.aws.ec2_ami: |
| 691 |
instance_id: "{{ ec2_instance_id }}" |
| 692 |
state: present |
| 693 |
name: "{{ ec2_ami_name }}_permissions" |
| 694 |
description: "{{ ec2_ami_description }}" |
| 695 |
tags: |
| 696 |
Name: "{{ ec2_ami_name }}_permissions" |
| 697 |
wait: true |
| 698 |
root_device_name: "{{ ec2_ami_root_disk }}" |
| 699 |
register: permissions_create_result |
| 700 |
|
| 701 |
- name: Modify the AMI launch permissions |
| 702 |
amazon.aws.ec2_ami: |
| 703 |
state: present |
| 704 |
image_id: "{{ permissions_create_result.image_id }}" |
| 705 |
name: "{{ ec2_ami_name }}_permissions" |
| 706 |
tags: |
| 707 |
Name: "{{ ec2_ami_name }}_permissions" |
| 708 |
launch_permissions: |
| 709 |
org_arns: ["arn:aws:organizations::123456789012:organization/o-123ab4cdef"] |
| 710 |
org_unit_arns: ["arn:aws:organizations::123456789012:ou/o-123example/ou-1234-5exampld"] |
| 711 |
register: permissions_update_result |
| 712 |
|
| 713 |
- name: Get ami info |
| 714 |
amazon.aws.ec2_ami_info: |
| 715 |
image_ids: "{{ permissions_create_result.image_id }}" |
| 716 |
describe_image_attributes: true |
| 717 |
register: permissions_info_result |
| 718 |
|
| 719 |
- name: Assert that launch permissions have changed |
| 720 |
ansible.builtin.assert: |
| 721 |
that: |
| 722 |
- permissions_update_result.changed |
| 723 |
- "'organization_arn' in permissions_info_result.images[0].launch_permissions[0]" |
| 724 |
- permissions_info_result.images[0].launch_permissions[0]['organization_arn'] == 'arn:aws:organizations::123456789012:organization/o-123ab4cdef' |
| 725 |
- "'organizational_unit_arn' in permissions_info_result.images[0].launch_permissions[1]" |
| 726 |
- permissions_info_result.images[0].launch_permissions[1]['organizational_unit_arn'] == 'arn:aws:organizations::123456789012:ou/o-123example/ou-1234-5exampld' |
| 727 |
|
| 728 |
# ============================================================ |
| 729 |
|
| 730 |
always: |
| 731 |
# ============================================================ |
| 732 |
|
| 733 |
# TEAR DOWN: snapshot, ec2 instance, ec2 key pair, security group, vpc |
| 734 |
- name: Announce teardown start |
| 735 |
ansible.builtin.debug: |
| 736 |
msg: "***** TESTING COMPLETE. COMMENCE TEARDOWN *****" |
| 737 |
|
| 738 |
- name: Delete ami |
| 739 |
amazon.aws.ec2_ami: |
| 740 |
state: absent |
| 741 |
image_id: "{{ ec2_ami_image_id_boot_tpm }}" |
| 742 |
wait: true |
| 743 |
ignore_errors: true |
| 744 |
|
| 745 |
- name: Delete ami |
| 746 |
amazon.aws.ec2_ami: |
| 747 |
state: absent |
| 748 |
image_id: "{{ ec2_ami_image_id }}" |
| 749 |
name: "{{ ec2_ami_name }}_ami" |
| 750 |
wait: true |
| 751 |
ignore_errors: true |
| 752 |
|
| 753 |
- name: Delete ami |
| 754 |
amazon.aws.ec2_ami: |
| 755 |
state: absent |
| 756 |
image_id: "{{ ec2_ami_no_device_true_image_id }}" |
| 757 |
wait: true |
| 758 |
ignore_errors: true |
| 759 |
|
| 760 |
- name: Delete ami |
| 761 |
amazon.aws.ec2_ami: |
| 762 |
state: absent |
| 763 |
image_id: "{{ ec2_ami_no_device_false_image_id }}" |
| 764 |
wait: true |
| 765 |
ignore_errors: true |
| 766 |
|
| 767 |
- name: Delete ami |
| 768 |
amazon.aws.ec2_ami: |
| 769 |
state: absent |
| 770 |
image_id: "{{ ec2_ami_image_id }}" |
| 771 |
name: "{{ ec2_ami_name }}_permissions" |
| 772 |
wait: true |
| 773 |
ignore_errors: true |
| 774 |
|
| 775 |
- name: Remove setup snapshot of ec2 instance |
| 776 |
amazon.aws.ec2_snapshot: |
| 777 |
state: absent |
| 778 |
snapshot_id: "{{ setup_snapshot.snapshot_id }}" |
| 779 |
ignore_errors: true |
| 780 |
|
| 781 |
- name: Remove setup ec2 instance |
| 782 |
amazon.aws.ec2_instance: |
| 783 |
state: absent |
| 784 |
instance_ids: |
| 785 |
- "{{ ec2_instance_id }}" |
| 786 |
wait: true |
| 787 |
ignore_errors: true |
| 788 |
|
| 789 |
- name: Remove setup keypair |
| 790 |
amazon.aws.ec2_key: |
| 791 |
name: "{{ ec2_ami_name }}_setup" |
| 792 |
state: absent |
| 793 |
ignore_errors: true |
| 794 |
|
| 795 |
- name: Remove setup security group |
| 796 |
amazon.aws.ec2_security_group: |
| 797 |
name: "{{ ec2_ami_name }}_setup" |
| 798 |
description: created by Ansible integration tests |
| 799 |
state: absent |
| 800 |
vpc_id: "{{ setup_vpc.vpc.id }}" |
| 801 |
ignore_errors: true |
| 802 |
|
| 803 |
- name: Remove setup subnet |
| 804 |
amazon.aws.ec2_vpc_subnet: |
| 805 |
az: "{{ availability_zone }}" |
| 806 |
tags: "{{ ec2_ami_name }}_setup" |
| 807 |
vpc_id: "{{ setup_vpc.vpc.id }}" |
| 808 |
cidr: "{{ subnet_cidr }}" |
| 809 |
state: absent |
| 810 |
resource_tags: |
| 811 |
Name: "{{ ec2_ami_name }}_setup" |
| 812 |
ignore_errors: true |
| 813 |
|
| 814 |
- name: Remove setup VPC |
| 815 |
amazon.aws.ec2_vpc_net: |
| 816 |
cidr_block: "{{ vpc_cidr }}" |
| 817 |
state: absent |
| 818 |
name: "{{ ec2_ami_name }}_setup" |
| 819 |
resource_tags: |
| 820 |
Name: "{{ ec2_ami_name }}_setup" |
| 821 |
ignore_errors: true |