共用方式為


在 Azure Resource Manager 範本的複製迴圈中使用 物件作為參數

當您在 Azure Resource Manager範本中使用物件作為參數時, (ARM 範本) ,您可以在複製迴圈中包含它們。 當這項技術與 序列複製迴圈結合時,特別適合用於部署子資源。

為了示範這種方法,讓我們看看部署 網路安全性群組的範本, (NSG) 兩個安全性規則。

首先,讓我們看看我們的參數。 當我們查看範本時,我們看到我們定義了一個名為 的參數,其中包含名為 networkSecurityGroupsSettingssecurityRules 陣列。 此陣列包含兩個 JSON 物件,每個物件都會指定定義安全性規則的設定。

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
    "parameters":{
        "networkSecurityGroupsSettings": {
            "value": {
                "securityRules": [
                    {
                        "name": "RDPAllow",
                        "description": "allow RDP connections",
                        "direction": "Inbound",
                        "priority": 100,
                        "sourceAddressPrefix": "*",
                        "destinationAddressPrefix": "10.0.0.0/24",
                        "sourcePortRange": "*",
                        "destinationPortRange": "3389",
                        "access": "Allow",
                        "protocol": "Tcp"
                    },
                    {
                        "name": "HTTPAllow",
                        "description": "allow HTTP connections",
                        "direction": "Inbound",
                        "priority": 200,
                        "sourceAddressPrefix": "*",
                        "destinationAddressPrefix": "10.0.1.0/24",
                        "sourcePortRange": "*",
                        "destinationPortRange": "80",
                        "access": "Allow",
                        "protocol": "Tcp"
                    }
                ]
            }
        }
    }
}

現在,讓我們看看我們的範本。 我們有名為 NSG1 的資源,可部署 NSG。 它也會使用 ARM 的內建屬性反復專案功能。 藉由將複製迴圈新增至範本中資源的屬性區段,您可以在部署期間動態設定屬性的項目數。 您也可以避免重複範本語法。

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "VNetSettings": {
            "type": "object"
        },
        "networkSecurityGroupsSettings": {
            "type": "object"
        }
    },
    "resources": [
        {
            "apiVersion": "2020-05-01",
            "type": "Microsoft.Network/virtualNetworks",
            "name": "[parameters('VNetSettings').name]",
            "location": "[resourceGroup().location]",
            "properties": {
                "addressSpace": {
                    "addressPrefixes": [
                        "[parameters('VNetSettings').addressPrefixes[0].addressPrefix]"
                    ]
                },
                "subnets": [
                    {
                        "name": "[parameters('VNetSettings').subnets[0].name]",
                        "properties": {
                            "addressPrefix": "[parameters('VNetSettings').subnets[0].addressPrefix]"
                        }
                    },
                    {
                        "name": "[parameters('VNetSettings').subnets[1].name]",
                        "properties": {
                            "addressPrefix": "[parameters('VNetSettings').subnets[1].addressPrefix]"
                        }
                    }
                ]
            }
        },
        {
            "apiVersion": "2020-05-01",
            "type": "Microsoft.Network/networkSecurityGroups",
            "name": "NSG1",
            "location": "[resourceGroup().location]",
            "properties": {
                "copy": [
                    {
                        "name": "securityRules",
                        "count": "[length(parameters('networkSecurityGroupsSettings').securityRules)]",
                        "input": {
                            "name": "[parameters('networkSecurityGroupsSettings').securityRules[copyIndex('securityRules')].name]",
                            "properties": {

                                "description": "[parameters('networkSecurityGroupsSettings').securityRules[copyIndex('securityRules')].description]",
                                "priority": "[parameters('networkSecurityGroupsSettings').securityRules[copyIndex('securityRules')].priority]",
                                "protocol": "[parameters('networkSecurityGroupsSettings').securityRules[copyIndex('securityRules')].protocol]",
                                "sourcePortRange": "[parameters('networkSecurityGroupsSettings').securityRules[copyIndex('securityRules')].sourcePortRange]",
                                "destinationPortRange": "[parameters('networkSecurityGroupsSettings').securityRules[copyIndex('securityRules')].destinationPortRange]",
                                "sourceAddressPrefix": "[parameters('networkSecurityGroupsSettings').securityRules[copyIndex('securityRules')].sourceAddressPrefix]",
                                "destinationAddressPrefix": "[parameters('networkSecurityGroupsSettings').securityRules[copyIndex('securityRules')].destinationAddressPrefix]",
                                "access": "[parameters('networkSecurityGroupsSettings').securityRules[copyIndex('securityRules')].access]",
                                "direction": "[parameters('networkSecurityGroupsSettings').securityRules[copyIndex('securityRules')].direction]"
                            }
                        }
                    }
                ]
            }
        }
    ]
}

讓我們來看看我們如何在 securityRules 子資源中指定我們的屬性值。 我們的所有屬性都會使用 parameters() 函式來參考。 然後,我們會使用點運算子來參考陣列,並以反覆運算 securityRules 的目前值編制索引。 最後,我們會使用另一個點運算子來參考物件的名稱。

試用範本

您可以在 GitHub 上取得範本範例。 若要部署範本,請複製報告並執行下列 Azure CLI 命令:

git clone https://github.com/mspnp/template-examples.git
cd template-examples/example3-object-param
az group create --location <location> --name <resource-group-name>
az deployment group create -g <resource-group-name> \
    --template-uri https://raw.githubusercontent.com/mspnp/template-examples/master/example3-object-param/deploy.json \
    --parameters deploy.parameters.json

下一步