2017-08-31 37 views
5

我有有条件创建资源的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)]" 
}, 

与条件条款,但这不是recogni SED。我怎样才能使输出参数有条件?

UPDATE:

我曾尝试以下:

"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))]" 
}, 

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

+1

我在Azure ARM模板中使用IF语句的经验非常糟糕。 (请参阅https://stackoverflow.com/questions/45923848/can-i-have-an-arm-template-resource-with-a-copy-array-of-0-to-n)。基本上,IF的双方(真/假)得到评估。目前没有办法解决我发现的这个问题。 –

+1

我为此添加了一个Azure UserVoice项目:https://feedback.azure.com/forums/34192--general-feedback/suggestions/31538470-arm-template-if-function-should-not-evaluate-both –

+0

Here's解决此问题的功能请求:https://feedback.azure.com/forums/281804-azure-resource-manager/suggestions/19492006-conditional-output-from-arm-template – jschmitter

回答

0

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

让我们来看看例如下面的模板只有在相应的资源已被部署时才返回一个值。

"$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函数始终评估双方。在这种情况下,输出中会返回一个空字符串。