【问题标题】:How to create a virtual machine on Azure using a python code?如何使用 python 代码在 Azure 上创建虚拟机?
【发布时间】:2015-07-09 20:17:24
【问题描述】:

我不熟悉使用 azure sdk 并使用它。 我阅读了很多关于如何创建虚拟机的示例,但没有一个对我有用。

在创建虚拟机之前,我在 sms.add_os_image 方法中遇到错误。 没有这种方法可以创建虚拟机吗?在这种情况下应该使用什么作为媒体链接和图像名称?

我当前错误代码的一部分如下所示:

#storage account:
result = sms.create_storage_account(name, desc, label, affinity_group=name)

operation_result = sms.get_operation_status(result.request_id)
print('Operation status: ' + operation_result.status)

result = sms.list_storage_accounts()
for account in result:
print('Service name: ' + account.service_name)
print('Affinity group: ' + account.storage_service_properties.affinity_group)
print('Location: ' + account.storage_service_properties.location)
print('')

#blob service:

storage_response=sms.get_storage_account_keys(name)
print "finished getting primary key " + storage_response.storage_service_keys.primary
account_key = storage_response.storage_service_keys.primary
blob_service = BlobService(account_name=name, account_key=account_key)

#container:

blob_service.create_container('containermaryland13')
target_blob_name = name+'.vhd'
os_image_url='https://{}.blob.core.windows.net/containermaryland13/{}'.format(name, target_blob_name)
image_name = '03f55de797f546a1b29d1b8d66be687a__CoreCLR-x64-Beta5-Linux-PartsUnlimited-Demo-App-201504.29'
sms.add_os_image(label=image_name, media_link=os_image_url, name=image_name, os='Linux')



linux_config = LinuxConfigurationSet(host_name='hostname', user_name='username', user_password='mypassword', disable_ssh_password_authentication=True)
os_hd = OSVirtualHardDisk(source_image_name=image_name, media_link=os_image_url)

sms.create_virtual_machine_deployment(service_name=name,
                                  deployment_name=name,
                                  deployment_slot='production',
                                  label=name,
                                  role_name=name,
                                  system_config=linux_config,
                                  os_virtual_hard_disk=os_hd,
                                  role_size='Small')
                                  """

有人可以帮我解决这个错误吗?

谢谢!

【问题讨论】:

    标签: python azure virtual-machine azure-virtual-machine


    【解决方案1】:

    一般来说,如果这张图片已经在你的IMAGES Gallery 中,你在重复添加时会遇到“冲突”的错误。所以我建议你检查这张图片是否已经注册在你的图片库中。如果您的 IMAGE Gallery 已有图片,您可以直接使用其图片名称和 URL。 另外,我创建了一个项目来测试这个方法:

    os_image_url='https://**.blob.core.windows.net/communityimages/**-**-**-1.vhd'
    image_name = 'ub-13-4-test'
    sms.add_os_image(label=image_name, media_link=os_image_url, name=image_name, os='Linux')
    

    在创建 VM 之前,您需要创建一个云服务作为 VM 的容器。 你可以看到这段代码:

    name = 'myvmPython'
    location = 'East Asia'
    sms.create_hosted_service(service_name=name,
        label=name,
        location=location)
    linux_config = LinuxConfigurationSet(host_name=name, user_name='*', user_password='*', disable_ssh_password_authentication=True)
    os_hd = OSVirtualHardDisk(source_image_name=image_name, media_link=os_image_url)
    
    sms.create_virtual_machine_deployment(service_name=name,
                                      deployment_name=name,
                                      deployment_slot='production',
                                      label=name,
                                      role_name=name,
                                      system_config=linux_config,
                                      os_virtual_hard_disk=os_hd,
                                      role_size='Small')
    

    顺便说一下,我使用的是 Python 2.7。此外,您可以与我们分享您的错误消息以获得进一步的帮助。

    【讨论】:

      【解决方案2】:

      您可以从图像面板检查您的图像是否存在,如下图所示: 此外,您可以将图像添加到图库:

      如果图像在您的图像存储库中,您可以直接使用其名称和 URL。

      根据错误信息,这意味着您的项目没有找到 VHD 资源,请检查您的 Blob URL。

      【讨论】:

      • 非常感谢您的回答! :)
      猜你喜欢
      • 2021-01-02
      • 2019-05-10
      • 1970-01-01
      • 1970-01-01
      • 2012-09-24
      • 1970-01-01
      • 2022-12-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多