Azure VM 配置 - 自定义数据

Azure VM provisioning - custom data

有人精通 python 的 azure SDK 吗? 我正在尝试使用从另一个 VM 捕获的映像创建多个 VM。 问题:

  1. 如何放置自定义图像进行部署?
poller =  compute_client.virtual_machines.begin_create_or_update(RESOURCE_GROUP_NAME, VM_NAME,
    {
        "location": LOCATION,
        "storage_profile": {
            "image_reference": {
                "publisher": 'Canonical',
                "offer": "UbuntuServer",
                "sku": "18.04-LTS",
                "version": "latest"
            }
        }
}
  1. 如何为实例提供自定义数据(元数据)? (元数据会不时更改)

这是我正在关注的文章, https://docs.microsoft.com/en-us/azure/developer/python/azure-sdk-example-virtual-machines?tabs=cmd

感谢任何帮助。

如果您从 Azure VM 捕获了托管映像,则可以在 ImageReference when deploying a new VM as the following code. If you intend to deploy it from a .vhd file, you can refer to 中使用 id 引用它。

poller = compute_client.virtual_machines.begin_create_or_update(RESOURCE_GROUP_NAME, VM_NAME,
    {
        "location": LOCATION,
        "storage_profile": {
           
            "image_reference": {
               "id": "/subscriptions/{subscription-id}/resourceGroups/{myResourceGroup}/providers/Microsoft.Compute/images/{existing-custom-image-name}"
            }
        },
    "hardware_profile": {
        "vm_size": "Standard_DS1_v2"
    },
    "os_profile": {
        "computer_name": VM_NAME,
        "admin_username": USERNAME,
        # "admin_password": PASSWORD,
        "custom_data": encoded_string,

您可以在 OSProfile 中指定自定义数据的 base-64 编码字符串。我正在使用 Python 3.9.4.

import base64

...

file = open("custom-data.sh", "rb")
a = file.read()
encoded_string = base64.b64encode(a).decode('utf-8')

...