在 Azure ARM 模板中使用条件

Using conditions in Azure ARM templates

有什么方法可以在模板中使用条件语句吗?

例如,我正在构建模板,该模板将在 QA 和 Production 上拥有带数据磁盘的虚拟机,但在 Dev 上没有数据磁盘。另一种情况是,有些扩展只需要安装在生产 VM 中,而不需要安装在其他地方。

感谢任何帮助。

实现这一点的关键属性是:

  • templateLink 设置要包含的模板和要传递给被调用模板的参数名称。

    "templateLink": {
        "uri": "[variables('sharedTemplateUrl')]",
        "contentVersion": "1.0.0.0"
    }
    
  • newOrExisting 根据其值,我们可以决定使用 QA 还是 Productoin 配置。

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

您可以查看 Azure ARM deployments: how to perform conditional deployments,其中提供了更多详细信息。

您可以利用新发布的比较功能来完成大部分工作。

下面是一个示例,说明如何使用参数来确定是否应部署存储帐户。

参数:

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

资源:

{
  "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"
}

请注意资源中的新条件 属性 以及存储提供程序的最新 API 版本。

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