2018-10-11 20:21:01 +02:00
|
|
|
- block:
|
|
|
|
- name: Make basic instance
|
|
|
|
ec2_instance:
|
|
|
|
name: "{{ resource_prefix }}-checkmode-comparison"
|
2019-10-21 14:55:44 +02:00
|
|
|
image_id: "{{ ec2_ami_image }}"
|
2018-10-11 20:21:01 +02:00
|
|
|
security_groups: "{{ sg.group_id }}"
|
2019-10-21 14:55:44 +02:00
|
|
|
instance_type: "{{ ec2_instance_type }}"
|
2018-10-11 20:21:01 +02:00
|
|
|
vpc_subnet_id: "{{ testing_subnet_a.subnet.id }}"
|
2019-05-10 20:43:15 +02:00
|
|
|
wait: false
|
2019-03-06 13:46:37 +01:00
|
|
|
register: basic_instance
|
2018-10-11 20:21:01 +02:00
|
|
|
|
|
|
|
- name: Make basic instance(check mode)
|
|
|
|
ec2_instance:
|
|
|
|
name: "{{ resource_prefix }}-checkmode-comparison-checkmode"
|
2019-10-21 14:55:44 +02:00
|
|
|
image_id: "{{ ec2_ami_image }}"
|
2018-10-11 20:21:01 +02:00
|
|
|
security_groups: "{{ sg.group_id }}"
|
2019-10-21 14:55:44 +02:00
|
|
|
instance_type: "{{ ec2_instance_type }}"
|
2019-03-06 13:46:37 +01:00
|
|
|
vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}"
|
2018-10-11 20:21:01 +02:00
|
|
|
check_mode: yes
|
|
|
|
|
|
|
|
- name: fact presented ec2 instance
|
2019-06-10 19:33:38 +02:00
|
|
|
ec2_instance_info:
|
2018-10-11 20:21:01 +02:00
|
|
|
filters:
|
|
|
|
"tag:Name": "{{ resource_prefix }}-checkmode-comparison"
|
|
|
|
register: presented_instance_fact
|
|
|
|
|
|
|
|
- name: fact checkmode ec2 instance
|
2019-06-10 19:33:38 +02:00
|
|
|
ec2_instance_info:
|
2018-10-11 20:21:01 +02:00
|
|
|
filters:
|
|
|
|
"tag:Name": "{{ resource_prefix }}-checkmode-comparison-checkmode"
|
|
|
|
register: checkmode_instance_fact
|
|
|
|
|
|
|
|
- name: Confirm whether the check mode is working normally.
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- "{{ presented_instance_fact.instances | length }} > 0"
|
|
|
|
- "{{ checkmode_instance_fact.instances | length }} == 0"
|
|
|
|
|
|
|
|
- name: Stop instance in check mode.
|
|
|
|
ec2_instance:
|
|
|
|
name: "{{ resource_prefix }}-checkmode-comparison"
|
|
|
|
state: stopped
|
|
|
|
vpc_subnet_id: "{{ testing_subnet_a.subnet.id }}"
|
|
|
|
check_mode: yes
|
|
|
|
|
|
|
|
- name: fact ec2 instance
|
2019-06-10 19:33:38 +02:00
|
|
|
ec2_instance_info:
|
2018-10-11 20:21:01 +02:00
|
|
|
filters:
|
|
|
|
"tag:Name": "{{ resource_prefix }}-checkmode-comparison"
|
|
|
|
register: confirm_checkmode_stopinstance_fact
|
|
|
|
|
|
|
|
- name: Verify that it was not stopped.
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- '"{{ confirm_checkmode_stopinstance_fact.instances[0].state.name }}" != "stopped"'
|
|
|
|
|
2019-05-10 20:43:15 +02:00
|
|
|
- name: Stop instance.
|
2018-10-11 20:21:01 +02:00
|
|
|
ec2_instance:
|
|
|
|
name: "{{ resource_prefix }}-checkmode-comparison"
|
|
|
|
state: stopped
|
|
|
|
vpc_subnet_id: "{{ testing_subnet_a.subnet.id }}"
|
2019-05-10 20:43:15 +02:00
|
|
|
register: instance_stop
|
|
|
|
until: not instance_stop.failed
|
|
|
|
retries: 10
|
|
|
|
|
2018-10-11 20:21:01 +02:00
|
|
|
- name: fact stopped ec2 instance
|
2019-06-10 19:33:38 +02:00
|
|
|
ec2_instance_info:
|
2018-10-11 20:21:01 +02:00
|
|
|
filters:
|
|
|
|
"tag:Name": "{{ resource_prefix }}-checkmode-comparison"
|
|
|
|
register: confirm_stopinstance_fact
|
|
|
|
|
|
|
|
- name: Verify that it was stopped.
|
|
|
|
assert:
|
|
|
|
that:
|
2019-05-10 20:43:15 +02:00
|
|
|
- '"{{ confirm_stopinstance_fact.instances[0].state.name }}" in ["stopped", "stopping"]'
|
2018-10-11 20:21:01 +02:00
|
|
|
|
|
|
|
- name: Running instance in check mode.
|
|
|
|
ec2_instance:
|
|
|
|
name: "{{ resource_prefix }}-checkmode-comparison"
|
|
|
|
state: running
|
|
|
|
vpc_subnet_id: "{{ testing_subnet_a.subnet.id }}"
|
|
|
|
check_mode: yes
|
2019-10-21 14:55:44 +02:00
|
|
|
|
2018-10-11 20:21:01 +02:00
|
|
|
- name: fact ec2 instance
|
2019-06-10 19:33:38 +02:00
|
|
|
ec2_instance_info:
|
2018-10-11 20:21:01 +02:00
|
|
|
filters:
|
|
|
|
"tag:Name": "{{ resource_prefix }}-checkmode-comparison"
|
|
|
|
register: confirm_checkmode_runninginstance_fact
|
2019-10-21 14:55:44 +02:00
|
|
|
|
2018-10-11 20:21:01 +02:00
|
|
|
- name: Verify that it was not running.
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- '"{{ confirm_checkmode_runninginstance_fact.instances[0].state.name }}" != "running"'
|
|
|
|
|
2019-05-10 20:43:15 +02:00
|
|
|
- name: Running instance.
|
2018-10-11 20:21:01 +02:00
|
|
|
ec2_instance:
|
|
|
|
name: "{{ resource_prefix }}-checkmode-comparison"
|
|
|
|
state: running
|
|
|
|
vpc_subnet_id: "{{ testing_subnet_a.subnet.id }}"
|
2019-10-21 14:55:44 +02:00
|
|
|
|
2018-10-11 20:21:01 +02:00
|
|
|
- name: fact ec2 instance.
|
2019-06-10 19:33:38 +02:00
|
|
|
ec2_instance_info:
|
2018-10-11 20:21:01 +02:00
|
|
|
filters:
|
|
|
|
"tag:Name": "{{ resource_prefix }}-checkmode-comparison"
|
|
|
|
register: confirm_runninginstance_fact
|
|
|
|
|
|
|
|
- name: Verify that it was running.
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- '"{{ confirm_runninginstance_fact.instances[0].state.name }}" == "running"'
|
|
|
|
|
|
|
|
- name: Terminate instance in check mode.
|
|
|
|
ec2_instance:
|
|
|
|
name: "{{ resource_prefix }}-checkmode-comparison"
|
|
|
|
state: absent
|
|
|
|
vpc_subnet_id: "{{ testing_subnet_a.subnet.id }}"
|
|
|
|
check_mode: yes
|
|
|
|
|
|
|
|
- name: fact ec2 instance
|
2019-06-10 19:33:38 +02:00
|
|
|
ec2_instance_info:
|
2018-10-11 20:21:01 +02:00
|
|
|
filters:
|
|
|
|
"tag:Name": "{{ resource_prefix }}-checkmode-comparison"
|
|
|
|
register: confirm_checkmode_terminatedinstance_fact
|
2019-10-21 14:55:44 +02:00
|
|
|
|
2018-10-11 20:21:01 +02:00
|
|
|
- name: Verify that it was not terminated,
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- '"{{ confirm_checkmode_terminatedinstance_fact.instances[0].state.name }}" != "terminated"'
|
|
|
|
|
2019-05-10 20:43:15 +02:00
|
|
|
- name: Terminate instance.
|
2018-10-11 20:21:01 +02:00
|
|
|
ec2_instance:
|
|
|
|
name: "{{ resource_prefix }}-checkmode-comparison"
|
|
|
|
state: absent
|
|
|
|
vpc_subnet_id: "{{ testing_subnet_a.subnet.id }}"
|
|
|
|
|
|
|
|
- name: fact ec2 instance
|
2019-06-10 19:33:38 +02:00
|
|
|
ec2_instance_info:
|
2018-10-11 20:21:01 +02:00
|
|
|
filters:
|
|
|
|
"tag:Name": "{{ resource_prefix }}-checkmode-comparison"
|
|
|
|
register: confirm_terminatedinstance_fact
|
2019-10-21 14:55:44 +02:00
|
|
|
|
2018-10-11 20:21:01 +02:00
|
|
|
- name: Verify that it was terminated,
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- '"{{ confirm_terminatedinstance_fact.instances[0].state.name }}" == "terminated"'
|
2019-10-21 14:55:44 +02:00
|
|
|
|
2018-10-11 20:21:01 +02:00
|
|
|
always:
|
|
|
|
- name: Terminate instance
|
|
|
|
ec2:
|
|
|
|
instance_ids: "{{ basic_instance.instance_ids }}"
|
|
|
|
state: absent
|
2019-10-21 14:55:44 +02:00
|
|
|
wait: false
|
2018-10-11 20:21:01 +02:00
|
|
|
register: removed
|
|
|
|
until: removed is not failed
|
|
|
|
ignore_errors: yes
|
|
|
|
retries: 10
|