---
- name: Workflow VPS Vultr modulaire
  hosts: localhost
  connection: local
  gather_facts: false

  vars_files:
    - vars.yml
  
#  vars:
#    vultr_api_key: "{{ lookup('env', 'VULTR_API_KEY') }}"
#    vps_name: "vps-temp-{{ ansible_date_time.epoch }}"
#    vps_region: "cdg"
#    vps_plan: "vc2-1c-1gb"
#    vps_os: "Debian 12 x64 (bookworm)"
#    block_storage_id: "{{ lookup('env', 'VULTR_BLOCK_STORAGE_ID') }}"
  
  tasks:
    - name: Créer le VPS
      vultr.cloud.server:
        name: "{{ vps_name }}"
        plan: "{{ vps_plan }}"
        region: "{{ vps_region }}"
        os: "{{ vps_os }}"
        state: present
      register: vps_creation
      tags: create

    - name: Attacher le block storage
      vultr.cloud.block_storage:
        id: "{{ block_storage_id }}"
        attached_to_instance: "{{ vps_creation.instance.id }}"
        state: present
      tags: attach

    - name: Pause pour sauvegarde
      pause:
        prompt: "Sauvegarde terminée ? Appuyez sur Entrée pour continuer"
      tags: backup

    - name: Détacher le block storage
      vultr.cloud.block_storage:
        id: "{{ block_storage_id }}"
        attached_to_instance: ""
        state: present
      tags: detach

    - name: Supprimer le VPS
      vultr.cloud.server:
        id: "{{ vps_creation.instance.id }}"
        state: absent
      tags: destroy
