2019-06-28 12:28:47 +02:00
|
|
|
- block:
|
|
|
|
- name: Make termination-protected instance in the testing subnet created in the test VPC
|
|
|
|
ec2_instance:
|
|
|
|
name: "{{ resource_prefix }}-test-protected-instance-in-vpc"
|
2019-10-21 14:55:44 +02:00
|
|
|
image_id: "{{ ec2_ami_image }}"
|
2019-06-28 12:28:47 +02:00
|
|
|
tags:
|
|
|
|
TestId: "{{ resource_prefix }}"
|
|
|
|
security_groups: "{{ sg.group_id }}"
|
|
|
|
vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}"
|
|
|
|
termination_protection: true
|
2019-10-21 14:55:44 +02:00
|
|
|
instance_type: "{{ ec2_instance_type }}"
|
2019-06-28 12:28:47 +02:00
|
|
|
state: running
|
2019-07-02 15:17:18 +02:00
|
|
|
wait: yes
|
2019-06-28 12:28:47 +02:00
|
|
|
register: in_test_vpc
|
2018-10-11 20:21:01 +02:00
|
|
|
|
2019-06-28 12:28:47 +02:00
|
|
|
- name: Make termination-protected instance in the testing subnet created in the test VPC(check mode)
|
|
|
|
ec2_instance:
|
|
|
|
name: "{{ resource_prefix }}-test-protected-instance-in-vpc-checkmode"
|
2019-10-21 14:55:44 +02:00
|
|
|
image_id: "{{ ec2_ami_image }}"
|
2019-06-28 12:28:47 +02:00
|
|
|
tags:
|
|
|
|
TestId: "{{ resource_prefix }}"
|
|
|
|
security_groups: "{{ sg.group_id }}"
|
|
|
|
vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}"
|
|
|
|
termination_protection: true
|
2019-10-21 14:55:44 +02:00
|
|
|
instance_type: "{{ ec2_instance_type }}"
|
2019-06-28 12:28:47 +02:00
|
|
|
check_mode: yes
|
2018-10-11 20:21:01 +02:00
|
|
|
|
2019-06-28 12:28:47 +02:00
|
|
|
- name: "fact presented ec2 instance"
|
|
|
|
ec2_instance_info:
|
|
|
|
filters:
|
|
|
|
"tag:Name": "{{ resource_prefix }}-test-protected-instance-in-vpc"
|
2019-07-02 15:17:18 +02:00
|
|
|
"instance-state-name": "running"
|
2019-06-28 12:28:47 +02:00
|
|
|
register: presented_instance_fact
|
2018-10-11 20:21:01 +02:00
|
|
|
|
2019-06-28 12:28:47 +02:00
|
|
|
- name: "fact checkmode ec2 instance"
|
|
|
|
ec2_instance_info:
|
|
|
|
filters:
|
|
|
|
"tag:Name": "{{ resource_prefix }}-test-protected-instance-in-vpc-checkmode"
|
|
|
|
register: checkmode_instance_fact
|
2018-10-11 20:21:01 +02:00
|
|
|
|
2019-06-28 12:28:47 +02:00
|
|
|
- name: "Confirm whether the check mode is working normally."
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- "{{ presented_instance_fact.instances | length }} > 0"
|
|
|
|
- "'{{ presented_instance_fact.instances.0.state.name }}' in ['running', 'pending']"
|
|
|
|
- "{{ checkmode_instance_fact.instances | length }} == 0"
|
|
|
|
|
|
|
|
- name: Try to terminate the instance
|
|
|
|
ec2_instance:
|
|
|
|
state: absent
|
|
|
|
name: "{{ resource_prefix }}-test-protected-instance-in-vpc"
|
2019-10-21 14:55:44 +02:00
|
|
|
image_id: "{{ ec2_ami_image }}"
|
2019-06-28 12:28:47 +02:00
|
|
|
tags:
|
|
|
|
TestId: "{{ resource_prefix }}"
|
|
|
|
security_groups: "{{ sg.group_id }}"
|
|
|
|
vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}"
|
|
|
|
termination_protection: true
|
2019-10-21 14:55:44 +02:00
|
|
|
instance_type: "{{ ec2_instance_type }}"
|
2019-06-28 12:28:47 +02:00
|
|
|
register: bad_terminate
|
|
|
|
ignore_errors: yes
|
|
|
|
- name: Cannot terminate protected instance
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- bad_terminate is failed
|
|
|
|
- name: Alter termination protection setting
|
|
|
|
ec2_instance:
|
|
|
|
name: "{{ resource_prefix }}-test-protected-instance-in-vpc"
|
2019-10-21 14:55:44 +02:00
|
|
|
image_id: "{{ ec2_ami_image }}"
|
2019-06-28 12:28:47 +02:00
|
|
|
vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}"
|
|
|
|
termination_protection: false
|
2019-10-21 14:55:44 +02:00
|
|
|
instance_type: "{{ ec2_instance_type }}"
|
2019-06-28 12:28:47 +02:00
|
|
|
- name: Try to terminate the instance again (should work)
|
|
|
|
ec2_instance:
|
|
|
|
name: "{{ resource_prefix }}-test-protected-instance-in-vpc"
|
2019-10-21 14:55:44 +02:00
|
|
|
image_id: "{{ ec2_ami_image }}"
|
2019-06-28 12:28:47 +02:00
|
|
|
vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}"
|
2019-10-21 14:55:44 +02:00
|
|
|
instance_type: "{{ ec2_instance_type }}"
|
2019-06-28 12:28:47 +02:00
|
|
|
state: absent
|
|
|
|
wait: false
|
|
|
|
register: terminate_results
|
|
|
|
- assert:
|
|
|
|
that: terminate_results is not failed
|
|
|
|
|
|
|
|
always:
|
|
|
|
- name: Terminate instance
|
|
|
|
ec2_instance:
|
|
|
|
filters:
|
|
|
|
tag:TestId: "{{ resource_prefix }}"
|
|
|
|
state: absent
|
|
|
|
wait: false
|
|
|
|
register: result
|
|
|
|
ignore_errors: yes
|