diff --git a/roles/manage_ec2_instances/tasks/main.yml b/roles/manage_ec2_instances/tasks/main.yml index 1ad057d44..fdb52dea5 100644 --- a/roles/manage_ec2_instances/tasks/main.yml +++ b/roles/manage_ec2_instances/tasks/main.yml @@ -4,10 +4,22 @@ ec2_info: '{{ ec2_info|combine(ec2_xtra) }}' when: (ec2_xtra is defined) and (ec2_xtra is not none) -- include_tasks: teardown.yml +- name: teardown workshop infrastructure + block: + - include_tasks: teardown_includes/teardown_prep.yml + + - include_tasks: teardown_includes/teardown_student_instances.yml + loop: "{{ range(1, student_total + 1, 1) | list }}" + + - include_tasks: teardown_includes/teardown_student_until.yml + + - include_tasks: teardown_includes/teardown_misc_instances.yml + + - include_tasks: teardown_includes/teardown_remaining.yml when: teardown|bool - name: provision aws resources and instances include_tasks: provision.yml tags: provisioned when: not teardown|bool + diff --git a/roles/manage_ec2_instances/tasks/teardown_includes/teardown_misc_instances.yml b/roles/manage_ec2_instances/tasks/teardown_includes/teardown_misc_instances.yml new file mode 100644 index 000000000..91fd9ac0f --- /dev/null +++ b/roles/manage_ec2_instances/tasks/teardown_includes/teardown_misc_instances.yml @@ -0,0 +1,66 @@ +--- +# retrieve instances for VPC 1 +- name: grab vpc node facts for workshop + ec2_instance_info: + region: "{{ ec2_region }}" + filters: + "vpc-id": "{{ ec2_vpc_id }}" + "tag:Workshop": "{{ ec2_name_prefix }}" + register: all_workshop_vpc_nodes + +- name: debug all_workshop_vpc_nodes + debug: + var: all_workshop_vpc_nodes + when: debug_teardown + +# retrieve instances for VPC 2 +- name: grab vpc2 node facts for workshop + amazon.aws.ec2_instance_info: + region: "{{ ec2_region }}" + filters: + "vpc-id": "{{ ec2_vpc_id }}" + "tag:Workshop": "{{ ec2_name_prefix }}" + register: all_workshop_vpc2_nodes + when: ec2_vpc_id2 is defined + +- name: debug all_workshop_vpc2_nodes + debug: + var: all_workshop_vpc2_nodes + when: + - debug_teardown + - ec2_vpc_id2 is defined + +# Destroy VPC 1 instances +- name: destroy EC2 instances + amazon.aws.ec2_instance: + region: "{{ ec2_region }}" + state: absent + instance_ids: "{{ all_workshop_vpc_nodes.instances | map(attribute='instance_id') | list }}" + wait: true + wait_timeout: "{{ student_total * 300 | int}}" + register: result_ec2_destroy + when: all_workshop_vpc_nodes.instances + +- name: debug result_ec2_destroy + debug: + var: result_ec2_destroy + when: debug_teardown + +# Destroy VPC 2 instances for network automation +- name: destroy EC2 instances (VPC2) + amazon.aws.ec2_instance: + region: "{{ ec2_region }}" + state: absent + instance_ids: "{{ all_workshop_vpc2_nodes.instances | map(attribute='instance_id') | list }}" + wait: true + wait_timeout: "{{ student_total * 300 | int}}" + register: result_ec2_destroy2 + when: + - ec2_vpc_id2 is defined + - all_workshop_vpc2_nodes.instances + +- name: debug result_ec2_destroy2 + debug: + var: result_ec2_destroy2 + when: debug_teardown + diff --git a/roles/manage_ec2_instances/tasks/teardown_includes/teardown_prep.yml b/roles/manage_ec2_instances/tasks/teardown_includes/teardown_prep.yml new file mode 100644 index 000000000..4ef6ab598 --- /dev/null +++ b/roles/manage_ec2_instances/tasks/teardown_includes/teardown_prep.yml @@ -0,0 +1,102 @@ +--- +- name: Get the VPC ID for {{ ec2_name_prefix }} + amazon.aws.ec2_vpc_net_info: + filters: + "tag:Name": "{{ ec2_name_prefix }}-vpc" + region: "{{ ec2_region }}" + register: vpc_net_facts + +- name: debug vpc_net_facts + debug: + var: vpc_net_facts + when: debug_teardown + + +- name: Get the VPC ID 2 for {{ ec2_name_prefix }} (NETWORK MODE) + amazon.aws.ec2_vpc_net_info: + filters: + "tag:Name": "{{ ec2_name_prefix }}-vpc2" + region: "{{ ec2_region }}" + register: vpc_net_facts2 + when: workshop_type == 'networking' or workshop_type == 'network' or workshop_type == 'demo' + +- name: debug vpc_net_facts2 + debug: + var: vpc_net_facts2 + when: debug_teardown + +- name: debugging vpc id for {{ ec2_name_prefix }} + debug: + msg: "vpc id:'{{vpc_net_facts.vpcs[0].id}}'" + when: + - debug_teardown + - vpc_net_facts.vpcs|length > 0 + +- name: use set fact for easier variables + set_fact: + ec2_vpc_id: "{{vpc_net_facts.vpcs[0].id|default('WORKSHOP_UNDEF')}}" + ec2_security_group: "{{ ec2_name_prefix }}-insecure_all" + when: ec2_security_group is undefined + +- name: debug ec2_vpc_id + debug: + var: ec2_vpc_id + when: debug_teardown + +- name: debug ec2_security_group + debug: + var: ec2_security_group + when: debug_teardown + +# VPC 2 for network automation +- name: dynamic instance variable creation since VPC was not supplied by user (NETWORK MODE) + set_fact: + ec2_vpc_id2: "{{vpc_net_facts2.vpcs[0].id}}" + ec2_security_group2: "{{ ec2_name_prefix }}-insecure_all2" + when: + - workshop_type == 'networking' or workshop_type == 'network' or workshop_type == 'demo' + - vpc_net_facts2.vpcs|length > 0 + - ec2_security_group2 is undefined + +- name: debug ec2_vpc_id2 + debug: + var: ec2_vpc_id2 + when: + - debug_teardown + - ec2_vpc_id2 is defined + - workshop_type == 'networking' or workshop_type == 'network' or workshop_type == 'demo' + +- name: debug ec2_security_group2 + debug: + var: ec2_security_group2 + when: debug_teardown + +# retrieve instances for VPC 1 +#- name: grab vpc node facts for workshop +# ec2_instance_info: +# region: "{{ ec2_region }}" +# filters: +# "vpc-id": "{{ec2_vpc_id}}" +# register: all_workshop_vpc_nodes +# +#- name: debug all_workshop_vpc_nodes +# debug: +# var: all_workshop_vpc_nodes +# when: debug_teardown +# +## retrieve instances for VPC 2 +#- name: grab vpc2 node facts for workshop +# amazon.aws.ec2_instance_info: +# region: "{{ ec2_region }}" +# filters: +# "vpc-id": "{{ec2_vpc_id2}}" +# register: all_workshop_vpc2_nodes +# when: ec2_vpc_id2 is defined +# +#- name: debug all_workshop_vpc2_nodes +# debug: +# var: all_workshop_vpc2_nodes +# when: +# - debug_teardown +# - ec2_vpc_id2 is defined +# diff --git a/roles/manage_ec2_instances/tasks/teardown.yml b/roles/manage_ec2_instances/tasks/teardown_includes/teardown_remaining.yml similarity index 71% rename from roles/manage_ec2_instances/tasks/teardown.yml rename to roles/manage_ec2_instances/tasks/teardown_includes/teardown_remaining.yml index 4b9218c84..64654aacb 100644 --- a/roles/manage_ec2_instances/tasks/teardown.yml +++ b/roles/manage_ec2_instances/tasks/teardown_includes/teardown_remaining.yml @@ -1,139 +1,4 @@ --- -- name: Get the VPC ID for {{ ec2_name_prefix }} - amazon.aws.ec2_vpc_net_info: - filters: - "tag:Name": "{{ ec2_name_prefix }}-vpc" - region: "{{ ec2_region }}" - register: vpc_net_facts - -- name: debug vpc_net_facts - debug: - var: vpc_net_facts - when: debug_teardown - - -- name: Get the VPC ID 2 for {{ ec2_name_prefix }} (NETWORK MODE) - amazon.aws.ec2_vpc_net_info: - filters: - "tag:Name": "{{ ec2_name_prefix }}-vpc2" - region: "{{ ec2_region }}" - register: vpc_net_facts2 - when: workshop_type == 'networking' or workshop_type == 'network' or workshop_type == 'demo' - -- name: debug vpc_net_facts2 - debug: - var: vpc_net_facts2 - when: debug_teardown - -- name: debugging vpc id for {{ ec2_name_prefix }} - debug: - msg: "vpc id:'{{vpc_net_facts.vpcs[0].id}}'" - when: - - debug_teardown - - vpc_net_facts.vpcs|length > 0 - -- name: use set fact for easier variables - set_fact: - ec2_vpc_id: "{{vpc_net_facts.vpcs[0].id|default('WORKSHOP_UNDEF')}}" - ec2_security_group: "{{ ec2_name_prefix }}-insecure_all" - when: ec2_security_group is undefined - -- name: debug ec2_vpc_id - debug: - var: ec2_vpc_id - when: debug_teardown - -- name: debug ec2_security_group - debug: - var: ec2_security_group - when: debug_teardown - -# VPC 2 for network automation -- name: set variables for instance creation dynamically since VPC was not supplied by user (NETWORK MODE) - set_fact: - ec2_vpc_id2: "{{vpc_net_facts2.vpcs[0].id}}" - ec2_security_group2: "{{ ec2_name_prefix }}-insecure_all2" - when: - - workshop_type == 'networking' or workshop_type == 'network' or workshop_type == 'demo' - - vpc_net_facts2.vpcs|length > 0 - - ec2_security_group2 is undefined - -- name: debug ec2_vpc_id2 - debug: - var: ec2_vpc_id2 - when: - - debug_teardown - - ec2_vpc_id2 is defined - - workshop_type == 'networking' or workshop_type == 'network' or workshop_type == 'demo' - -- name: debug ec2_security_group2 - debug: - var: ec2_security_group2 - when: debug_teardown - -# retrieve instances for VPC 1 -- name: grab vpc node facts for workshop - ec2_instance_info: - region: "{{ ec2_region }}" - filters: - "vpc-id": "{{ec2_vpc_id}}" - register: all_workshop_vpc_nodes - -- name: debug all_workshop_vpc_nodes - debug: - var: all_workshop_vpc_nodes - when: debug_teardown - -# retrieve instances for VPC 2 -- name: grab vpc2 node facts for workshop - amazon.aws.ec2_instance_info: - region: "{{ ec2_region }}" - filters: - "vpc-id": "{{ec2_vpc_id2}}" - register: all_workshop_vpc2_nodes - when: ec2_vpc_id2 is defined - -- name: debug all_workshop_vpc2_nodes - debug: - var: all_workshop_vpc2_nodes - when: - - debug_teardown - - ec2_vpc_id2 is defined - -# Destroy VPC 1 instances -- name: destroy EC2 instances - amazon.aws.ec2_instance: - region: "{{ ec2_region }}" - state: absent - instance_ids: "{{ all_workshop_vpc_nodes.instances | map(attribute='instance_id') | list }}" - wait: true - wait_timeout: "{{ student_total * 300 | int}}" - register: result_ec2_destroy - when: all_workshop_vpc_nodes.instances - -- name: debug result_ec2_destroy - debug: - var: result_ec2_destroy - when: debug_teardown - -# Destroy VPC 2 instances for network automation -- name: destroy EC2 instances (VPC2) - amazon.aws.ec2_instance: - region: "{{ ec2_region }}" - state: absent - instance_ids: "{{ all_workshop_vpc2_nodes.instances | map(attribute='instance_id') | list }}" - wait: true - wait_timeout: "{{ student_total * 300 | int}}" - register: result_ec2_destroy2 - when: - - ec2_vpc_id2 is defined - - all_workshop_vpc2_nodes.instances - -- name: debug result_ec2_destroy2 - debug: - var: result_ec2_destroy2 - when: debug_teardown - - name: Cleanup subnets for {{ ec2_name_prefix }}-vpc (SECURITY MODE) block: - name: Get left ENI @@ -432,3 +297,4 @@ when: - snapshots_details.snapshots|length > 0 when: workshop_type == 'smart_mgmt' + diff --git a/roles/manage_ec2_instances/tasks/teardown_includes/teardown_student_instances.yml b/roles/manage_ec2_instances/tasks/teardown_includes/teardown_student_instances.yml new file mode 100644 index 000000000..df744c2e2 --- /dev/null +++ b/roles/manage_ec2_instances/tasks/teardown_includes/teardown_student_instances.yml @@ -0,0 +1,72 @@ +--- +# retrieve instances for VPC 1 +- name: grab vpc node facts for workshop + ec2_instance_info: + region: "{{ ec2_region }}" + filters: + "vpc-id": "{{ ec2_vpc_id }}" + "tag:Student": "student{{ item }}" + "tag:Workshop": "{{ ec2_name_prefix }}" + register: all_workshop_vpc_nodes + +- name: debug all_workshop_vpc_nodes + debug: + var: all_workshop_vpc_nodes + when: debug_teardown + +# retrieve instances for VPC 2 +- name: grab vpc2 node facts for workshop + amazon.aws.ec2_instance_info: + region: "{{ ec2_region }}" + filters: + "vpc-id": "{{ ec2_vpc_id2 }}" + "tag:Student": "student{{ item }}" + "tag:Workshop": "{{ ec2_name_prefix }}" + register: all_workshop_vpc2_nodes + when: ec2_vpc_id2 is defined + +- name: debug all_workshop_vpc2_nodes + debug: + var: all_workshop_vpc2_nodes + when: + - debug_teardown + - ec2_vpc_id2 is defined + +# Destroy VPC 1 instances +- name: destroy EC2 instances + amazon.aws.ec2_instance: + region: "{{ ec2_region }}" + state: absent + instance_ids: "{{ all_workshop_vpc_nodes.instances | map(attribute='instance_id') | list }}" + wait: true + wait_timeout: "{{ student_total * 300 | int}}" + register: result_ec2_destroy + async: 360 + poll: 0 + when: all_workshop_vpc_nodes.instances + +- name: debug result_ec2_destroy + debug: + var: result_ec2_destroy + when: debug_teardown + +# Destroy VPC 2 instances for network automation +- name: destroy EC2 instances (VPC2) + amazon.aws.ec2_instance: + region: "{{ ec2_region }}" + state: absent + instance_ids: "{{ all_workshop_vpc2_nodes.instances | map(attribute='instance_id') | list }}" + wait: true + wait_timeout: "{{ student_total * 300 | int}}" + register: result_ec2_destroy2 + async: 360 + poll: 0 + when: + - ec2_vpc_id2 is defined + - all_workshop_vpc2_nodes.instances + +- name: debug result_ec2_destroy2 + debug: + var: result_ec2_destroy2 + when: debug_teardown + diff --git a/roles/manage_ec2_instances/tasks/teardown_includes/teardown_student_until.yml b/roles/manage_ec2_instances/tasks/teardown_includes/teardown_student_until.yml new file mode 100644 index 000000000..440133267 --- /dev/null +++ b/roles/manage_ec2_instances/tasks/teardown_includes/teardown_student_until.yml @@ -0,0 +1,45 @@ +--- +# retrieve instances for VPC 1 +- name: grab vpc node facts for student{{ student_total }} instances + ec2_instance_info: + region: "{{ ec2_region }}" + filters: + "vpc-id": "{{ ec2_vpc_id }}" + "tag:Student": "student{{ student_total }}" + "tag:Workshop": "{{ ec2_name_prefix }}" + register: final_workshop_vpc_nodes + +- name: debug final_workshop_vpc_nodes + debug: + var: final_workshop_vpc_nodes + when: debug_teardown + +# retrieve instances for VPC 1 +- name: poll until last student nodes are terminated - VPC 1 + ec2_instance_info: + region: "{{ ec2_region }}" + filters: + "vpc-id": "{{ ec2_vpc_id }}" + "tag:Student": "student{{ student_total }}" + "tag:Workshop": "{{ ec2_name_prefix }}" + instance-state-name: [ "running", "shutting-down", "stopping", "stopped" ] + register: all_workshop_vpc_nodes + until: all_workshop_vpc_nodes.instances|length == 0 + retries: 36 + delay: 10 + +# retrieve instances for VPC 2 +- name: poll until last student nodes are terminated - VPC 2 + amazon.aws.ec2_instance_info: + region: "{{ ec2_region }}" + filters: + "vpc-id": "{{ ec2_vpc_id2 }}" + "tag:Student": "student{{ student_total }}" + "tag:Workshop": "{{ ec2_name_prefix }}" + instance-state-name: [ "running", "shutting-down", "stopping", "stopped" ] + register: all_workshop_vpc2_nodes + when: ec2_vpc_id2 is defined + until: all_workshop_vpc2_nodes.instances|length == 0 + retries: 36 + delay: 10 +