gpt4 book ai didi

azure - Arm Template条件输出参数

转载 作者:行者123 更新时间:2023-12-04 11:47:48 24 4
gpt4 key购买 nike

我有一个 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))]"
},

但它给了我相同的错误消息,它必须同时评估 true 和 false 条件。

最佳答案

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

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

"$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 实例。仅当位置参数不为空字符串时,才会部署 AppInsights 实例。因此,仅当该实例已创建时,才会返回该实例的检测 key 。

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

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

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

关于azure - Arm Template条件输出参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45979991/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com