ansible/test/integration/targets/ec2_instance/tasks/termination_protection.yml
Mark Chappell 145b79ef0e ec2_instance test cleanup (#63708)
* ec2_instance/ec2_instance_info : Fixup sanity test errors

* Move ec2_instance integration tests to use aws_defaults

* Search for the AMI instead of hardcoding an AMI

* Make our VPC CIDR variable

* Remove AZ assumptions - no guarantees about specific AZs being available

* Make sure we terminate instances when we're done with them.

* Add a 10 second pause for IAM roles to become available before using them

* Wait on instance changes by default

* Switch out t2 instances for t3 they're cheaper and have more CPU available

* Pull t3.nano instance info a little earlier

* rework vpc_name and vpc_cidr a little

* Mark ec2_instance tests unsupported for now, they take too long
2019-10-21 14:55:44 +02:00

92 lines
3.3 KiB
YAML

- 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"
image_id: "{{ ec2_ami_image }}"
tags:
TestId: "{{ resource_prefix }}"
security_groups: "{{ sg.group_id }}"
vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}"
termination_protection: true
instance_type: "{{ ec2_instance_type }}"
state: running
wait: yes
register: in_test_vpc
- 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"
image_id: "{{ ec2_ami_image }}"
tags:
TestId: "{{ resource_prefix }}"
security_groups: "{{ sg.group_id }}"
vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}"
termination_protection: true
instance_type: "{{ ec2_instance_type }}"
check_mode: yes
- name: "fact presented ec2 instance"
ec2_instance_info:
filters:
"tag:Name": "{{ resource_prefix }}-test-protected-instance-in-vpc"
"instance-state-name": "running"
register: presented_instance_fact
- name: "fact checkmode ec2 instance"
ec2_instance_info:
filters:
"tag:Name": "{{ resource_prefix }}-test-protected-instance-in-vpc-checkmode"
register: checkmode_instance_fact
- 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"
image_id: "{{ ec2_ami_image }}"
tags:
TestId: "{{ resource_prefix }}"
security_groups: "{{ sg.group_id }}"
vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}"
termination_protection: true
instance_type: "{{ ec2_instance_type }}"
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"
image_id: "{{ ec2_ami_image }}"
vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}"
termination_protection: false
instance_type: "{{ ec2_instance_type }}"
- name: Try to terminate the instance again (should work)
ec2_instance:
name: "{{ resource_prefix }}-test-protected-instance-in-vpc"
image_id: "{{ ec2_ami_image }}"
vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}"
instance_type: "{{ ec2_instance_type }}"
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