Home > front end >  I want to create the exact same VM I have on Azure console with Python SDK. Right now, I am facing i
I want to create the exact same VM I have on Azure console with Python SDK. Right now, I am facing i

Time:12-20

The VM I can create now is via VS Code. I use the same image as the one in console VM. Now I want to run a user script which uses some files. When I login to the VS Code VM, it is empty, so obviously the use script isn't working. These are the VM Parameters that I am using..

def create_vm_parameters(vm_reference, compute_client, encoded_init_script, filename, nic_id):
"""Create the VM parameters structure.
"""
return {
    'location': LOCATION,
    'tags': {
        'Name': str(filename)
    },
    'os_profile': {
        'computer_name': VM_NAME,
        'admin_username': USERNAME,
        'admin_password': PASSWORD
    },
    'hardware_profile': {
        'vm_size': VM_TYPE
    },
    'storage_profile': {
        'image_reference': {
            'publisher': vm_reference['publisher'],
            'offer': vm_reference['offer'],
            'sku': vm_reference['sku'],
            'version': vm_reference['version']
        },
        'osDisk': {
            'createOption': 'FromImage'
        },
     },
    'network_profile': {
        'network_interfaces': [
            {
                'id': nic_id
            }
        ]
    },
    "userData": encoded_init_script,
}

CodePudding user response:

One way to achieve this would be to copy the data from the existing VM to a storage account, and then mount the storage account as a data disk to the new VM that you are creating using the Python SDK.

In the Python code, include the following in the storage_profile section of the VM parameters

'storage_profile': {
        'image_reference': {
            'publisher': vm_reference['publisher'],
            'offer': vm_reference['offer'],
            'sku': vm_reference['sku'],
            'version': vm_reference['version']
        },
        'osDisk': {
            'createOption': 'FromImage'
        },
        'dataDisks': [
            {
                'lun': 0,
                'createOption': 'Attach',
                'caching': 'ReadWrite',
                'diskSizeGB': 128,
                'managedDisk': {
                    'storageAccountType': 'Standard_LRS',
                    'id': '<storage-account-id>'
                }
            }
        ]
    },

replace with the ID of the storage account you created.

This should allow you to mount the storage account as a data disk to the new VM you are creating using the Python SDK. You can then access the data inside the VM by mounting the data disk.

CodePudding user response:

Solved this with VM Parameters: We

def create_vm_parameters(vm_reference, compute_client, encoded_init_script, filename, nic_id):
    """Create the VM parameters structure.
    """
    return {
        'location': LOCATION,
        'tags': {
            'Name': str(filename)
        },
        # 'os_profile': {
        #     'computer_name': VM_NAME,
        #     'admin_username': USERNAME,
        #     'admin_password': PASSWORD
        # },
        'hardware_profile': {
            'vm_size': VM_TYPE
        },
        'storage_profile': {
            # 'image_reference': {
            #     'publisher': vm_reference['publisher'],
            #     'offer': vm_reference['offer'],
            #     'sku': vm_reference['sku'],
            #     'version': vm_reference['version']
            # },
            'osDisk': {
                'osType': 'Linux',
                'name': '<Disk-Name>',
                'createOption': 'Attach',
                'managedDisk': {
                'id': '<resource-id>'
                },
                'caching': 'ReadWrite'
            }
        },
        'network_profile': {
            'network_interfaces': [
                {
                    'id': nic_id
                }
            ]
        },
        "userData": encoded_init_script,
    }

What I was doing here is including the image and OS references (which are now commented). Also, I took the snapshot of the existing OSDisk, and created another OSDisk(say, snap-OSDisk). Then attached this (snap-OSDisk) to this VM that is being created.

Obviously, if there is a better way to do this, I welcome it.

  • Related