【问题标题】:Azure SDK ARM Template deployment: Could not find member 'id'Azure SDK ARM 模板部署:找不到成员\'id\'
【发布时间】:2022-11-23 14:23:22
【问题描述】:

我正在尝试使用 arm 模板通过 python azure sdk 部署虚拟机。我在这里使用微软提供的代码: https://learn.microsoft.com/en-us/samples/azure-samples/resource-manager-python-template-deployment/resource-manager-python-template-deployment/

但是在尝试使用模板时出现错误。

parameters = my parameters as a python dict
       
parameters = {k: {'value': v} for k, v in parameters.items()}
template = self.ts_client.template_specs.get('test-rg', 'deploy-vm.test').as_dict()

deployment_properties = {'mode': DeploymentMode.incremental,
                       'template': template,
                       'parameters': parameters}
    
self.client.deployments.create_or_update(self.resource_group,'azure-sample', {'properties': deployment_properties, 'tags': []})

与示例代码唯一不同的部分是,我不是从文件中读取模板,而是通过 sdk 获取模板并将其转换为字典,然后将 deployment_properties 作为字典传递给 begin_create_or_update 方法。如果我不这样传递它,它会给出异常:参数“Deployment.properties”不能为无。

但是我收到此错误:

azure.core.exceptions.HttpResponseError: (InvalidRequestContent) The request content was invalid and could not be deserialized: 'Could not find member 'id' on object of type 'Template'. Path 'properties.template.id', line 1, position 34.'.

知道这可能是什么吗?

【问题讨论】:

  • 该错误消息特别突出显示了缺少的必需属性。您是否已验证您的模板具有该属性?

标签: python json azure arm-template azure-sdk


【解决方案1】:

我在我的环境中尝试并得到相同类型的错误。

安慰:

确保您传递的是正确的 Arm 模板template.json并检查它是否处于正确状态。 提供有效 ID 或根据以下内容更正模板Azure 虚拟机使用此 MS-Docs 的模板。

在我使用文档验证我的模板后,使用带有 Arm 模板的 python 成功部署了虚拟机。

代码:

from  azure.mgmt.resource  import  ResourceManagementClient
from  azure.mgmt.resource.resources.models  import  DeploymentMode
from  azure.identity  import  DefaultAzureCredential
import  json


subscription_id = '<subscription id>'
resource_group = '<your rg name >'
creds = DefaultAzureCredential()
client = ResourceManagementClient( creds, '<sub id>')

parameters = {
'virtualMachineName': '',
'location': '',
'virtualMachineRG':''
}

parameters = {k: {'value': v} for  k, v  in  parameters.items()}
with  open(r'<path of file >', 'r') as  template_file_fd:
template = json.load(template_file_fd)
deployment_properties = {
'mode': DeploymentMode.incremental,
'template': template,
'parameters': parameters
}
deployment_async_operation = client.deployments.begin_create_or_update(
resource_group, 'azure-sample', {'properties': deployment_properties, 'tags': []})
deployment_async_operation.wait()

安慰:

门户网站:

参考:Microsoft.Compute/virtualMachines - Bicep, ARM template & Terraform AzAPI reference | Microsoft Learn

【讨论】:

    猜你喜欢
    • 2022-10-31
    • 2019-02-11
    • 1970-01-01
    • 1970-01-01
    • 2017-12-19
    • 2022-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多