gpt4 book ai didi

json - 如何使用条件逻辑将完全限定的 Azure 资源 ID 作为原始 json 传递?

转载 作者:行者123 更新时间:2023-12-03 00:06:29 25 4
gpt4 key购买 nike

我正在尝试有条件地将网络安全组和路由表的完全限定资源 ID 传递到 vnet 中的子网。目前,这些是使用虚拟网络资源上的arm属性迭代进行部署的。

我是按照这篇文章才达到这一点的。 https://github.com/MicrosoftDocs/azure-docs/issues/29115

我成功地能够根据填充的属性值将路由表和 nsgs 附加到子网。但是,我无法使用变量和参数构建完全限定的资源 ID 来有条件地部署对象。

我尝试过使用Azure ARM函数,例如subscription()和resource()。但是,每当我使用这些函数连接 FQDN 字符串时,我都会收到有关格式错误的 JSON 的错误。根据上面的文章,MSFT 支持人员表示这需要通过完全限定的方式传递,但我无法想出一种方法来执行此操作并通过使用 json() 函数进行转换。我测试了传递资源 Id 的短名称,ARM API 还报告我需要将其作为完全限定名称传递

我真的不希望这些硬编码在我的模板顶部,因为在创建这些对象之前我不知道资源 ID 是什么。另外,这显然也是不好的做法,违背了可重用模板的目的。

{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"location": {
"type": "string",
"allowedValues": ["australiaeast", "australiasoutheast"],
"defaultValue": "australiasoutheast",
"metadata": {
"description": "Deployment location"
}
},
"routeTables": {
"type": "array",
"defaultValue": [
"TrustedSubnets",
"UntrustedSubnets"
],
"metadata": {
"description": "Array of interconnect route table names, (e.g. GatewaySubnet / Internal / Onprem)"
}
},
"subnets": {
"type": "array",
"defaultValue": [
{
"Name": "Management",
"Address": "10.118.124.0/24",
"Nsg": "/sub/1234/resourceGroups/azresgroup/providers/Microsoft.Network/networkSecurityGroups/ManagementSn-ase-nsg",
"routeTable": "/sub/1234/resourceGroups/azresgroup/providers/Microsoft.Network/routeTables/TrustedSubnets-ase-rt"
},
{
"Name": "Trusted",
"Address": "10.118.125.0/24",
"Nsg": "/sub/1234/resourceGroups/azresgroup/providers/Microsoft.Network/networkSecurityGroups/TrustedSn-ase-nsg",
"routeTable": "/sub/1234/resourceGroups/azresgroup/providers/Microsoft.Network/routeTables/TrustedSubnets-ase-rt"
},
{
"Name": "Untrusted",
"Address": "10.118.126.0/24",
"Nsg": "/sub/1234/resourceGroups/azresgroup/providers/Microsoft.Network/networkSecurityGroups/UntrustedSn-ase-nsg",
"routeTable": "/sub/1234/resourceGroups/azresgroup/providers/Microsoft.Network/routeTables/UntrustedSubnets-ase-rt"
},
{
"Name": "GatewaySubnet",
"Address": "10.118.127.0/24",
"Nsg": "",
"routeTable": ""
}
],
"metadata": {
"description": "Subnet properties to be deployed per region. Each entry must contain a Name, Address, Nsg and routeTable key. Route tables and NSG's must be fully qualified"
}
},
"vnetName": {
"type": "string",
"defaultValue": "hubvnet",
"metadata": {
"description": "Virtual network name"
}
},
"vnetAddressPrefix": {
"type": "string",
"defaultValue": "10.118.124.0/22",
"metadata": {
"description": "Address prefix"
}
}
},
"variables": {
"alertsDistributionList": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ddb9b182bca7a8afb8abb3b8a9bcb1b8afa9ae9db8b0bcb4b1f3beb2b0f3bca8" rel="noreferrer noopener nofollow">[email protected]</a>",
"HubNetAgResourceId": "[resourceId('microsoft.insights/actionGroups', concat(parameters('vnetName'), '-ag'))]",
"vnetResourceId": "[resourceId('Microsoft.Network/virtualNetworks', concat(parameters('vnetName')))]"
},
"resources": [
{
"type": "Microsoft.Network/networkSecurityGroups",
"apiVersion": "2019-04-01",
"name": "[if(contains(parameters('location'), 'australiasoutheast'), concat(parameters('subnets')[copyIndex()].Name, 'Sn', '-ase-nsg'), concat(parameters('subnets')[copyIndex()].Name, 'Sn', '-ae-nsg'))]",
"location": "[parameters('location')]",
"copy": {
"name": "NsgCopy",
"count": 3,
"mode": "Serial",
"batchSize": 1
},
"properties": {
"securityRules": []
},
"dependsOn": []
},
{
"apiVersion": "2019-04-01",
"name": "[if(contains(parameters('location'), 'australiasoutheast'), concat(parameters('routeTables')[copyIndex()], '-ase-rt'), concat(parameters('routeTables')[copyIndex()], '-ae-rt'))]",
"type": "Microsoft.Network/routeTables",
"location": "[resourceGroup().location]",
"copy": {
"name": "RtCopy",
"count": "[length(parameters('routeTables'))]"
},
"properties": {
"routes": [],
"disableBgpRoutePropagation": true
}
},
{
"type": "Microsoft.Network/virtualNetworks",
"name": "[parameters('vnetName')]",
"apiVersion": "2019-04-01",
"location": "[resourceGroup().location]",
"properties": {
"addressSpace": {
"addressPrefixes": [
"[parameters('vnetAddressPrefix')]"
]
},
"dhcpOptions": {
"dnsServers": []
},
"virtualNetworkPeerings": [],
"copy": [{
"name": "subnets",
"count": "[length(parameters('subnets'))]",
"input": {
"name": "[parameters('subnets')[copyIndex('subnets')].Name]",
"properties": {
"addressPrefix": "[parameters('subnets')[copyIndex('subnets')].Address]",
"networkSecurityGroup": "[if(not(empty(parameters('subnets')[copyIndex('subnets')].Nsg)), json(concat('{\"id\": \"', parameters('subnets')[copyIndex('subnets')].Nsg, '\"}')), json('null'))]",
"routeTable": "[if(not(empty(parameters('subnets')[copyIndex('subnets')].routeTable)), json(concat('{\"id\": \"', parameters('subnets')[copyIndex('subnets')].routeTable, '\"}')), json('null'))]"
}
}
}]
},
"dependsOn": [
"NsgCopy",
"RtCopy"
]
}
]
}

我希望避免在声明子网数组的参数属性中对资源 ID 进行硬编码。我希望使用智能逻辑生成这些,然后在底部的子网循环中转换并作为原始 json 传递

我在模板顶部混淆了我的子 ID。如果您想测试我的模板,请替换您的值

非常感谢任何帮助。 :)

最佳答案

我可能会这样做:

"variables": {
"copy": [
{
"name": "routeTables",
"count": "[length(parameters('subnets'))]",
"input": {
"id": "[parameters('subnets')[copyIndex('routeTables')].routeTable]"
}
}
]
},

"properties": {
...
"routeTable": "[if(not(empty(parameters('subnets')[copyIndex('subnets')].routeTable)), variables('routeTables')[copyIndex('subnets')], json('null'))]"
}

关于json - 如何使用条件逻辑将完全限定的 Azure 资源 ID 作为原始 json 传递?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56787940/

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