Azure Application Gateway ARM模板问题与backendHttpSettingsCollection中的pickHostNameFromBackendAddress和主机名

问题描述

在配置backendHttpSettingsCollection时,ARM模板出现问题。我不能同时为两个条件(true,false)使用pickHostNameFromBackendAddress,如果我们选择True,那么它会为主机名抛出错误,我尝试应用json('null'),但仍然抛出相同的错误

有什么方法可以在同一ARM模板中实现这两种条件。

模板:

MAINFRAGMENT

参数:

{
        "name": "backendHttpSettingsCollection","count": "[length(parameters('backendHttpSettings'))]","input": {
          "name": "[parameters('backendHttpSettings')[copyIndex('backendHttpSettingsCollection')].name]","properties": {
            "port": 443,"pickHostNameFromBackendAddress": "[parameters('backendHttpSettings')[copyIndex('backendHttpSettingsCollection')].pickHostNameFromBackendAddress]","hostName": "[if(parameters('backendHttpSettings')[copyIndex('backendHttpSettingsCollection')].pickHostNameFromBackendAddress,json('null'),parameters('backendHttpSettings')[copyIndex('backendHttpSettingsCollection')].hostName)]","protocol": "Https","probeEnabled": "[parameters('backendHttpSettings')[copyIndex('backendHttpSettingsCollection')].probeEnabled]","probe": {
              "id": "[resourceId('Microsoft.Network/applicationGateways/probes',parameters('applicationGatewayName'),parameters('backendHttpSettings')[copyIndex('backendHttpSettingsCollection')].probe)]"
            }
          }
        }
      },

谢谢

解决方法

我可以重现相同的错误。即使将空值或空白值分配给hostName,也无法通过模板验证。

作为一种解决方法,您可以尝试将所有属性包装在参数对象中。为正确和错误方案定义两个对象参数。参见以下示例:

 "parameters": {
               "pick": {
                  "type": "bool","defaultValue": true
               },"host": {
                  "type": "string","defaultValue": "leviCustom.com"
               },"pickFalse":{
                "type": "object","defaultValue":{
                    "port": 443,"protocol": "Https","cookieBasedAffinity": "Disabled","hostName": "[parameters('host')]","pickHostNameFromBackendAddress": false,"requestTimeout": 20
                }
            },"pickTrue":{
                "type": "object","pickHostNameFromBackendAddress": true,"requestTimeout": 20
                }
            }
        },

"backendHttpSettingsCollection": [
                            {
                                "name": "LeviHttpSetting","properties": "[if(parameters('pick'),parameters('pickTrue'),parameters('pickFalse'))]"
                            }
,

将属性作为变量,如下所示:

"copy": [
  {
    "name": "backendHttpSettingsCollectionWithHost","count": "[length(parameters('backendHttpSettings'))]","input": {
      "name": "[parameters('backendHttpSettings')[copyIndex('backendHttpSettingsCollectionWithHost')].name]","properties": {
        "port": 443,"hostName": "[parameters('backendHttpSettings')[copyIndex('backendHttpSettingsCollectionWithHost')].hostname]","probeEnabled": "[parameters('backendHttpSettings')[copyIndex('backendHttpSettingsCollectionWithHost')].probeEnabled]","probe": {
          "id": "[resourceId('Microsoft.Network/applicationGateways/probes',parameters('applicationGatewayName'),parameters('backendHttpSettings')[copyIndex('backendHttpSettingsCollectionWithHost')].probe)]"
        }
    }
  },{
    "name": "backendHttpSettingsCollectionWithoutHost","input": {
      "name": "[parameters('backendHttpSettings')[copyIndex('backendHttpSettingsCollectionWithoutHost')].name]","pickHostNameFromBackendAddress": "[parameters('backendHttpSettings')[copyIndex('backendHttpSettingsCollectionWithoutHost')].pickHostNameFromBackendAddress]","probeEnabled": "[parameters('backendHttpSettings')[copyIndex('backendHttpSettingsCollectionWithoutHost')].probeEnabled]",parameters('backendHttpSettings')[copyIndex('backendHttpSettingsCollectionWithoutHost')].probe)]"
        }
      }
    }
  }

并在以下应用程序网关属性中使用:

"backendHttpSettingsCollection": "[if(variables('pickHostNameFromBackendAddress')[0],variables('backendHttpSettingsCollectionWithoutHost'),variables('backendHttpSettingsCollectionWithHost'))]",

这在两种情况下都有效