Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 596
Jagadeesh Reddy Avatar asked Apr 01 '26 03:04

Jagadeesh Reddy


1 Answers

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

...
like image 63
Nancy Xiong Avatar answered Apr 02 '26 20:04

Nancy Xiong



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!