forked from ansible-collections/community.aws
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ec2_spot_instance: add parameter to enable terminating spot instances…
… when cancelling request (ansible-collections#1402) ec2_spot_instance: add parameter to enable terminating spot instances when cancelling request SUMMARY This PR adds a new parameter terminate_instances (true | false) to enable terminating spot instances when cancelling request. Can be used only when state=absent. Fixes ansible-collections#1360 ISSUE TYPE Feature Pull Request COMPONENT NAME ec2_spot_instance ADDITIONAL INFORMATION Reviewed-by: Bikouo Aubin Reviewed-by: Mandar Kulkarni <[email protected]> Reviewed-by: Alina Buzachis Reviewed-by: Mark Chappell
- Loading branch information
Showing
4 changed files
with
141 additions
and
4 deletions.
There are no files selected for viewing
2 changes: 2 additions & 0 deletions
2
changelogs/fragments/1402-ec2_spot_instance-ability-to-terminate-instances.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
minor_changes: | ||
- ec2_spot_instance - add parameter ``terminate_instances`` to support terminate instances associated with spot requests. (https://github.com/ansible-collections/amazon.aws/pull/1402). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
tests/integration/targets/ec2_spot_instance/tasks/terminate_associated_instances.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
--- | ||
- block: | ||
|
||
# Spot instance request creation | ||
- name: Simple Spot Request Creation | ||
amazon.aws.ec2_spot_instance: | ||
launch_specification: | ||
image_id: "{{ ec2_ami_id }}" | ||
key_name: "{{ resource_prefix }}-keypair" | ||
instance_type: "t2.micro" | ||
subnet_id: "{{ vpc_subnet_result.subnet.id }}" | ||
tags: | ||
ansible-test: "{{ resource_prefix }}" | ||
register: create_result | ||
|
||
# Get instance ID of associated spot instance request | ||
- name: Get info about the spot instance request created | ||
amazon.aws.ec2_spot_instance_info: | ||
spot_instance_request_ids: | ||
- "{{ create_result.spot_request.spot_instance_request_id }}" | ||
register: spot_instance_info_result | ||
retries: 5 | ||
until: spot_instance_info_result.spot_request[0].instance_id is defined | ||
|
||
- name: Pause to allow instance launch | ||
pause: | ||
seconds: 60 | ||
|
||
- name: Get instance ID of the instance associated with above spot instance request | ||
set_fact: | ||
instance_id_1: "{{ spot_instance_info_result.spot_request[0].instance_id }}" | ||
|
||
- name: Check state of instance - BEFORE request cancellation | ||
amazon.aws.ec2_instance_info: | ||
instance_ids: ["{{ instance_id_1 }}"] | ||
register: instance_info_result | ||
|
||
# Cancel spot instance request | ||
- name: Spot Request Termination | ||
amazon.aws.ec2_spot_instance: | ||
spot_instance_request_ids: | ||
- '{{ create_result.spot_request.spot_instance_request_id }}' | ||
state: absent | ||
|
||
# Verify that instance is not terminated and still running | ||
- name: Check state of instance - AFTER request cancellation | ||
amazon.aws.ec2_instance_info: | ||
instance_ids: ["{{ instance_id_1 }}"] | ||
register: instance_info_result | ||
|
||
- assert: | ||
that: instance_info_result.instances[0].state.name == 'running' | ||
|
||
#========================================================================== | ||
|
||
# Spot instance request creation | ||
- name: Simple Spot Request Creation | ||
amazon.aws.ec2_spot_instance: | ||
launch_specification: | ||
image_id: "{{ ec2_ami_id }}" | ||
key_name: "{{ resource_prefix }}-keypair" | ||
instance_type: "t2.micro" | ||
subnet_id: "{{ vpc_subnet_result.subnet.id }}" | ||
tags: | ||
ansible-test: "{{ resource_prefix }}" | ||
register: create_result | ||
|
||
# Get instance ID of associated spot instance request | ||
- name: Get info about the spot instance request created | ||
amazon.aws.ec2_spot_instance_info: | ||
spot_instance_request_ids: | ||
- "{{ create_result.spot_request.spot_instance_request_id }}" | ||
register: spot_instance_info_result | ||
retries: 5 | ||
until: spot_instance_info_result.spot_request[0].instance_id is defined | ||
|
||
- name: Pause to allow instance launch | ||
pause: | ||
seconds: 60 | ||
|
||
- name: Get instance ID of the instance associated with above spot instance request | ||
set_fact: | ||
instance_id_2: "{{ spot_instance_info_result.spot_request[0].instance_id }}" | ||
|
||
- name: Check state of instance - BEFORE request cancellation | ||
amazon.aws.ec2_instance_info: | ||
instance_ids: ["{{ instance_id_2 }}"] | ||
register: instance_info_result | ||
|
||
# Cancel spot instance request | ||
- name: Spot Request Termination | ||
amazon.aws.ec2_spot_instance: | ||
spot_instance_request_ids: | ||
- '{{ create_result.spot_request.spot_instance_request_id }}' | ||
state: absent | ||
terminate_instances: true | ||
|
||
- name: wait for instance to terminate | ||
pause: | ||
seconds: 60 | ||
|
||
# Verify that instance is terminated or shutting-down | ||
- name: Check state of instance - AFTER request cancellation | ||
amazon.aws.ec2_instance_info: | ||
instance_ids: ["{{ instance_id_2 }}"] | ||
register: instance_info_result | ||
|
||
- assert: | ||
that: instance_info_result.instances[0].state.name in ['terminated', 'shutting-down'] |