0

我想使用具有不同 NSG 的单个模板 json 文件创建 2 个 Azure VM。

vm-template.json

  "resources": [
    {
        "name": "[parameters('vmName')]",
        "type": "Microsoft.Compute/virtualMachines",
        "apiVersion": "[variables('computeApiVersion')]",
        "location": "[variables('location')]",
        "tags": {
            "Created By": "PAMC"
        },
        "dependsOn": [
            "[concat('Microsoft.Network/networkInterfaces/', parameters('networkInterfaceName'))]"
        ],
        "properties": {
            "osProfile": {
                "computerName": "[parameters('vmName')]",
                "adminUsername": "[parameters('vmUsername')]",
                "adminPassword": "[parameters('vmPassword')]"
            },
            "hardwareProfile": {
                "vmSize": "[parameters('vmSize')]"
            },
            "storageProfile": {
                "imageReference": {
                    "publisher": "[variables('imagePublisher')]",
                    "offer": "[variables('imageOffer')]",
                    "sku": "[variables('imageSku')]",
                    "version": "latest"
                }
            },
            "networkProfile": {
                "networkInterfaces": [
                    {
                        "id": "[resourceId('Microsoft.Network/networkInterfaces', parameters('networkInterfaceName'))]"
                    }
                ]
            }
        }
    },
    {
        "name": "[parameters('networkInterfaceName')]",
        "type": "Microsoft.Network/networkInterfaces",
        "apiVersion": "[variables('networkApiVersion')]",
        "location": "[variables('location')]",
        "dependsOn": [
            "[concat('Microsoft.Network/publicIpAddresses/', parameters('publicIPAddressName'))]",
            "[concat('Microsoft.Network/networkSecurityGroups/', parameters('networkSecurityGroupName'))]"
        ],
        "properties": {
            "ipConfigurations": [
                {
                    "name": "ipconfig1",
                    "properties": {
                        "subnet": {
                            "id": "[variables('subnetRef')]"
                        },
                        "privateIPAllocationMethod": "Dynamic",
                        "publicIpAddress": {
                            "id": "[resourceId(variables('resourceGroupName'),'Microsoft.Network/publicIpAddresses', parameters('publicIPAddressName'))]"
                        }
                    }
                }
            ],
            "networkSecurityGroup": {
                "id": "[resourceId(variables('resourceGroupName'), 'Microsoft.Network/networkSecurityGroups', parameters('networkSecurityGroupName'))]"
            }
        }
    },
    {
        "name": "[parameters('publicIPAddressName')]",
        "type": "Microsoft.Network/publicIpAddresses",
        "apiVersion": "[variables('networkApiVersion')]",
        "location": "[variables('location')]",
        "properties": {
            "publicIpAllocationMethod": "[variables('publicIPAddressType')]"
        }
    },
    {
        "name": "nsg1",
        "type": "Microsoft.Network/networkSecurityGroups",
        "apiVersion": "[variables('networkApiVersion')]",
        "location": "[variables('location')]",
        "properties": {
            "securityRules": [
                {
                    "name": "default-allow-ssh",
                    "properties": {
                        "priority": 1000,
                        "sourceAddressPrefix": "*",
                        "protocol": "TCP",
                        "destinationPortRange": "22",
                        "access": "Allow",
                        "direction": "Inbound",
                        "sourcePortRange": "*",
                        "destinationAddressPrefix": "*"
                    }
                },
                {
                    "name": "port1",
                    "properties": {
                        "priority": 1010,
                        "sourceAddressPrefix": "*",
                        "protocol": "TCP",
                        "destinationPortRange": "[parameters('port1')]",
                        "access": "Allow",
                        "direction": "Inbound",
                        "sourcePortRange": "*",
                        "destinationAddressPrefix": "*"
                    }
                }
            ]
        }
    },
    {
        "name": 'nsg2')]",
        "type": "Microsoft.Network/networkSecurityGroups",
        "apiVersion": "[variables('networkApiVersion')]",
        "location": "[variables('location')]",
        "properties": {
            "securityRules": [
                {
                    "name": "default-allow-ssh",
                    "properties": {
                        "priority": 1000,
                        "sourceAddressPrefix": "*",
                        "protocol": "TCP",
                        "destinationPortRange": "22",
                        "access": "Allow",
                        "direction": "Inbound",
                        "sourcePortRange": "*",
                        "destinationAddressPrefix": "*"
                    }
                },
                {
                    "name": "port2",
                    "properties": {
                        "priority": 1010,
                        "sourceAddressPrefix": "*",
                        "protocol": "TCP",
                        "destinationPortRange": "[parameters('port2')]",
                        "access": "Allow",
                        "direction": "Inbound",
                        "sourcePortRange": "*",
                        "destinationAddressPrefix": "*"
                    }
                },
                {
                    "name": "port3",
                    "properties": {
                        "priority": 1020,
                        "sourceAddressPrefix": "*",
                        "protocol": "TCP",
                        "destinationPortRange": "[parameters('port3')]",
                        "access": "Allow",
                        "direction": "Inbound",
                        "sourcePortRange": "*",
                        "destinationAddressPrefix": "*"
                    }
                }
            ]
        }
    }
  ]

我将使用不同的参数从另一个模板调用上面的 vm-template.json 2 次。

 {
        "apiVersion": "[variables('resourceDeploymentApiVersion')]",
        "name": "template1",
        "type": "Microsoft.Resources/deployments",
        "properties": {
            "mode": "Incremental",
            "templateLink": {
                "uri": "[variables('vmTemplateURL')]"
            },
            "parameters": {
                ....
            }
        }             
    },
 {
        "apiVersion": "[variables('resourceDeploymentApiVersion')]",
        "name": "template2",
        "type": "Microsoft.Resources/deployments",
        "properties": {
            "mode": "Incremental",
            "templateLink": {
                "uri": "[variables('vmTemplateURL')]"
            },
            "parameters": {
                ....
            }
        }             
    },

如何在 vm-template.json 中使用 nsg1 for template1 和 nsg2 for template2?

4

1 回答 1

0

我在那里没有看到问题,只需为 nsg 名称创建一个新参数并使用它来创建 NSG 并将其链接到 vm。此外,您似乎应该为此使用副本,这更有意义(至少对我而言)。

如果它们有不同的规则,您可以使用变量来创建适当的规则:

"baseRule":     [
    {
        "name": "default-allow-ssh",
        "properties": {
            "priority": 1000,
            "sourceAddressPrefix": "*",
            "protocol": "TCP",
            "destinationPortRange": "22",
            "access": "Allow",
            "direction": "Inbound",
            "sourcePortRange": "*",
            "destinationAddressPrefix": "*"
        }
    },
    {
        "name": "port2",
        "properties": {
            "priority": 1010,
            "sourceAddressPrefix": "*",
            "protocol": "TCP",
            "destinationPortRange": "[parameters('port2')]",
            "access": "Allow",
            "direction": "Inbound",
            "sourcePortRange": "*",
            "destinationAddressPrefix": "*"
        }
    }
],
"extendedRule": [
    {
        "name": "port3",
        "properties": {
            "priority": 1020,
            "sourceAddressPrefix": "*",
            "protocol": "TCP",
            "destinationPortRange": "[parameters('port3')]",
            "access": "Allow",
            "direction": "Inbound",
            "sourcePortRange": "*",
            "destinationAddressPrefix": "*"
        }
    }
]

并使用它来构建正确的规则:

"securityRules": "[if(equals(nsgname, firstnsg), variables('baseRule'), concat(variables('baseRule'), variables('extendedRule'))]"
于 2018-07-16T13:42:23.403 回答