Mar*_*Bez 5 azure azure-resource-manager azure-bicep
我正在尝试创建一个二头肌模板来根据条件部署具有 1 个或 2 个 NIC 的虚拟机。
有人知道是否有办法在属性定义中使用条件语句来部署虚拟机网卡?似乎资源定义中不允许使用 if 函数,并且由于 ID 无效而导致三元错误。
只是试图使用 resources = if (bool) {} 避免出现 2 个重复的虚拟机资源定义
networkProfile: {
networkInterfaces: [
{
id: nic_wan.id
properties: {
primary: true
}
}
{
id: bool ? nic_lan.id : '' #Trying to deploy this as a conditional if bool = true.
properties: {
primary: false
}
}
]
}
Run Code Online (Sandbox Code Playgroud)
上面的代码会出错,因为一旦定义了 NIC,它就需要一个有效的 ID。
“properties.networkProfile.networkInterfaces[1].id”无效。期望以“/subscriptions/{subscriptionId}”或“/providers/{resourceProviderNamespace}/”开头的完全限定资源 ID。(代码:LinkedInvalidPropertyId)
Tho*_*mas 15
您可以创建一些变量来处理该问题:
// Define the default nic
var defaultNic = [
{
id: nic_wan.id
properties: {
primary: true
}
}
]
// Add second nic if required
var nics = concat(defaultNic, bool ? [
{
id: nic_lan.id
properties: {
primary: false
}
}
] : [])
// Deploy the VM
resource vm 'Microsoft.Compute/virtualMachines@2020-12-01' = {
...
properties: {
...
networkProfile: {
networkInterfaces: nics
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3875 次 |
| 最近记录: |