---
#
# query asg's and determine to get details of what needs to be deleted.
# if we find more than 1 for each type, then exit
- name: query all asg's for this cluster
  ec2_asg_facts:
    region: "{{ openshift_aws_region }}"
    tags: "{{ {'kubernetes.io/cluster/' ~ openshift_aws_clusterid: openshift_aws_clusterid} | combine(openshift_aws_node_group.tags) }}"
  register: asgs

- fail:
    msg: "Found more than 1 auto scaling group that matches the query for group: {{ openshift_aws_node_group }}"
  when:
  - asgs.results|length > 1

- debug:
    msg: "{{ asgs }}"
    verbosity: 1

# if re-running deprovisioning, the scale group/launch config may have already
# been removed
- name: delete launch configs and scale groups
  when: asgs.results|length == 1
  block:

  - name: save launch config and scale group names
    set_fact:
      l_delete_sg_launch_config_name: "{{ asgs.results[0].launch_config_name }}"
      l_delete_sg_name: "{{ asgs.results[0].auto_scaling_group_name }}"

  - when: openshift_aws_create_scale_group
    include_tasks: uninstall_scale_group.yml

  - when: openshift_aws_create_launch_config
    include_tasks: uninstall_launch_config.yml

- name: get list of LCs
  ec2_lc_facts:
    region: "{{ openshift_aws_region }}"
  register: l_launch_config_list

- name: remove stray LCs for this node group
  ec2_lc:
    name: "{{ item.launch_configuration_name }}"
    state: absent
    region: "{{ openshift_aws_region }}"
  when: item.launch_configuration_name is match("^" ~ openshift_aws_node_group.name ~ ".*")
  with_items: "{{ l_launch_config_list.launch_configurations }}"
