Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revise teardown strategy to handle 200+ instances #1589

Open
wants to merge 1 commit into
base: devel
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion roles/manage_ec2_instances/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Original file line number Diff line number Diff line change
@@ -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

102 changes: 102 additions & 0 deletions roles/manage_ec2_instances/tasks/teardown_includes/teardown_prep.yml
Original file line number Diff line number Diff line change
@@ -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
#
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -432,3 +297,4 @@
when:
- snapshots_details.snapshots|length > 0
when: workshop_type == 'smart_mgmt'

Loading