| 1 |
--- |
| 2 |
- module_defaults: |
| 3 |
group/aws: |
| 4 |
access_key: "{{ aws_access_key }}" |
| 5 |
secret_key: "{{ aws_secret_key }}" |
| 6 |
session_token: "{{ security_token | default(omit) }}" |
| 7 |
region: "{{ aws_region }}" |
| 8 |
|
| 9 |
block: |
| 10 |
- name: Get available AZs |
| 11 |
amazon.aws.aws_az_info: |
| 12 |
filters: |
| 13 |
region-name: "{{ aws_region }}" |
| 14 |
register: az_info |
| 15 |
|
| 16 |
- name: Pick an AZ |
| 17 |
ansible.builtin.set_fact: |
| 18 |
availability_zone: "{{ az_info['availability_zones'][0]['zone_name'] }}" |
| 19 |
|
| 20 |
# ============================================================ |
| 21 |
- name: Create a VPC |
| 22 |
amazon.aws.ec2_vpc_net: |
| 23 |
name: "{{ resource_prefix }}-vpc" |
| 24 |
state: present |
| 25 |
cidr_block: "{{ vpc_cidr }}" |
| 26 |
tags: |
| 27 |
Name: "{{ resource_prefix }}-vpc" |
| 28 |
Description: Created by ansible-test |
| 29 |
register: vpc_result |
| 30 |
|
| 31 |
- name: Create a subnet |
| 32 |
amazon.aws.ec2_vpc_subnet: |
| 33 |
cidr: "{{ vpc_cidr }}" |
| 34 |
az: "{{ availability_zone }}" |
| 35 |
vpc_id: "{{ vpc_result.vpc.id }}" |
| 36 |
tags: |
| 37 |
Name: "{{ resource_prefix }}-vpc" |
| 38 |
Description: Created by ansible-test |
| 39 |
state: present |
| 40 |
register: vpc_subnet_result |
| 41 |
|
| 42 |
- name: Create a security group |
| 43 |
amazon.aws.ec2_security_group: |
| 44 |
name: "{{ resource_prefix }}-sg" |
| 45 |
description: Created by {{ resource_prefix }} |
| 46 |
rules: [] |
| 47 |
state: present |
| 48 |
vpc_id: "{{ vpc_result.vpc.id }}" |
| 49 |
register: vpc_sg_result |
| 50 |
|
| 51 |
- name: Create a new ec2 key pair |
| 52 |
amazon.aws.ec2_key: |
| 53 |
name: "{{ resource_prefix }}-keypair" |
| 54 |
|
| 55 |
- name: Set facts to simplify use of extra resources |
| 56 |
ansible.builtin.set_fact: |
| 57 |
vpc_id: "{{ vpc_result.vpc.id }}" |
| 58 |
vpc_subnet_id: "{{ vpc_subnet_result.subnet.id }}" |
| 59 |
vpc_sg_id: "{{ vpc_sg_result.group_id }}" |
| 60 |
|
| 61 |
# ============================================================ |
| 62 |
|
| 63 |
- name: Run tests for terminating associated instances |
| 64 |
ansible.builtin.import_tasks: terminate_associated_instances.yml |
| 65 |
- name: Create simple spot instance request |
| 66 |
amazon.aws.ec2_spot_instance: |
| 67 |
launch_specification: |
| 68 |
image_id: "{{ ec2_ami_id }}" |
| 69 |
key_name: "{{ resource_prefix }}-keypair" |
| 70 |
instance_type: t2.medium |
| 71 |
subnet_id: "{{ vpc_subnet_result.subnet.id }}" |
| 72 |
tags: |
| 73 |
ansible-test: "{{ resource_prefix }}" |
| 74 |
register: create_result |
| 75 |
|
| 76 |
- name: Assert that result has changed and request has been created |
| 77 |
ansible.builtin.assert: |
| 78 |
that: |
| 79 |
- create_result is changed |
| 80 |
- create_result.spot_request is defined |
| 81 |
- create_result.spot_request.spot_instance_request_id is defined |
| 82 |
- create_result.spot_request.launch_specification.subnet_id == vpc_subnet_result.subnet.id |
| 83 |
|
| 84 |
- name: Get info about the spot instance request created |
| 85 |
amazon.aws.ec2_spot_instance_info: |
| 86 |
spot_instance_request_ids: |
| 87 |
- "{{ create_result.spot_request.spot_instance_request_id }}" |
| 88 |
register: spot_instance_info_result |
| 89 |
|
| 90 |
- name: Assert that the spot request created is open or active |
| 91 |
ansible.builtin.assert: |
| 92 |
that: |
| 93 |
- spot_instance_info_result.spot_request[0].state in ['open', 'active'] |
| 94 |
|
| 95 |
- name: Create spot request with more complex options |
| 96 |
amazon.aws.ec2_spot_instance: |
| 97 |
launch_specification: |
| 98 |
image_id: "{{ ec2_ami_id }}" |
| 99 |
key_name: "{{ resource_prefix }}-keypair" |
| 100 |
instance_type: t2.medium |
| 101 |
block_device_mappings: |
| 102 |
- device_name: /dev/sdb |
| 103 |
ebs: |
| 104 |
delete_on_termination: true |
| 105 |
volume_type: gp3 |
| 106 |
volume_size: 5 |
| 107 |
network_interfaces: |
| 108 |
- associate_public_ip_address: false |
| 109 |
subnet_id: "{{ vpc_subnet_result.subnet.id }}" |
| 110 |
delete_on_termination: true |
| 111 |
device_index: 0 |
| 112 |
placement: |
| 113 |
availability_zone: "{{ availability_zone }}" |
| 114 |
monitoring: |
| 115 |
enabled: false |
| 116 |
spot_price: 0.002 |
| 117 |
tags: |
| 118 |
camelCase: helloWorld |
| 119 |
PascalCase: HelloWorld |
| 120 |
snake_case: hello_world |
| 121 |
Title Case: Hello World |
| 122 |
lowercase spaced: hello world |
| 123 |
ansible-test: "{{ resource_prefix }}" |
| 124 |
register: complex_create_result |
| 125 |
|
| 126 |
- ansible.builtin.assert: |
| 127 |
that: |
| 128 |
- complex_create_result is changed |
| 129 |
- complex_create_result.spot_request is defined |
| 130 |
- complex_create_result.spot_request.spot_instance_request_id is defined |
| 131 |
- complex_create_result.spot_request.type == 'one-time' |
| 132 |
- '"0.002" in complex_create_result.spot_request.spot_price' ## AWS pads trailing zeros on the spot price |
| 133 |
- launch_spec.placement.availability_zone == availability_zone |
| 134 |
- launch_spec.block_device_mappings|length == 1 |
| 135 |
- launch_spec.block_device_mappings.0.ebs.delete_on_termination == true |
| 136 |
- launch_spec.block_device_mappings.0.ebs.volume_type == 'gp3' |
| 137 |
- launch_spec.block_device_mappings.0.ebs.volume_size == 5 |
| 138 |
- launch_spec.network_interfaces|length == 1 |
| 139 |
- launch_spec.network_interfaces.0.device_index == 0 |
| 140 |
- launch_spec.network_interfaces.0.associate_public_ip_address == false |
| 141 |
- launch_spec.network_interfaces.0.delete_on_termination == true |
| 142 |
- spot_request_tags|length == 6 |
| 143 |
- spot_request_tags['camelCase'] == 'helloWorld' |
| 144 |
- spot_request_tags['PascalCase'] == 'HelloWorld' |
| 145 |
- spot_request_tags['snake_case'] == 'hello_world' |
| 146 |
- spot_request_tags['Title Case'] == 'Hello World' |
| 147 |
- spot_request_tags['lowercase spaced'] == 'hello world' |
| 148 |
vars: |
| 149 |
launch_spec: "{{ complex_create_result.spot_request.launch_specification }}" |
| 150 |
spot_request_tags: "{{ complex_create_result.spot_request.tags }}" |
| 151 |
|
| 152 |
- name: Get info about the complex spot instance request created |
| 153 |
amazon.aws.ec2_spot_instance_info: |
| 154 |
spot_instance_request_ids: |
| 155 |
- "{{ complex_create_result.spot_request.spot_instance_request_id }}" |
| 156 |
register: complex_info_result |
| 157 |
|
| 158 |
- name: Assert that the complex spot request created is open/active and correct keys are set |
| 159 |
ansible.builtin.assert: |
| 160 |
that: |
| 161 |
- complex_info_result.spot_request[0].state in ['open', 'active'] |
| 162 |
- complex_create_result.spot_request.spot_price == complex_info_result.spot_request[0].spot_price |
| 163 |
- create_launch_spec.block_device_mappings[0].ebs.volume_size == info_launch_spec.block_device_mappings[0].ebs.volume_size |
| 164 |
- create_launch_spec.block_device_mappings[0].ebs.volume_type == info_launch_spec.block_device_mappings[0].ebs.volume_type |
| 165 |
- create_launch_spec.network_interfaces[0].delete_on_termination == info_launch_spec.network_interfaces[0].delete_on_termination |
| 166 |
vars: |
| 167 |
create_launch_spec: "{{ complex_create_result.spot_request.launch_specification }}" |
| 168 |
info_launch_spec: "{{ complex_info_result.spot_request[0].launch_specification }}" |
| 169 |
|
| 170 |
- name: Get info about the created spot instance requests and filter result based on provided filters |
| 171 |
amazon.aws.ec2_spot_instance_info: |
| 172 |
spot_instance_request_ids: |
| 173 |
- "{{ create_result.spot_request.spot_instance_request_id }}" |
| 174 |
- "{{ complex_create_result.spot_request.spot_instance_request_id }}" |
| 175 |
filters: |
| 176 |
tag:ansible-test: "{{ resource_prefix }}" |
| 177 |
launch.block-device-mapping.device-name: /dev/sdb |
| 178 |
register: spot_instance_info_filter_result |
| 179 |
|
| 180 |
- name: Assert that the correct spot request was returned in the filtered result |
| 181 |
ansible.builtin.assert: |
| 182 |
that: |
| 183 |
- spot_instance_info_filter_result.spot_request[0].spot_instance_request_id == complex_create_result.spot_request.spot_instance_request_id |
| 184 |
|
| 185 |
# Assert check mode |
| 186 |
- name: Create spot instance request (check_mode) |
| 187 |
amazon.aws.ec2_spot_instance: |
| 188 |
launch_specification: |
| 189 |
image_id: "{{ ec2_ami_id }}" |
| 190 |
key_name: "{{ resource_prefix }}-keypair" |
| 191 |
instance_type: t2.medium |
| 192 |
subnet_id: "{{ vpc_subnet_result.subnet.id }}" |
| 193 |
tags: |
| 194 |
ansible-test: "{{ resource_prefix }}" |
| 195 |
check_mode: true |
| 196 |
register: check_create_result |
| 197 |
|
| 198 |
- ansible.builtin.assert: |
| 199 |
that: |
| 200 |
- check_create_result is changed |
| 201 |
|
| 202 |
- name: Remove spot instance request (check_mode) |
| 203 |
amazon.aws.ec2_spot_instance: |
| 204 |
spot_instance_request_ids: "{{ create_result.spot_request.spot_instance_request_id }}" |
| 205 |
state: absent |
| 206 |
check_mode: true |
| 207 |
register: check_cancel_result |
| 208 |
|
| 209 |
- ansible.builtin.assert: |
| 210 |
that: |
| 211 |
- check_cancel_result is changed |
| 212 |
|
| 213 |
- name: Remove spot instance requests |
| 214 |
amazon.aws.ec2_spot_instance: |
| 215 |
spot_instance_request_ids: |
| 216 |
- "{{ create_result.spot_request.spot_instance_request_id }}" |
| 217 |
- "{{ complex_create_result.spot_request.spot_instance_request_id }}" |
| 218 |
state: absent |
| 219 |
register: cancel_result |
| 220 |
|
| 221 |
- ansible.builtin.assert: |
| 222 |
that: |
| 223 |
- cancel_result is changed |
| 224 |
- '"Cancelled Spot request" in cancel_result.msg' |
| 225 |
|
| 226 |
- name: Sometimes we run the next test before the EC2 API is fully updated from the previous task |
| 227 |
ansible.builtin.pause: |
| 228 |
seconds: 3 |
| 229 |
|
| 230 |
- name: Check no change if request is already cancelled (idempotency) |
| 231 |
amazon.aws.ec2_spot_instance: |
| 232 |
spot_instance_request_ids: "{{ create_result.spot_request.spot_instance_request_id }}" |
| 233 |
state: absent |
| 234 |
register: cancel_request_again |
| 235 |
|
| 236 |
- ansible.builtin.assert: |
| 237 |
that: |
| 238 |
- cancel_request_again is not changed |
| 239 |
- '"Spot request not found or already cancelled" in cancel_request_again.msg' |
| 240 |
|
| 241 |
- name: Gracefully try to remove non-existent request (NotFound) |
| 242 |
amazon.aws.ec2_spot_instance: |
| 243 |
spot_instance_request_ids: |
| 244 |
- sir-12345678 |
| 245 |
state: absent |
| 246 |
register: fake_cancel_result |
| 247 |
|
| 248 |
- ansible.builtin.assert: |
| 249 |
that: |
| 250 |
- fake_cancel_result is not changed |
| 251 |
- '"Spot request not found or already cancelled" in fake_cancel_result.msg' |
| 252 |
|
| 253 |
always: |
| 254 |
# ============================================================ |
| 255 |
- name: Delete spot instances |
| 256 |
amazon.aws.ec2_instance: |
| 257 |
state: absent |
| 258 |
filters: |
| 259 |
vpc-id: "{{ vpc_result.vpc.id }}" |
| 260 |
|
| 261 |
- name: Get all spot requests created during test |
| 262 |
amazon.aws.ec2_spot_instance_info: |
| 263 |
filters: |
| 264 |
tag:ansible-test: "{{ resource_prefix }}" |
| 265 |
register: spot_request_list |
| 266 |
|
| 267 |
- name: Remove spot instance requests |
| 268 |
amazon.aws.ec2_spot_instance: |
| 269 |
spot_instance_request_ids: |
| 270 |
- "{{ item.spot_instance_request_id }}" |
| 271 |
state: absent |
| 272 |
ignore_errors: true |
| 273 |
retries: 5 |
| 274 |
with_items: "{{ spot_request_list.spot_request }}" |
| 275 |
|
| 276 |
- name: Remove the security group |
| 277 |
amazon.aws.ec2_security_group: |
| 278 |
name: "{{ resource_prefix }}-sg" |
| 279 |
description: "{{ resource_prefix }}" |
| 280 |
rules: [] |
| 281 |
state: absent |
| 282 |
vpc_id: "{{ vpc_result.vpc.id }}" |
| 283 |
ignore_errors: true |
| 284 |
retries: 5 |
| 285 |
|
| 286 |
- name: Remove the subnet |
| 287 |
amazon.aws.ec2_vpc_subnet: |
| 288 |
cidr: "{{ vpc_cidr }}" |
| 289 |
az: "{{ availability_zone }}" |
| 290 |
vpc_id: "{{ vpc_result.vpc.id }}" |
| 291 |
state: absent |
| 292 |
ignore_errors: true |
| 293 |
retries: 5 |
| 294 |
when: vpc_subnet_result is defined |
| 295 |
|
| 296 |
- name: Remove the VPC |
| 297 |
amazon.aws.ec2_vpc_net: |
| 298 |
name: "{{ resource_prefix }}-vpc" |
| 299 |
cidr_block: "{{ vpc_cidr }}" |
| 300 |
state: absent |
| 301 |
ignore_errors: true |
| 302 |
retries: 5 |
| 303 |
|
| 304 |
- name: Remove key pair by name |
| 305 |
amazon.aws.ec2_key: |
| 306 |
name: "{{ resource_prefix }}-keypair" |
| 307 |
state: absent |
| 308 |
ignore_errors: true |