azure icon indicating copy to clipboard operation
azure copied to clipboard

vm from existing managed disk

Open DII-dsward opened this issue 4 years ago • 3 comments

How to create a VM from an existing managed disk? We have a VHD that we'd like to convert to a managed disk and deploy the VM. Not seeing any documentation on that.

- name: Create managed disk from vhd
  azure_rm_manageddisk:
    name: "{{ vm_name }}-OS"
    location: "{{ Region }}"
    resource_group: "{{ ResourceGroup }}"
    create_option: import
    source_uri: "{{ vhd_uri }}"
    os_type: linux
    storage_account_type: Premium_LRS
    state: present

- name: Create Virtual Appliance
  azure_rm_virtualmachine:
    state: present
    resource_group: "{{ ResourceGroup }}"
    name: "{{ vm_name }}"
    vm_size: "{{ vm_size }}"
    network_interfaces: "{{ vm_nic_1 }}"
    os_disk_name: "{{vm_name}}-OS"
    managed_disk_type: "Premium_LRS"
    boot_diagnostics:
      enabled: "yes"
      storage_account: "{{ diag_name }}"
    availability_set: "{{ avs_name }}"

DII-dsward avatar Oct 14 '21 19:10 DII-dsward

@DII-dsward Can you try the following way? Thank you very much!

    - name: Create Resource group
      azure_rm_resourcegroup:
        name: "{{ resource_group }}"
        location: eastus

    - name: Create virtual network
      azure_rm_virtualnetwork:
        resource_group: "{{ resource_group }}"
        name: "{{ network_name }}"
        address_prefixes: 10.42.0.0/24

    - name: Create subnet
      azure_rm_subnet:
        resource_group: "{{ resource_group }}"
        name: "{{ subnet_name }}"
        address_prefix: 10.42.0.0/28
        virtual_network: "{{ network_name }}"

    - name: Create managed disk from vhd
      azure_rm_manageddisk:
        name: "{{ vm_name }}-OS"
        location: "{{ Region }}"
        resource_group: "{{ ResourceGroup }}"
        create_option: import
        source_uri: "{{ vhd_uri }}"
        os_type: linux
        storage_account_type: Premium_LRS
        state: present
     register: output

    - name: Create a VM multiple data managed disks
      azure_rm_virtualmachine:
        resource_group: "{{ resource_group }}"
        name: "{{ vm_name }}"
        vm_size: Standard_D4s_v3
        managed_disk_type: Standard_LRS
        admin_username: *****
        admin_password: ******
        ssh_public_keys:
          - path: /home/azureuser/.ssh/authorized_keys
            key_data: "ssh-rsa ****************"
        image:
          offer: UbuntuServer
          publisher: Canonical
          sku: 18.04-LTS
          version: latest
        data_disks:
          - lun: 0
            disk_size_gb: 1024
            managed_disk_id: "{{ output.state.id }}"

Fred-sun avatar Jul 08 '22 03:07 Fred-sun

@DII-dsward You may attach the disk to VM by azure_rm_manageddisk.py, Does this meet your means?

    - name: attach data disk
      azure_rm_manageddisk:
        name: "{{ disk_name }}"
        resource_group: "{{ resource_group }}"
        disk_size_gb: 1024
        attach_caching: read_only
        managed_by: "{{ virtual_machine.vm_name }}"
        zone: 1

Fred-sun avatar Feb 27 '23 06:02 Fred-sun

kindly ping!

Fred-sun avatar Mar 27 '23 02:03 Fred-sun