---
- name: Destroy any existing droplets
  hosts: 127.0.0.1
  vars_files:
    - ../vars/secrets.yml
  tasks:
    - name: Check for existing inventory
      community.digitalocean.digital_ocean_droplet_info:
        oauth_token: "{{ DO_API_TOKEN }}"
      register: droplets

    - name: Count existing droplets
      set_fact:
        droplet_count: "{{ droplets.data | length }}"

    - name: Delete existing droplets
      community.digitalocean.digital_ocean_droplet:
        state: absent
        oauth_token: "{{ DO_API_TOKEN }}"
        id: "{{ item.id }}"
      loop: "{{ droplets.data }}"
      when: droplet_count != "0"
