使用 ARM 模板进行条件部署Conditional deployment in ARM templates

有时,需要选择在 Azure 资源管理器 (ARM) 模板中部署资源。Sometimes you need to optionally deploy a resource in an Azure Resource Manager (ARM) template. 使用 condition 元素指定是否部署资源。Use the condition element to specify whether the resource is deployed. 此元素的值将解析为 true 或 false。The value for this element resolves to true or false. 如果值为 true,则创建了该资源。When the value is true, the resource is created. 如果值为 false,则未创建该资源。When the value is false, the resource isn't created. 值只能应用到整个资源。The value can only be applied to the whole resource.

备注

条件部署不会级联到子资源Conditional deployment doesn't cascade to child resources. 如果要有条件地部署资源及其子资源,需要对每种资源类型应用相同的条件。If you want to conditionally deploy a resource and its child resources, you must apply the same condition to each resource type.

新资源或现有资源New or existing resource

可以使用条件部署来创建新资源或使用现有资源。You can use conditional deployment to create a new resource or use an existing one. 以下示例演示如何使用条件部署新的存储帐户或使用现有存储帐户。The following example shows how to use condition to deploy a new storage account or use an existing storage account.

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

当参数 newOrExisting 设置为 new 时,条件的计算结果为 true。When the parameter newOrExisting is set to new, the condition evaluates to true. 将部署存储帐户。The storage account is deployed. 但是,当 newOrExisting 设置为 existing 时,条件的计算结果为 false,并且不部署存储帐户。However, when newOrExisting is set to existing, the condition evaluates to false and the storage account isn't deployed.

有关使用 condition 元素的完整示例模板,请参阅具有新的或现有虚拟网络、存储和公共 IP 的 VMFor a complete example template that uses the condition element, see VM with a new or existing Virtual Network, Storage, and Public IP.

允许条件Allow condition

可以传入指示是否允许条件的参数值。You can pass in a parameter value that indicates whether a condition is allowed. 以下示例部署 SQL Server,并可选择允许 Azure IP。The following example deploys a SQL server and optionally allows Azure IPs.

{
  "type": "Microsoft.Sql/servers",
  "apiVersion": "2015-05-01-preview",
  "name": "[parameters('serverName')]",
  "location": "[parameters('location')]",
  "properties": {
    "administratorLogin": "[parameters('administratorLogin')]",
    "administratorLoginPassword": "[parameters('administratorLoginPassword')]",
    "version": "12.0"
  },
  "resources": [
    {
      "condition": "[parameters('allowAzureIPs')]",
      "type": "firewallRules",
      "apiVersion": "2015-05-01-preview",
      "name": "AllowAllWindowsAzureIps",
      "location": "[parameters('location')]",
      "dependsOn": [
        "[resourceId('Microsoft.Sql/servers/', parameters('serverName'))]"
      ],
      "properties": {
        "endIpAddress": "0.0.0.0",
        "startIpAddress": "0.0.0.0"
      }
    }
  ]
}

有关完整的模板,请参阅 Azure SQL 逻辑服务器For the complete template, see Azure SQL logical server.

运行时函数Runtime functions

如果对条件性部署的资源使用 referencelist 函数,则会对该函数进行评估,即使资源尚未部署。If you use a reference or list function with a resource that is conditionally deployed, the function is evaluated even if the resource isn't deployed. 如果该函数引用某个不存在的资源,则会出现错误。You get an error if the function refers to a resource that doesn't exist.

请使用 if 函数,以确保仅当资源已部署时,才根据条件评估函数。Use the if function to make sure the function is only evaluated for conditions when the resource is deployed. 请查看示例模板的 if 函数,该模板将 if 和 reference 用于进行条件部署的资源。See the if function for a sample template that uses if and reference with a conditionally deployed resource.

将资源设置为依赖于条件资源,这与设置任何其他资源完全一样。You set a resource as dependent on a conditional resource exactly as you would any other resource. 条件资源未部署时,Azure 资源管理器会自动将其从所需依赖项中删除。When a conditional resource isn't deployed, Azure Resource Manager automatically removes it from the required dependencies.

完整模式Complete mode

如果以完整模式部署模板并且由于条件的计算结果为 false 而未部署资源,则结果取决于用于部署模板的 REST API 版本。If you deploy a template with complete mode and a resource isn't deployed because condition evaluates to false, the result depends on which REST API version you use to deploy the template. 如果使用 2019-05-10 之前的版本,则不会删除该资源。If you use a version earlier than 2019-05-10, the resource isn't deleted. 如果使用 2019-05-10 或更高版本,则会删除该资源。With 2019-05-10 or later, the resource is deleted. 最新版本的 Azure PowerShell 和 Azure CLI 在条件为 false 时会删除该资源。The latest versions of Azure PowerShell and Azure CLI delete the resource when condition is false.

后续步骤Next steps