ARM 模板 - 有条件地添加到数组
ARM Template - Conditionally add to an Array
{
"$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(类似于变量 Subnet1
或 Subnet2
),否则部署将失败。
我尝试了什么?
我查看了 condition,但它处于资源级别,因此无法使用。
我也查看了 if,但问题是它仍然包含数组中的 JSON 块和一个空白的 virtualNetworkSubnetId,这也失败了。我希望排除 virtualNetworkRules 资源数组的整个第一个元素(JSON 块),即当参数 externalSubnet1
为空字符串时,数组中应该只有 2 个元素。
那么,我们怎样才能做到这一点呢?谢谢。
如果我理解正确那么你想根据有条件地执行VirtualNetworkRule
字符串参数 externalsubnet1 。如果存在参数 value 则 x1
将被执行 如果 value 为空则x2
和x3
将被执行。在这种情况下,您可以使用以下模板:
模板:
{
"$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
}
}
]
}
]
}
输出:
{
"$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(类似于变量 Subnet1
或 Subnet2
),否则部署将失败。
我尝试了什么?
我查看了 condition,但它处于资源级别,因此无法使用。
我也查看了 if,但问题是它仍然包含数组中的 JSON 块和一个空白的 virtualNetworkSubnetId,这也失败了。我希望排除 virtualNetworkRules 资源数组的整个第一个元素(JSON 块),即当参数 externalSubnet1
为空字符串时,数组中应该只有 2 个元素。
那么,我们怎样才能做到这一点呢?谢谢。
如果我理解正确那么你想根据有条件地执行VirtualNetworkRule
字符串参数 externalsubnet1 。如果存在参数 value 则 x1
将被执行 如果 value 为空则x2
和x3
将被执行。在这种情况下,您可以使用以下模板:
模板:
{
"$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
}
}
]
}
]
}
输出: