| 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 |
# ============================================================ |
| 75 |
|
| 76 |
- name: Create an image from the snapshot with boot_mode and tpm_support |
| 77 |
amazon.aws.ec2_ami: |
| 78 |
name: "{{ ec2_ami_name }}_ami-boot-tpm" |
| 79 |
description: "{{ ec2_ami_description }}" |
| 80 |
state: present |
| 81 |
boot_mode: uefi |
| 82 |
tpm_support: v2.0 |
| 83 |
launch_permissions: |
| 84 |
user_ids: [] |
| 85 |
tags: |
| 86 |
Name: "{{ ec2_ami_name }}_ami-boot-tpm" |
| 87 |
root_device_name: "{{ ec2_ami_root_disk }}" |
| 88 |
device_mapping: |
| 89 |
- device_name: "{{ ec2_ami_root_disk }}" |
| 90 |
volume_type: gp2 |
| 91 |
size: 8 |
| 92 |
delete_on_termination: true |
| 93 |
snapshot_id: "{{ setup_snapshot.snapshot_id }}" |
| 94 |
register: result |
| 95 |
ignore_errors: true |
| 96 |
|
| 97 |
- name: Set image id fact for deletion later |
| 98 |
ansible.builtin.set_fact: |
| 99 |
ec2_ami_image_id_boot_tpm: "{{ result.image_id }}" |
| 100 |
ec2_ami_snapshot_boot_tpm: "{{ result.block_device_mapping[ec2_ami_root_disk].snapshot_id }}" |
| 101 |
|
| 102 |
- name: Gather facts about the image created |
| 103 |
amazon.aws.ec2_ami_info: |
| 104 |
image_ids: "{{ ec2_ami_image_id_boot_tpm }}" |
| 105 |
register: ami_facts_result_boot_tpm |
| 106 |
ignore_errors: true |
| 107 |
|
| 108 |
- name: Assert that new ami has been created with desired options |
| 109 |
ansible.builtin.assert: |
| 110 |
that: |
| 111 |
- result.changed |
| 112 |
- result.image_id.startswith('ami-') |
| 113 |
- ami_facts_result_boot_tpm.images[0].image_id | length != 0 |
| 114 |
- ami_facts_result_boot_tpm.images[0].boot_mode == 'uefi' |
| 115 |
- ami_facts_result_boot_tpm.images[0].tpm_support == 'v2.0' |
| 116 |
|
| 117 |
# ============================================================ |
| 118 |
|
| 119 |
always: |
| 120 |
# ============================================================ |
| 121 |
|
| 122 |
# TEAR DOWN: snapshot, ec2 instance, ec2 key pair, security group, vpc |
| 123 |
- name: Announce teardown start |
| 124 |
ansible.builtin.debug: |
| 125 |
msg: "***** TESTING COMPLETE. COMMENCE TEARDOWN *****" |
| 126 |
|
| 127 |
- name: Remove setup ec2 instance |
| 128 |
amazon.aws.ec2_instance: |
| 129 |
state: absent |
| 130 |
instance_ids: |
| 131 |
- "{{ ec2_instance_id }}" |
| 132 |
wait: true |
| 133 |
ignore_errors: true |
| 134 |
|
| 135 |
- name: Delete ami |
| 136 |
amazon.aws.ec2_ami: |
| 137 |
state: absent |
| 138 |
image_id: "{{ ec2_ami_image_id_boot_tpm }}" |
| 139 |
wait: true |
| 140 |
ignore_errors: true |
| 141 |
|
| 142 |
- name: Remove setup snapshot of ec2 instance |
| 143 |
amazon.aws.ec2_snapshot: |
| 144 |
state: absent |
| 145 |
snapshot_id: "{{ setup_snapshot.snapshot_id }}" |
| 146 |
ignore_errors: true |
| 147 |
|
| 148 |
- name: Remove setup keypair |
| 149 |
amazon.aws.ec2_key: |
| 150 |
name: "{{ ec2_ami_name }}_setup" |
| 151 |
state: absent |
| 152 |
ignore_errors: true |
| 153 |
|
| 154 |
- name: Remove setup security group |
| 155 |
amazon.aws.ec2_security_group: |
| 156 |
name: "{{ ec2_ami_name }}_setup" |
| 157 |
description: created by Ansible integration tests |
| 158 |
state: absent |
| 159 |
vpc_id: "{{ setup_vpc.vpc.id }}" |
| 160 |
ignore_errors: true |
| 161 |
|
| 162 |
- name: Remove setup subnet |
| 163 |
amazon.aws.ec2_vpc_subnet: |
| 164 |
az: "{{ availability_zone }}" |
| 165 |
tags: "{{ ec2_ami_name }}_setup" |
| 166 |
vpc_id: "{{ setup_vpc.vpc.id }}" |
| 167 |
cidr: "{{ subnet_cidr }}" |
| 168 |
state: absent |
| 169 |
resource_tags: |
| 170 |
Name: "{{ ec2_ami_name }}_setup" |
| 171 |
ignore_errors: true |
| 172 |
|
| 173 |
- name: Remove setup VPC |
| 174 |
amazon.aws.ec2_vpc_net: |
| 175 |
cidr_block: "{{ vpc_cidr }}" |
| 176 |
state: absent |
| 177 |
name: "{{ ec2_ami_name }}_setup" |
| 178 |
resource_tags: |
| 179 |
Name: "{{ ec2_ami_name }}_setup" |
| 180 |
ignore_errors: true |