Jagadeesh Reddy
Jagadeesh Reddy

Reputation: 41

Azure VM provisioning - custom data

anyone proficient in azure SDK for python? I'm trying to create many VM's using an image that I captured from another VM. Questions:

  1. How do I put the custom image for deploying?
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. How do I give custom data(metadata) to the instances? (the metadata will change from time to time)

This is the article I am following, https://learn.microsoft.com/en-us/azure/developer/python/azure-sdk-example-virtual-machines?tabs=cmd

Any help is appreciated.

Upvotes: 3

Views: 562

Answers (1)

Nancy Xiong
Nancy Xiong

Reputation: 28224

If you have captured a managed image from Azure VM, you can reference it with id in ImageReference when deploying a new VM as the following code. If you intend to deploy it from a .vhd file, you can refer to this.

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,

You can specify a base-64 encoded string of custom data in OSProfile. I am using Python 3.9.4.

import base64

...

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

...

Upvotes: 3

Related Questions