joh*_*ley 7 azure azure-rm-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": []
},
Run Code Online (Sandbox Code Playgroud)
在我的输出参数中,我有以下内容,如果未创建资源,则会导致错误:
"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)]"
},
Run Code Online (Sandbox Code Playgroud)
我试过这个:
"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)]"
},
Run Code Online (Sandbox Code Playgroud)
条款条款,但不承认.如何使输出参数有条件?
更新:
我尝试过以下方法:
"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))]"
},
Run Code Online (Sandbox Code Playgroud)
但它给了我相同的错误信息,它必须评估真假条件.
有一个技巧可以解决这个问题,我们成功地使用它.
让我们看一下,例如,如果已部署相应的资源,以下模板如何返回值.
"$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"
}
}
Run Code Online (Sandbox Code Playgroud)
模板声明了两个资源.一个应用服务计划和一个Application Insights实例.仅当location参数不为空字符串时才部署AppInsights实例.因此,只有在创建实例时,才会返回此实例的检测项.
为实现这一目标,我们还需要一个始终存在的资源.在我们的例子中,这是服务计划.我们使用此资源在未部署AppInsights时获取引用.当然,这可能是任何天蓝色的资源.
诀窍发生在两个变量上appInsightsOrPlanResource,appInsightsKeyOrPlanName我们声明.当appInsightsLocation设置然后这两个变量最终引用其从输出返回的实例的关键.
appInsightsLocation另一方面,当未提供时,这两个变量包含对未使用但有效的服务计划的有效引用.我们需要这样做,因为if函数总是评估双方.但是,在这种情况下,输出会返回一个空字符串.
| 归档时间: |
|
| 查看次数: |
1541 次 |
| 最近记录: |