如何在Azure资源pipe理器模板中始终如一地输出公共IP地址?

我有一个Azure模板,除其他外,定义一个公共IP地址。 我想在模板的输出中列出该IP地址,但在第一次部署之后,我收到一条错误消息:

Message: Unable to evaluate template outputs: 'builderConnectionInformation'. Please see error details and deployment operations. Please see https://aka.ms/arm-debug for usage details. Exception Details: Error Code: DeploymentOutputEvaluationFailed Message: The template output 'builderConnectionInformation' is not valid: The language expression property 'ipAddress' doesn't exist, available properties are 'provisioningState, resourceGuid, publicIPAllocationMethod, idleTimeoutInMinutes'.. Target: builderConnectionInformation 

但是,如果我重新部署,它会显示我想要的输出:

 Deployment completed. Outputs: - builderConnectionInformation = {'Username': 'ExampleAdministrator', 'Password': '<PASSWORD>', 'Public IP Address': '1.2.3.4'} 

部署细节

我正在使用Python脚本并调用Azure Python SDK进行部署。 整个脚本在这里 ; 相关位是:

  result = resource.resource_groups.create_or_update( parsed.group_name, {'location': parsed.group_location}) template_params = { 'storageAccountName': parsed.storage_account_name, 'builderVmAdminPassword': parsed.builder_vm_admin_password, 'builderVmSize': parsed.builder_vm_size} template_params = {k: {'value': v} for k, v in template_params.items()} deploy_params = { 'mode': 'incremental', 'template': template, 'parameters': template_params} async_operation = resource.deployments.create_or_update( parsed.group_name, parsed.deployment_name, deploy_params) result = async_operation.result() msg = "Deployment completed. Outputs:" for k, v in result.properties.outputs.items(): msg += f"\n- {k} = {str(v['value'])}" print(msg) 

我的模板的表示可以在这里find。 (注意:我实际上使用的是YAML模板 ,但是在使用之前,它被转换为一个Python对象,与JSON的使用方式相同,否则它将起作用,所以我不认为这是问题所在)。转换的JSON模板,因为这是每个人都会熟悉的)是:

 { "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#", "contentVersion": "1.0.0.0", ... "parameters": { "builderVmAdminPassword": { "type": "securestring" }, ... }, "variables": { "apiVersion": "2015-06-15", "builderVmAdminUsername": "WinTrialAdmin", "pubIpAddrName": "builderPubIp", "pubIpAddrType": "Dynamic", ... }, "resources": [ ... { "type": "Microsoft.Network/publicIPAddresses", "name": "[variables('pubIpAddrName')]", "apiVersion": "[variables('apiVersion')]", "location": "[resourceGroup().location]", "properties": { "publicIPAllocationMethod": "[variables('pubIpAddrType')]" } }, .... ], "outputs": { "builderConnectionInformation": { "type": "object", "value": { "Username": "[variables('builderVmAdminUsername')]", "Password": "[parameters('builderVmAdminPassword')]", "Public IP Address": "[reference(variables('pubIpAddrName')).ipAddress]" } } } 

我试过了

我想知道是否公共IP在模板部署返回时还没有完成分配,但是如果发生这种情况,我没有办法解决这个问题 – 唯一发生在我身上的是使用dependsOn输出的属性,但输出不支持dependsOn属性。

我也发现一些信息说,而不是

 "[reference(variables('pubIpAddrName')).ipAddress]" 

我应该使用:

 "[reference(variables('pubIpAddrName')).properties.ipAddress]" 

但是,无论是第一次部署,还是其后的时间,都会得出类似的结果。 以下是初始部署的确切消息 – 请注意,它没有列出ipAddress作为可用的属性:

 Message: Unable to evaluate template outputs: 'builderConnectionInformation'. Please see error details and deployment operations. Please see https://aka.ms/arm-debug for usage details. Exception Details: Error Code: DeploymentOutputEvaluationFailed Message: The template output 'builderConnectionInformation' is not valid: The language expression property 'properties' doesn't exist, available properties are 'provisioningState, resourceGuid, publicIPAllocationMethod, idleTimeoutInMinutes'.. Target: builderConnectionInformation 

以下是后续部署的确切消息 – 请注意,它列出了一个ipAddress属性:

 Message: Unable to evaluate template outputs: 'builderConnectionInformation'. Please see error details and deployment operations. Please see https://aka.ms/arm-debug for usage details. Exception Details: Error Code: DeploymentOutputEvaluationFailed Message: The template output 'builderConnectionInformation' is not valid: The language expression property 'properties' doesn't exist, available properties are 'provisioningState, resourceGuid, ipAddress, publicIPAllocationMethod, idleTimeoutInMinutes, ipConfiguration'.. Target: builderConnectionInformation 

概要

如何确保始终返回公用IP地址,即使在第一次部署时也是如此?

我在我的实验室进行了testing,得到和你一样的结果。 根据这个链接和GitHub上的这个链接 。 这是平台中已知的一个限制,dynamic公共IP地址在VM启动并运行之前不会自行parsing。 解决方法有两个选项:

  1. 在静态模式下创build公共IP地址。 这将确保公共IP地址被立即分配。 但是请注意,您可能会收取额外的费用。

  2. 将公有IP地址的依赖关系更改为IP地址所连接的虚拟机。 这将确保公共IP地址始终可用。

我build议你使用一个静态的公共IP地址,我在我的实验室testing过,这对我很有用。

  "pubIpAddrType": "Static", 

Dynamic更改为Static