Arm Template条件输出参数

时间:2017-08-31 11:06:43

标签: azure arm-template

我有一个有条件地创建资源的ARM模板:

    {
  "type": "Microsoft.Storage/storageAccounts",
  "sku": {
    "name": "Standard_GRS",
    "tier": "Standard"
  },
  "kind": "BlobStorage",
  "name": "[variables('storageAccounts_name')]",
  "condition": "[equals(parameters('is_Not_Development'), 'True')]",
  "apiVersion": "2017-06-01",
  "location": "[resourceGroup().location]",
  "scale": null,
  "properties": {
    "accessTier": "Hot"
  },
  "dependsOn": []
},

在我的输出参数中,我有以下内容,如果未创建资源,则会导致错误:

    "storageAccountConnectionString": {
  "type": "string",
  "value": "[Concat('DefaultEndpointsProtocol=https;AccountName=',variables('StorageAccounts_name'),';AccountKey=',listKeys(resourceId('Microsoft.Storage/storageAccounts', variables('StorageAccounts_name')), providers('Microsoft.Storage', 'storageAccounts').apiVersions[0]).keys[0].value)]"
},

我试过这个:

    "storageAccountConnectionString": {
  "type": "string",
  "condition": "[equals(parameters('is_Not_Development'), 'True')]",
  "value": "[Concat('DefaultEndpointsProtocol=https;AccountName=',variables('StorageAccounts_name'),';AccountKey=',listKeys(resourceId('Microsoft.Storage/storageAccounts', variables('StorageAccounts_name')), providers('Microsoft.Storage', 'storageAccounts').apiVersions[0]).keys[0].value)]"
},

使用条件子句但是无法识别。如何使输出参数成为条件?

更新:

我尝试了以下内容:

    "storageAccountConnectionString": {
  "type": "string",
  "value": "[if(equals(parameters('is_Not_Development'),'False'),'null',Concat('DefaultEndpointsProtocol=https;AccountName=',variables('StorageAccounts_name'),';AccountKey=',listKeys(resourceId('Microsoft.Storage/storageAccounts', variables('StorageAccounts_name')), providers('Microsoft.Storage', 'storageAccounts').apiVersions[0]).keys[0].value))]"
},

但它给了我相同的错误信息,它必须同时评估真假条件。

2 个答案:

答案 0 :(得分:6)

有一个解决这个问题的技巧,我们成功地使用它。

让我们看看以下模板仅在部署了相应资源时如何返回值。

"$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "appInsightsLocation": {
      "type": "string",
      "defaultValue": "",
      "allowedValues": [
        "",
        "northeurope",
        "westeurope"
      ]
    }
  },
  "variables": {
    "appInsightsName": "exampleAppInsights",
    "planName": "example-plan",
    "appInsightsEnabled": "[if(greater(length(parameters('appInsightsLocation')), 0), 'true', 'false')]",
    "appInsightsOrPlanResource": "[if(bool(variables('appInsightsEnabled')), concat('Microsoft.Insights/components/', variables('appInsightsName')), concat('Microsoft.Web/serverFarms/', variables('planName')))]",
    "appInsightsKeyOrPlanName": "[if(bool(variables('appInsightsEnabled')), 'InstrumentationKey', 'name')]"
  },
  "resources": [
    {
      "comments": "The example service plan",
      "apiVersion": "2015-08-01",
      "type": "Microsoft.Web/serverfarms",
      "location": "[resourceGroup().location]",
      "name": "[variables('planName')]",
      "sku": {
        "name": "B1",
        "capacity": 1
      },
      "properties": {
        "numberOfWorkers": 1,
        "name": "[variables('planName')]"
      }
    },
    {
      "comments": "The application insights instance",
      "apiVersion": "2014-04-01",
      "condition": "[bool(variables('appInsightsEnabled'))]",
      "type": "Microsoft.Insights/components",
      "location": "[parameters('appInsightsLocation')]",
      "name": "[variables('appInsightsName')]",
      "properties": {}
    }
  ],
  "outputs": {
    "appInsightsKey": {
      "value": "[if(bool(variables('appInsightsEnabled')), reference(variables('appInsightsOrPlanResource'))[variables('appInsightsKeyOrPlanName')], '')]",
      "type": "string"
    }
  }

模板声明了两个资源。一个应用服务计划和一个Application Insights实例。仅当location参数不为空字符串时才部署AppInsights实例。因此,只有在创建实例的情况下,才会返回此实例的检测键。

为实现这一目标,我们还需要一个始终存在的资源。在我们的例子中,这是服务计划。我们使用此资源在未部署AppInsights时获取引用。当然,这可能是任何天蓝色的资源。

诀窍发生在我们声明的两个变量appInsightsOrPlanResourceappInsightsKeyOrPlanName上。当提供appInsightsLocation时,这两个变量最终会引用从输出返回的实例的键。

另一方面,如果未提供appInsightsLocation,则这两个变量包含对未使用但有效的服务计划的有效引用。我们需要这样做,因为if函数总是评估双方。在这种情况下,输出会返回一个空字符串。

答案 1 :(得分:0)

我知道这是一个古老的问题,但是如果有人到达这里,MSFT似乎已经以两种方式解决了这个问题。

在2019年2月,他们修复了'if'评估,仅评估真实面。
https://feedback.azure.com/forums/281804-azure-resource-manager/suggestions/31538470-arm-template-if-function-should-not-evaluate-both

2019年8月,他们在输出中添加了对condition:的支持。 https://feedback.azure.com/forums/281804-azure-resource-manager/suggestions/19492006-conditional-output-from-arm-template

https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-group-authoring-templates#outputs

只要您使用的是Azure CLI版本2.0.72,您就可以访问这些更改。我刚刚在2.0.76上进行了测试,它们似乎可以正常工作。