Create VM on Azure with custom image using python

感情迁移 提交于 2019-12-11 02:25:13

问题


Launching a VM using a marketplace image in Azure is pretty straight forward.

here is the relevant piece of code.

def create_vm(network_client, compute_client):

vm_parameters = {
    'storage_profile': {
        'image_reference': {
            'publisher': 'MicrosoftWindowsServer',
            'offer': 'WindowsServer',
            'sku': '2012-R2-Datacenter',
            'version': 'latest'
        }
    },

vm = compute_client.virtual_machines.create_or_update(
    GROUP_NAME, 
    VM_NAME, 
    vm_parameters
)

(Clearly there is more in the actual code, this is the part that I think is most relevant)

So in this case the image reference points to the marketplace.

I used the following doc to create a custom image.

https://docs.microsoft.com/en-us/azure/virtual-machines/windows/create-vm-generalized-managed?toc=%2fazure%2fvirtual-machines%2fwindows%2ftoc.json

I would like to create a VM based on the new custom image that I created. It is my perception that image_reference should point to something else but it isn't clear to me what it should be. Can anybody help here?

Thanks!


回答1:


In fact, Azure Python SDK uses Azure Rest API. You could check this example.

So, you could modify your script like below:

vm_parameters = {
    'storage_profile': {
        'image_reference': {
            'id' : '/subscriptions/{subscription-id}/resourceGroups/myResourceGroup/providers/Microsoft.Compute/images/{existing-custom-image-name}'
        }
    },


来源:https://stackoverflow.com/questions/49068135/create-vm-on-azure-with-custom-image-using-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!