簡體   English   中英

如何使用azure-sdk-for-python創建具有自定義映像的VM?

[英]How to create a VM with a custom image using azure-sdk-for-python?

我正在使用python的“新”azure sdk: https//github.com/Azure/azure-sdk-for-python

Linked是一個用作文檔的用法示例: https//azure-sdk-for-python.readthedocs.org/en/latest/resourcemanagementcomputenetwork.html

在此示例中,他們從公共圖像創建實例,提供圖像發布者,商品,SKU和版本。 我想從自定義圖像創建一個實例(存在於天藍色門戶網站上的“我的圖像”中),我只有一個圖像名稱,沒有發布者或SKU。

這支持嗎? 我該怎么辦?

注意:如果可能的話,我想避免使用azure CLI命令,只依賴於python庫。

謝謝!

如果其他人遇到此問題,SourceImage實際上是舊方法(ASM)。 對於ARM,以下內容將初始化StorageProfile以提供對自定義映像的引用:

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),
        ),
    ),
)

上面兩個非常重要的事情是'operating_system_type'以及創建source_image的方式。

Azure SDK for Python支持使用自定義映像創建VM。

如果Azure門戶上的“我的圖像”中存在自定義圖像,則可以使用Azure Python SDK在存儲上使用VHD圖像的OS_DISK_NAMESTORAGE_NAME參數創建VM。

Azure Python SDK API包含相同的功能REST API。 請參閱REST API文檔“創建或更新虛擬機” https://msdn.microsoft.com/en-us/library/azure/mt163591.aspx ,創建具有映像的VM只需要osDisk存儲配置文件元素(看下面的快照)。

在此輸入圖像描述

因此,示例代碼僅部分修改(刪除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
                    ),
                ),
            )
        ),
    ),
)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM