---
- name: Include prompts
  import_tasks: prompts.yml

- set_fact:
    algo_region: >-
      {%- if region is defined -%}{{ region }}{%-
      elif _algo_region.user_input -%}{{ azure_regions[_algo_region.user_input | int - 1]['name'] }}{%-
      else -%}{{ azure_regions[default_region | int - 1]['name'] }}{%-
      endif -%}

- name: Create AlgoVPN Server
  azure.azcollection.azure_rm_deployment:
    state: present
    deployment_name: "{{ algo_server_name }}"
    template: "{{ lookup('file', role_path + '/files/deployment.json') }}"
    secret: "{{ secret }}"
    tenant: "{{ tenant }}"
    client_id: "{{ client_id }}"
    subscription_id: "{{ subscription_id }}"
    resource_group_name: "{{ algo_server_name }}"
    location: "{{ algo_region }}"
    parameters:
      sshKeyData:
        value: "{{ lookup('file', SSH_keys.public) }}"
      WireGuardPort:
        value: "{{ wireguard_port }}"
      vmSize:
        value: "{{ cloud_providers.azure.size }}"
      imageReferencePublisher:
        value: "{{ cloud_providers.azure.image.publisher }}"
      imageReferenceOffer:
        value: "{{ cloud_providers.azure.image.offer }}"
      imageReferenceSku:
        value: "{{ cloud_providers.azure.image.sku }}"
      imageReferenceVersion:
        value: "{{ cloud_providers.azure.image.version }}"
      osDiskType:
        value: "{{ cloud_providers.azure.osDisk.type }}"
      SshPort:
        value: "{{ ssh_port }}"
      UserData:
        value: "{{ lookup('template', 'files/cloud-init/base.yml') | b64encode }}"
  register: azure_rm_deployment

- set_fact:
    cloud_instance_ip: "{{ azure_rm_deployment.deployment.outputs.publicIPAddresses.value }}"
    ansible_ssh_user: algo
    ansible_ssh_port: "{{ ssh_port }}"
    cloudinit: true
