How to create a custom image virtual machine using azure-sdk-for-python?

I am using the "new" azure sdk for python: https://github.com/Azure/azure-sdk-for-python

A related one is a usage example that serves as documentation: https://azure-sdk-for-python.readthedocs.org/en/latest/resourcemanagementcomputenetwork.html

In this example, they create an instance from a public image, providing the image publisher, offer, SKU and version. I would like to create an instance from a user image (present in “My Images” on the azure portal), for which I have only the image name, neither the publisher nor the SKU.

Is it supported? How do I proceed?

Note. I would like to avoid using the azure command line if possible, only relying on the python library.

Thanks!

+4
source share
2 answers

In case anyone else encounters this problem, SourceImage is actually used for an older method (ASM). For ARM, the following will initialize StorageProfile to provide a reference to the user image:

storage_profile = azure.mgmt.compute.StorageProfile(
    os_disk=azure.mgmt.compute.OSDisk(
        caching=azure.mgmt.compute.CachingTypes.none,
        create_option=azure.mgmt.compute.DiskCreateOptionTypes.from_image,
        name=OS_DISK_NAME,
        virtual_hard_disk=azure.mgmt.compute.VirtualHardDisk(
            uri='https://{0}.blob.core.windows.net/vhds/{1}.vhd'.
            format(STORAGE_NAME, OS_DISK_NAME),
        ),
        operating_system_type='Linux',
        source_image=azure.mgmt.compute.VirtualHardDisk(
            uri='https://{0}.blob.core.windows.net/{1}/{2}'.format(
                STORAGE_NAME, CUSTOM_IMAGE_PATH, CUSTOM_IMAGE_VHD),
        ),
    ),
)

The two important things described above are "operating_system_type" and the way to create the source file.

+1
source

The Azure SDK for Python support creates a virtual machine with a custom image.

" " Azure Portal, OS_DISK_NAME & STORAGE_NAME VHD Azure Python SDK.

API- Azure Python SDK API REST. REST API " " https://msdn.microsoft.com/en-us/library/azure/mt163591.aspx, osDisk (. , ).

enter image description here

, ( image_reference), :

# 4. Create the virtual machine

result = compute_client.virtual_machines.create_or_update(
    GROUP_NAME,
    azure.mgmt.compute.VirtualMachine(
        location=REGION,
        name=VM_NAME,
        os_profile=azure.mgmt.compute.OSProfile(
            admin_username=ADMIN_USERNAME,
            admin_password=ADMIN_PASSWORD,
            computer_name=COMPUTER_NAME,
        ),
        hardware_profile=azure.mgmt.compute.HardwareProfile(
          virtual_machine_size=azure.mgmt.compute.VirtualMachineSizeTypes.standard_a0
        ),
        network_profile=azure.mgmt.compute.NetworkProfile(
            network_interfaces=[
                azure.mgmt.compute.NetworkInterfaceReference(
                    reference_uri=nic_id,
                ),
            ],
        ),
        storage_profile=azure.mgmt.compute.StorageProfile(
            os_disk=azure.mgmt.compute.OSDisk(
                caching=azure.mgmt.compute.CachingTypes.none,
                create_option=azure.mgmt.compute.DiskCreateOptionTypes.from_image,
                name=OS_DISK_NAME, // Your VHD name
                virtual_hard_disk=azure.mgmt.compute.VirtualHardDisk(
                    uri='https://{0}.blob.core.windows.net/vhds/{1}.vhd'.format(
                        STORAGE_NAME, // your storage account name
                        OS_DISK_NAME, // Your VHD name
                    ),
                ),
            )
        ),
    ),
)
0

Source: https://habr.com/ru/post/1612667/


All Articles