Using conditions in Azure ARM templates

空扰寡人 提交于 2020-01-12 15:51:13

问题


Is there any way to use conditional statements in templates?

for example I am building template which will have vms with data disks on QA and Production, but no data disks on Dev. Another scenario would be there are some extensions only needs to be installed in prod VMs but no where else.

Any help is appreciated.


回答1:


The key properties to achieve this are:

  • templateLink that sets the template to be included and the names of the parameters to be passed to the called template.

    "templateLink": {
        "uri": "[variables('sharedTemplateUrl')]",
        "contentVersion": "1.0.0.0"
    }
    
  • newOrExisting based on its value we can decide to use an QA versus Productoin config.

    "newOrExisting": "new",
    
    "configHash": {
      "new": "[concat(parameters('templateBaseUrl'),'partials/QA.json')]",
      "existing": "[concat(parameters('templateBaseUrl'),'partials/Production.json')]"
    }
    
    "configTemplate": "[variables('configHash')[parameters('Settings').newOrExisting]]"
    

You could see Azure ARM deployments: how to perform conditional deployments which has provided more details.




回答2:


You can leverage the newly released comparison functions to accomplish most of this.

Here is an example of how you would use a parameter to determine if a storage account should be deployed.

Parameter:

"deployStorage": {
  "type": "string"
},

Resource:

{
  "condition": "[equals(parameters('deployStorage'),'yes')]",
  "name": "[variables('storageAccountName')]",
  "type": "Microsoft.Storage/storageAccounts",
  "location": "[resourceGroup().location]",
  "apiVersion": "2017-06-01",
  "sku": {
    "name": "[parameters('storageAccountType')]"
  },
  "kind": "Storage"
}

Notice the new condition property in the resource along with the most recent API version for the storage provider.

Reference: https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-group-template-functions-comparison



来源:https://stackoverflow.com/questions/35899613/using-conditions-in-azure-arm-templates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!