培训
认证
Microsoft Certified: Azure Developer Associate - Certifications
在 Microsoft Azure 中构建端到端解决方案,以创建 Azure Functions、实现和管理 Web 应用、开发使用 Azure 存储的解决方案等。
你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
在部署 Bicep 文件之前,可以预览将发生的更改。 Azure 资源管理器提供 What-if 操作,让你在部署 Bicep 文件时了解资源发生的更改。 what-if 操作不会对现有资源进行任何更改, 而是预测在部署指定的 Bicep 文件时发生的更改。
可将 what-if 操作与 Azure PowerShell、Azure CLI 或 REST API 操作配合使用。 资源组、订阅、管理组合租户级部署支持 What-if。
在 What-If 操作期间,不支持评估和扩展 templateLink
。 因此,在 What-If 操作结果中看不见使用嵌套部署中的模板链接部署的任何资源(包括模板规格引用)。
若要通过分步指导了解 what-if 操作,请参阅预览使用 what-if 进行的 Azure 部署更改。
若要部署 Bicep 文件或 ARM 模板,需要对要部署的资源具有写入权限,并且需要对 Microsoft.Resources/deployments 资源类型的所有操作具有访问权限。 例如,若要部署虚拟机,需要 Microsoft.Compute/virtualMachines/write
和 Microsoft.Resources/deployments/*
权限。 What-if 操作具有相同的权限要求。
有关角色和权限的列表,请参阅 Azure 内置角色。
What-if 将扩展嵌套模板,直到达到以下限制:
只要达到其中一个限制,剩余资源的更改类型将设置为“忽略”。
若要在 PowerShell 中使用 what-if,则必须安装 Az 模块 4.2 或更高版本。
若要安装该模块,请使用:
Install-Module -Name Az -Force
若要详细了解如何安装模块,请参阅安装 Azure PowerShell。
若要在 Azure CLI 中使用 what-if,则必须安装 Azure CLI 2.14.0 或更高版本。 如果需要,请安装 Azure CLI 的最新版本。
在 PowerShell 或 Azure CLI 中使用 what-if 时,输出包含进行了颜色编码的结果,方便你查看不同类型的更改。
文本输出如下:
Resource and property changes are indicated with these symbols:
- Delete
+ Create
~ Modify
The deployment will update the following scope:
Scope: /subscriptions/./resourceGroups/ExampleGroup
~ Microsoft.Network/virtualNetworks/vnet-001 [2018-10-01]
- tags.Owner: "Team A"
~ properties.addressSpace.addressPrefixes: [
- 0: "10.0.0.0/16"
+ 0: "10.0.0.0/15"
]
~ properties.subnets: [
- 0:
name: "subnet001"
properties.addressPrefix: "10.0.0.0/24"
]
Resource changes: 1 to modify.
备注
What-if 操作无法解析引用函数。 每次将属性设置为包含引用函数的模板表达式时,What-if 都会报告属性将改变。 发生此行为的原因是,What-if 将属性的当前值(如布尔值 true
或 false
)与未解析的模板表达式进行比较。 很明显,这些值不匹配。 部署 Bicep 文件时,只有在模板表达式解析为其他值时,属性才会更改。
若要在部署 Bicep 文件前预览更改,请使用 New-AzResourceGroupDeployment 或 New-AzSubscriptionDeployment。 将 -Whatif
开关参数添加到部署命令。
New-AzResourceGroupDeployment -Whatif
New-AzSubscriptionDeployment -Whatif
和 New-AzDeployment -Whatif
可使用 -Confirm
开关参数来预览所做的更改,让系统显示是否继续部署的提示。
New-AzResourceGroupDeployment -Confirm
New-AzSubscriptionDeployment -Confirm
和 New-AzDeployment -Confirm
上述命令返回适用于手动检查的文本摘要。 若要获取可通过编程方式检查更改的对象,请使用 Get-AzResourceGroupDeploymentWhatIfResult 或 Get-AzSubscriptionDeploymentWhatIfResult。
$results = Get-AzResourceGroupDeploymentWhatIfResult
$results = Get-AzSubscriptionDeploymentWhatIfResult
或 $results = Get-AzDeploymentWhatIfResult
若要在部署 Bicep 文件前预览更改,请使用:
可以使用 --confirm-with-what-if
开关(或其缩写形式 -c
)预览更改,并让系统显示是否继续部署的提示。 将此开关添加到:
例如,对于资源组部署,请使用 az deployment group create --confirm-with-what-if
或 -c
。
上述命令返回适用于手动检查的文本摘要。 若要获取可通过编程方式检查更改的 JSON 对象,请使用 --no-pretty-print
开关。 例如,对于资源组部署,请使用 az deployment group what-if --no-pretty-print
。
若要返回没有颜色的结果,请打开 Azure CLI 配置文件。 将“no_color”设置为“yes”。
对于 REST API,请使用:
what-if 操作列出七种不同的更改类型:
FullResourcePayloads
(默认值)时,将返回此更改类型。sku.tier
属性始终设置为在 Microsoft.ServiceBus
命名空间中匹配 sku.name
。FullResourcePayloads
(默认值)时,将返回此更改类型。ResourceIdOnly
时,才会看到此状况。控制返回的有关预测更改的详细信息级别。 可以使用两个选项:
默认值为 FullResourcePayloads。
对于 PowerShell 部署命令,请使用 -WhatIfResultFormat
参数。 在编程对象命令中,使用 ResultFormat
参数。
对于 Azure CLI,请使用 --result-format
参数。
以下结果显示了两种不同的输出格式:
完整资源有效负载
Resource and property changes are indicated with these symbols:
- Delete
+ Create
~ Modify
The deployment will update the following scope:
Scope: /subscriptions/./resourceGroups/ExampleGroup
~ Microsoft.Network/virtualNetworks/vnet-001 [2018-10-01]
- tags.Owner: "Team A"
~ properties.addressSpace.addressPrefixes: [
- 0: "10.0.0.0/16"
+ 0: "10.0.0.0/15"
]
~ properties.subnets: [
- 0:
name: "subnet001"
properties.addressPrefix: "10.0.0.0/24"
]
Resource changes: 1 to modify.
仅限资源 ID
Resource and property changes are indicated with this symbol:
! Deploy
The deployment will update the following scope:
Scope: /subscriptions/./resourceGroups/ExampleGroup
! Microsoft.Network/virtualNetworks/vnet-001
Resource changes: 1 to deploy.
为了了解 what-if 的工作原理,让我们运行一些测试。 首先,部署一个用于创建虚拟网络的 Bicep 文件。 将使用此虚拟网络来测试 what-if 如何报告更改。 下载 Bicep 文件的副本。
resource vnet 'Microsoft.Network/virtualNetworks@2023-11-01' = {
name: 'vnet-001'
location: resourceGroup().location
tags: {
CostCenter: '12345'
Owner: 'Team A'
}
properties: {
addressSpace: {
addressPrefixes: [
'10.0.0.0/16'
]
}
enableVmProtection: false
enableDdosProtection: false
subnets: [
{
name: 'subnet001'
properties: {
addressPrefix: '10.0.0.0/24'
}
}
{
name: 'subnet002'
properties: {
addressPrefix: '10.0.1.0/24'
}
}
]
}
}
若要部署 Bicep 文件,请使用:
New-AzResourceGroup `
-Name ExampleGroup `
-Location centralus
New-AzResourceGroupDeployment `
-ResourceGroupName ExampleGroup `
-TemplateFile "what-if-before.bicep"
部署完成后,即可测试 what-if 操作。 这一次,将部署一个用于更改虚拟网络的 Bicep 文件。 缺少一个原始标记,已删除了一个子网,并且已更改了地址前缀。 下载 Bicep 文件的副本。
resource vnet 'Microsoft.Network/virtualNetworks@2023-11-01' = {
name: 'vnet-001'
location: resourceGroup().location
tags: {
CostCenter: '12345'
}
properties: {
addressSpace: {
addressPrefixes: [
'10.0.0.0/15'
]
}
enableVmProtection: false
enableDdosProtection: false
subnets: [
{
name: 'subnet002'
properties: {
addressPrefix: '10.0.1.0/24'
}
}
]
}
}
若要查看更改,请使用:
New-AzResourceGroupDeployment `
-Whatif `
-ResourceGroupName ExampleGroup `
-TemplateFile "what-if-after.bicep"
what-if 输出类似于:
文本输出如下:
Resource and property changes are indicated with these symbols:
- Delete
+ Create
~ Modify
The deployment will update the following scope:
Scope: /subscriptions/./resourceGroups/ExampleGroup
~ Microsoft.Network/virtualNetworks/vnet-001 [2018-10-01]
- tags.Owner: "Team A"
+ properties.enableVmProtection: false
~ properties.addressSpace.addressPrefixes: [
- 0: "10.0.0.0/16"
+ 0: "10.0.0.0/15"
]
~ properties.subnets: [
- 0:
name: "subnet001"
properties.addressPrefix: "10.0.0.0/24"
]
Resource changes: 1 to modify.
请注意,输出顶部的颜色用于指示更改类型。
输出的底部显示了“已删除所有者”标记。 地址前缀已从 10.0.0.0/16 更改为 10.0.0.0/15。 已删除名为 subnet001 的子网。 请记住,这些更改并未部署。 如果部署 Bicep 文件,则可预览会发生的更改。
列出为已删除的某些属性实际上不会更改。 当属性不在 Bicep 文件中时,它们可能被错误地报告为已删除,但在部署过程中会自动设置为默认值。 此结果在 what-if 响应中被视为“干扰信息”。 最终部署的资源将具有为属性设置的值。 当 what-if 操作成熟时,将从结果中筛选出这些属性。
现在,让我们将命令设置为变量,以编程方式评估 what-if 结果。
$results = Get-AzResourceGroupDeploymentWhatIfResult `
-ResourceGroupName ExampleGroup `
--template-file "what-if-after.bicep"
可以看到每项更改的摘要。
foreach ($change in $results.Changes)
{
$change.Delta
}
若要在部署 Bicep 文件之前预览所做的更改,请在部署命令中使用 confirm 开关参数。 如果更改符合预期,请确认你想要完成此部署。
New-AzResourceGroupDeployment `
-ResourceGroupName ExampleGroup `
-Confirm `
-TemplateFile "what-if-after.bicep"
文本输出如下:
Resource and property changes are indicated with these symbols:
- Delete
+ Create
~ Modify
The deployment will update the following scope:
Scope: /subscriptions/./resourceGroups/ExampleGroup
~ Microsoft.Network/virtualNetworks/vnet-001 [2018-10-01]
- tags.Owner: "Team A"
+ properties.enableVmProtection: false
~ properties.addressSpace.addressPrefixes: [
- 0: "10.0.0.0/16"
+ 0: "10.0.0.0/15"
]
~ properties.subnets: [
- 0:
name: "subnet001"
properties.addressPrefix: "10.0.0.0/24"
]
Resource changes: 1 to modify.
Are you sure you want to execute the deployment?
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"):
你会看到预期的更改,并且可以确认你想要运行此部署。
如果不再需要示例资源,请使用 Azure CLI 或 Azure PowerShell 删除资源组。
az group delete --name ExampleGroup
可以通过 Azure SDK 使用 What-if 操作。
对于 Python,请使用 What-if。
对于 Java,请使用 DeploymentWhatIf 类。
对于 .NET,请使用 DeploymentWhatIf 类。
培训
认证
Microsoft Certified: Azure Developer Associate - Certifications
在 Microsoft Azure 中构建端到端解决方案,以创建 Azure Functions、实现和管理 Web 应用、开发使用 Azure 存储的解决方案等。
文档
使用 Azure CLI 部署 Bicep 文件 - Azure Resource Manager
了解如何使用 Azure 资源管理器和 Azure CLI 将资源部署到 Azure。 Bicep 文件中定义了资源。
Bicep 模块 - Azure Resource Manager
介绍如何在 Bicep 文件中定义模块,以及如何使用模块范围。
创建用于 Bicep 部署的参数文件 - Azure Resource Manager
了解如何创建 Bicep 参数文件,而不是在脚本中将参数作为内联值传递。