使用Azure ARM模板中的条件

nit*_*inb 8 azure azure-resource-manager

有没有办法在模板中使用条件语句?

例如,我正在构建模板,该模板将在QA和Production上具有包含数据磁盘的vms,但在Dev上没有数据磁盘.另一种情况是,只有一些扩展只需要安装在prod虚拟机中,但不能安装在其他地方.

任何帮助表示赞赏.

dba*_*kol 10

您可以利用新发布的比较函数来完成大部分工作.

以下是如何使用参数确定是否应部署存储帐户的示例.

参数:

"deployStorage": {
  "type": "string"
},
Run Code Online (Sandbox Code Playgroud)

资源:

{
  "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"
}
Run Code Online (Sandbox Code Playgroud)

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

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


Ste*_*ven 4

实现这一目标的关键属性是:

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

    "templateLink": {
        "uri": "[variables('sharedTemplateUrl')]",
        "contentVersion": "1.0.0.0"
    }
    
    Run Code Online (Sandbox Code Playgroud)
  • 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]]"
    
    Run Code Online (Sandbox Code Playgroud)

您可以查看Azure ARM 部署:如何执行条件部署,其中提供了更多详细信息。

  • 我找到了另一种使用子模板的方法,如以下密码身份验证类型示例所示。https://github.com/Azure/azure-quickstart-templates/blob/master/checkpoint-single-nic/azuredeploy.json#L11 (2认同)
  • 更新:看起来有更好的方法:条件元素:https://learn.microsoft.com/en-us/azure/azure-resource-manager/resource-group-authoring-templates (2认同)