---
- name: Create VPS resource
  delegate_to: 127.0.0.1
  block:
    - name: Create SSH key
      community.digitalocean.digital_ocean_sshkey:
        oauth_token: "{{ DO_API_TOKEN }}"
        name: "Ansible Key"
        ssh_pub_key: "{{ SSH_PUBLIC_KEY }}"
        state: present
      register: public_key

    - name: Create automated builder VPS
      community.digitalocean.digital_ocean_droplet:
        state: present
        oauth_token: "{{ DO_API_TOKEN }}"
        name: automated-builder-vps
        size: s-4vcpu-8gb
        region: nyc3
        ## TODO: update when porting to Debian 14 / forky
        image: debian-13-x64
        wait_timeout: 500
        ssh_keys: ["{{ public_key.data.ssh_key.id }}"]
        project: "Automated Builder"
      register: automated_builder_vps

    - name: set VPS_IP
      set_fact:
        VPS_IP: "{{ automated_builder_vps.data.droplet.networks.v4 | selectattr('type', 'equalto', 'public') | map(attribute='ip_address') | first }}"

    - name: Set ansible_host to VPS IP for SSH connection
      set_fact:
        ansible_host: "{{ VPS_IP }}"
