gpt4 book ai didi

azure - ARM 模板 - 有条件地添加到数组

转载 作者:行者123 更新时间:2023-12-03 07:09:43 24 4
gpt4 key购买 nike

{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"externalSubnet1": {
"type": "string",
"defaultValue": ""
}
},
"variables": {
"SQLServerName": "someName",
"SQLDatabaseName": "someDatabase",
"Subnet1": "/subscriptions/771adxxx-xxxx-xxxx-9xxx-xxxxxxxxxxxx/resourceGroups/some_resource_group/providers/Microsoft.Network/virtualNetworks/some_vnet/subnets/some_subnet1",
"Subnet2": "/subscriptions/771adxxx-xxxx-xxxx-9xxx-xxxxxxxxxxxx/resourceGroups/some_resource_group/providers/Microsoft.Network/virtualNetworks/some_vnet/subnets/some_subnet1",
},
"resources": [{
"name": "[variables('SQLServerName')]",
"type": "Microsoft.Sql/servers",
"location": "Central US",
"apiVersion": "2021-05-01-preview",
"dependsOn": [],
"tags": {
"displayName": "Logical SQL Server"
},
"kind": "v12.0",
"properties": {
"administratorLogin": "xyz",
"administratorLoginPassword": "xyz",
"version": "12.0"
},
"resources": [
{
"type": "Microsoft.Sql/servers/virtualNetworkRules",
"apiVersion": "2021-05-01-preview",
"name": "x1",
"dependsOn": ["[resourceId('Microsoft.Sql/servers', variables('SQLServerName'))]"],
"properties": {
"virtualNetworkSubnetId": "[parameters('externalSubnet1')]",
"ignoreMissingVnetServiceEndpoint": false
}
}, {
"type": "Microsoft.Sql/servers/virtualNetworkRules",
"apiVersion": "2021-05-01-preview",
"name": "x2",
"dependsOn": ["[resourceId('Microsoft.Sql/servers', variables('SQLServerName'))]"],
"properties": {
"virtualNetworkSubnetId": "[variables('Subnet1')]",
"ignoreMissingVnetServiceEndpoint": false
}
}, {
"type": "Microsoft.Sql/servers/virtualNetworkRules",
"apiVersion": "2021-05-01-preview",
"name": "x3",
"dependsOn": ["[resourceId('Microsoft.Sql/servers', variables('SQLServerName'))]"],
"properties": {
"virtualNetworkSubnetId": "[variables('Subnet2')]",
"ignoreMissingVnetServiceEndpoint": false
}
}
]
}
]
}

我有以下 ARM 模板。最里面的资源数组有3条虚拟网络规则。我想要的是,如果参数 externalSubnet1 的值存在,即它是非空字符串,那么我希望包含资源数组的第一个元素。这是有道理的,因为变量 externalSubnet1 需要是有效的子网 ID(类似于变量 Subnet1Subnet2),否则部署将失败。

我尝试了什么?

我查看了condition但它处于资源级别,因此无法使用它。

我还查看了if但问题是它仍然在数组中包含带有空白 virtualNetworkSubnetId 的 JSON block ,并且也会失败。我希望排除 virtualNetworkRules 资源数组的整个第一个元素(JSON block ),即当参数 externalSubnet1 为空字符串时,数组中应该只有 2 个元素。

那么,我们如何才能实现这一目标呢?谢谢。

最佳答案

如果我理解正确,那么您希望根据字符串参数 externalsubnet1 有条件地执行VirtualNetworkRule/strong> 。如果参数值存在,则将执行x1,如果值为空,则执行x2x3 将被执行。在这种情况下,您可以使用以下模板:

模板:

{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"externalSubnet1": {
"type": "string"
}
},
"variables": {
"SQLServerName": "ansserver",
"Subnet1": "/subscriptions/xxxxxxxxxxxxxxxxxxxxxxxxx/resourceGroups/ansumantest/providers/Microsoft.Network/virtualNetworks/ansuman-vnet/subnets/default",
"Subnet2": "/subscriptions/xxxxxxxxxxxxxxxxxxxx/resourceGroups/ansumantest/providers/Microsoft.Network/virtualNetworks/ansuman-vnet/subnets/subnet1"
},
"resources": [{
"name": "[variables('SQLServerName')]",
"type": "Microsoft.Sql/servers",
"location": "East US",
"apiVersion": "2015-05-01-preview",
"properties": {
"administratorLogin": "ansuman",
"administratorLoginPassword": "password",
"version": "12.0",
"publicNetworkAccess":"Enabled"
},
"resources": [
{
"condition":"[not(empty(parameters('externalSubnet1')))]",
"type": "virtualNetworkRules",
"apiVersion": "2021-05-01-preview",
"name": "[concat(variables('SQLServerName'),'test1')]",
"dependsOn": ["[resourceId('Microsoft.Sql/servers', variables('SQLServerName'))]"],
"properties": {
"virtualNetworkSubnetId": "[parameters('externalSubnet1')]",
"ignoreMissingVnetServiceEndpoint": false
}
}, {
"condition":"[empty(parameters('externalSubnet1'))]",
"type": "virtualNetworkRules",
"apiVersion": "2021-05-01-preview",
"name": "[concat(variables('SQLServerName'),'test2')]",
"dependsOn": ["[resourceId('Microsoft.Sql/servers', variables('SQLServerName'))]"],
"properties": {
"virtualNetworkSubnetId": "[variables('Subnet1')]",
"ignoreMissingVnetServiceEndpoint": false
}
}, {
"condition":"[empty(parameters('externalSubnet1'))]",
"type": "virtualNetworkRules",
"apiVersion": "2021-05-01-preview",
"name": "[concat(variables('SQLServerName'),'test3')]",
"dependsOn": ["[resourceId('Microsoft.Sql/servers', variables('SQLServerName'))]"],
"properties": {
"virtualNetworkSubnetId": "[variables('Subnet2')]",
"ignoreMissingVnetServiceEndpoint": false
}
}
]
}
]
}

输出:

enter image description here

enter image description here

关于azure - ARM 模板 - 有条件地添加到数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70664461/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com