###
# This Playbook creates VM from a given Azure image URN 
###

---
- name: Debug input parameter
  debug:
    msg: "{{ item.split(':')[0] }} {{ item.split(':')[1] }} {{ item.split(':')[2] }} {{ item.split(':')[3] }}" 

- name: Set image properties
  set_fact:
    publisher: "{{ item.split(':')[0] }}"
    offer: "{{ item.split(':')[1] }}"
    sku: "{{ item.split(':')[2] }}"
    version: "{{ item.split(':')[3] }}"
    ssh_pub_key: "{{ lookup('file', '~/.ssh/id_rsa.pub') }}"
    resource_group: "{{ lookup('env', 'RESOURCE_GROUP') }}"
    vnet_name: "{{ lookup('env', 'HOST_VNET_NAME') }}"
    container_name: "{{ lookup('env', 'CONTAINER_NAME') }}"
    admin_user_name: "{{ lookup('env', 'ADMIN_USER_NAME') }}"
    admin_password: "{{ lookup('env', 'ADMIN_PASSWORD') }}"

- name: Set VM name variable
  set_fact:
    vmname: "{{ offer | replace('_','-') }}-{{ sku | replace('_','-') }}-{{ version }}"
    
- debug:
    msg: "Creating VM - {{ vmname }}"

- name: Create VM
  no_log: true
  azure_rm_virtualmachine:
    resource_group: "{{ resource_group }}"
    name: "{{ vmname }}"
    admin_username: "{{ admin_user_name }}"
    admin_password: "{{ admin_password }}"
    managed_disk_type: Standard_LRS
    ssh_public_keys:
      - path: "/home/{{ admin_user_name }}/.ssh/authorized_keys"
        key_data: "{{ ssh_pub_key }}"
    vm_size: Standard_B1ms
    virtual_network_name: "{{ vnet_name }}"
    image:
      offer: "{{ offer }}"
      publisher: "{{ publisher }}"
      sku: "{{ sku }}"
      version: "{{ version }}"
  register: azurevm
  ignore_errors: yes

- set_fact:
    azurevm_privateip: "{{ azurevm.ansible_facts.azure_vm.properties.networkProfile.networkInterfaces[0].properties.ipConfigurations[0].properties.privateIPAddress }}"
  when: azurevm.failed == false
  ignore_errors: yes

- name: Add the new VM to in-memory inventory
  add_host:
    hostname: '{{ vmname }}'
    groups: just_created
    ansible_host: "{{ azurevm_privateip }}"
  when: azurevm.failed == false