ARM 模板,包含不同资源组中的资源列表

问题描述 投票:0回答:1

我必须更新一些现有的 ARM 模板,以将一对资源(NSG、VNet)部署到多个资源组,其中资源组作为数组传递到模板。

问:如何使 VNet 依赖于同一资源组中的 NSG?

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",

  "parameters": {
    "listOfRGs": {
      "type": "array",
      "defaultValue": ["rg1", "rg2",..."rgN"],
    }
  },
  "resources": [
    "copy": [
      {
        "name": "rgNsgCopyIdx",
        "count": "[length(parameters('listOfRGs'))]",
        "input": {
           "type": "Microsoft.Network/networkSecurityGroups",
           "name": "myNSG",
           "resourceGroup": "[parameters('listOfRGs')[copyIndex('rgNsgCopyIdx')]]",
           .... and some other properties
        }
      },
      {
        "name": "rgVNetCopyIdx",
        "count": "[length(parameters('listOfRGs'))]",
        "input": {
           "type": "Microsoft.Network/virtualNetworks",
           "name": "myVNET",
           "resourceGroup": "[parameters('listOfRGs')[copyIndex('rgVNetCopyIdx')]]",
           "dependsOn": [
             "<WHAT GOES HERE TO DEPEND ON THE NSG IN THE SAME RESOURCE GROUP>"
           ]
        }
      },
    ]
  ]
}
azure azure-resource-manager azure-rm-template
1个回答
0
投票

我认为你需要使用

Microsoft.Resources/deployments
copy
。假设您有三个资源组。

  1. rg中心
  2. rg1
  3. rg2

rgCenter
用于运行部署,它将调用
rg1
rg2

中的两个部署

deploy.ps1

$rgName = "rgCenter"

New-AzResourceGroupDeployment -Name "testDeployment" -ResourceGroupName $rgName -TemplateFile "template.json"

template.json

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "location": {
            "type": "string",
            "defaultValue": "[resourceGroup().location]"
        },
        "listOfRGs": {
            "type": "array",
            "defaultValue": ["rg1", "rg2"]
        }
    },
    "variables": {
    },
    "resources": [
        {
            "copy": {
                "name": "rgcopy",
                "count": "[length(parameters('listOfRGs'))]"
            },
            "type": "Microsoft.Resources/deployments",
            "apiVersion": "2021-04-01",
            "name": "[format('{0}deployment', parameters('listOfRGs')[copyIndex()])]",
            "resourceGroup": "[parameters('listOfRGs')[copyIndex()]]",
            "properties": {
                "mode": "Incremental",
                "template": {
                    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
                    "contentVersion": "1.0.0.0",
                    "parameters": {},
                    "variables": {},
                    "resources": [
                        {
                            "type": "Microsoft.Network/networkSecurityGroups",
                            "apiVersion": "2023-02-01",
                            "name": "nsg1",
                            "location": "[parameters('location')]",
                            "properties": {
                            }
                        },
                        {
                            "type": "Microsoft.Network/virtualNetworks",
                            "apiVersion": "2021-08-01",
                            "name": "vnet1",
                            "location": "[parameters('location')]",
                            "properties": {
                                "addressSpace": {
                                    "addressPrefixes": [
                                        "10.0.0.0/16"
                                    ]
                                },
                                "subnets": [
                                    {
                                        "name": "subnet1",
                                        "properties": {
                                            "addressPrefix": "10.0.0.0/24",
                                            "networkSecurityGroup": {
                                                 "id": "[resourceId(parameters('listOfRGs')[copyIndex()] ,'Microsoft.Network/networkSecurityGroups', 'nsg1')]"
                                            }
                                        }
                                    }
                                ]
                            }
                        }
                    ]
                },
                "parameters": {}
            }
        }
    ]
}

部署历史记录位于

rgCenter
enter image description here

资源已部署

rg1
enter image description here

资源已部署

rg2
enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.