2017-08-31 62 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函數始終評估雙方。在這種情況下,輸出中會返回一個空字符串。