您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

资源管理器模板中的条件部署Conditional deployment in Resource Manager templates

有时,您需要根据需要在模板中部署资源。Sometimes you need to optionally deploy a resource in a 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.

新资源或现有资源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",
    "name": "[variables('storageAccountName')]",
    "apiVersion": "2017-06-01",
    "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设置为 "现有" 时,条件的计算结果为 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",
    "name": "[parameters('serverName')]",
    "apiVersion": "2015-05-01-preview",
    "location": "[parameters('location')]",
    "properties": {
        "administratorLogin": "[parameters('administratorLogin')]",
        "administratorLoginPassword": "[parameters('administratorLoginPassword')]",
        "version": "12.0"
    },
    "resources": [
        {
            "condition": "[parameters('allowAzureIPs')]",
            "type": "firewallRules",
            "name": "AllowAllWindowsAzureIps",
            "apiVersion": "2015-05-01-preview",
            "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

如果将引用列表函数与有条件部署的资源结合使用,则即使未部署资源,也会计算该函数。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,并使用有条件部署的资源引用。See the if function for a sample template that uses if and reference with a conditionally deployed resource.

具有完整模式的条件Condition with 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. 当条件为 false 时,Azure PowerShell 和 Azure CLI 的最新版本将删除该资源。The latest versions of Azure PowerShell and Azure CLI delete the resource when condition is false.

后续步骤Next steps