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

如何在 Azure 资源管理器模板中一致地输出公共 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)

您可以找到我的模板的图示这里。(注:我实际上使用的是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 地址?

答案1

我在实验室里测试过,得到了和你一样的结果。根据这个关联和这个关联在 GitHub 上。这是平台的一个已知限制,动态公共 IP 地址在 VM 启动并运行之前无法自行解析。有两种解决方法:

  1. 以静态模式创建公共 IP 地址。这将确保立即分配公共 IP 地址。但是,请注意,您可能需要支付额外费用。

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

我建议您使用静态公共 IP,我在我的实验室中测试过,并且它对我有用。

 "pubIpAddrType": "Static",

将值从 更改DynamicStatic

相关内容