如何基于参数privateonly或public附加公共IP,尝试使用多个VM创建它。我在这里提供正在运行的完整模板。
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"location": {
"type": "string",
"defaultValue": "[resourceGroup().location]",
"metadata": {
"description": "Location for the resources."
}
},
"vmName": {
"type": "string",
"defaultValue": "vm",
"metadata": {
"description": "Name for the Virtual Machine."
}
},
"ClusterType": {
"type": "string",
"defaultValue": "3 vm apache",
"metadata": {
"description": "Type of cluster to deploy, this is using a single storage account"
}
},
"adminUsername": {
"type": "string",
"defaultValue": "centos",
"metadata": {
"description": "User name for the Virtual Machine."
}
},
"adminPasswordOrKey": {
"type": "string"
},
"vmSize": {
"type": "string",
"metadata": {
"description": "Size for the Virtual Machine."
}
},
"storageNewOrExisting": {
"type": "string",
"defaultValue": "new"
},
"storageAccountName": {
"type": "string",
"defaultValue": "[concat('storage', uniqueString(resourceGroup().id))]",
"metadata": {
"description": "Name of the storage account"
}
},
"storageAccountType": {
"type": "string",
"defaultValue": "Standard_LRS",
"metadata": {
"description": "Storage account type"
}
},
"storageAccountResourceGroupName": {
"type": "string",
"defaultValue": "[resourceGroup().name]",
"metadata": {
"description": "Name of the resource group for the existing storage account"
}
},
"virtualNetworkNewOrExisting": {
"type": "string",
"defaultValue": "new",
"metadata": {
"description": "Determines whether or not a new virtual network should be provisioned."
}
},
"virtualNetworkName": {
"type": "string",
"defaultValue": "VirtualNetwork",
"metadata": {
"description": "Name of the virtual network"
}
},
"addressPrefixes": {
"type": "array",
"defaultValue": [
"10.0.0.0/16"
],
"metadata": {
"description": "Address prefix of the virtual network"
}
},
"subnetName": {
"type": "string",
"defaultValue": "default",
"metadata": {
"description": "Name of the subnet"
}
},
"subnetPrefix": {
"type": "string",
"defaultValue": "10.0.0.0/24",
"metadata": {
"description": "Subnet prefix of the virtual network"
}
},
"virtualNetworkResourceGroupName": {
"type": "string",
"defaultValue": "[resourceGroup().name]",
"metadata": {
"description": "Name of the resource group for the existing virtual network"
}
},
"vmDataDiskSize":{
"type": "string",
"defaultValue": "50",
"metadata": {
"description": "Minimum data disk size should be 50 GB"
},
"publicIpName": {
"type": "string",
"defaultValue": "rxnode",
"metadata": {
"description": "Name of public IP Address"
}
}
},
"variables": {
"publisher": "OpenLogic",
"offer": "CentOS",
"sku": "7.3",
"version": "latest",
"vmBootDiskSize": 50,
"nicName": "[concat(parameters('vmName'), '-nic-')]",
"numberOfVM": "[int(first(parameters('ClusterType')))]",
"apacheinstallation": "[contains(parameters('ClusterType'), 'apache')]",
"networkSecurityGroupName": "[concat(parameters('vmName'), '-nsg-ssh')]",
"publicIpName": "[concat(parameters('vmName'),'-publicip')]"
"privateIp": {
"privateIPAllocationMethod": "Dynamic",
"subnet": {
"id": "[resourceId(parameters('virtualNetworkResourceGroupName'), 'Microsoft.Network/virtualNetworks/subnets/', parameters('virtualNetworkName'), parameters('subnetName'))]"
}
},
"copy": [
{
"name": "publicIPAddress",
"count": "[variables('numberOfVM')]",
"input": {
"id": "[resourceId('Microsoft.Network/publicIPAddresses', concat(variables('publicIpName'), copyIndex('publicIPAddress')))]"
}
}
]
},
"resources": [
{
"condition": "[equals(parameters('storageNewOrExisting'), 'new')]",
"type": "Microsoft.Storage/storageAccounts",
"apiVersion": "2018-02-01",
"name": "[parameters('storageAccountName')]",
"location": "[parameters('location')]",
"kind": "Storage",
"sku": {
"name": "[parameters('storageAccountType')]"
}
},
{
"condition": "[equals(parameters('publicIpName'), 'None')]",
"type": "Microsoft.Network/publicIPAddresses",
"apiVersion": "2018-04-01",
"name": "[concat(variables('publicIpName'), copyIndex())]",
"location": "[parameters('location')]",
"copy": {
"name": "ipLoop",
"count": "[variables('numberOfVM')]"
},
"sku": {
"name": "Basic"
},
"properties": {
"publicIPAllocationMethod": "Dynamic"
}
},
{
"condition": "[equals(parameters('virtualNetworkNewOrExisting'), 'new')]",
"type": "Microsoft.Network/virtualNetworks",
"apiVersion": "2018-04-01",
"name": "[parameters('virtualNetworkName')]",
"location": "[parameters('location')]",
"properties": {
"addressSpace": {
"addressPrefixes": "[parameters('addressPrefixes')]"
},
"subnets": [
{
"name": "[parameters('subnetName')]",
"properties": {
"addressPrefix": "[parameters('subnetPrefix')]"
}
}
]
}
},
{
"name": "[variables('networkSecurityGroupName')]",
"type": "Microsoft.Network/networkSecurityGroups",
"apiVersion": "2018-04-01",
"location": "[parameters('location')]",
"properties": {
"securityRules": [
{
"name": "default-allow-ssh",
"properties": {
"priority": 1000,
"sourceAddressPrefix": "*",
"protocol": "Tcp",
"destinationPortRange": "22",
"access": "Allow",
"direction": "Inbound",
"sourcePortRange": "*",
"destinationAddressPrefix": "*"
}
},
{
"name": "allow-webport-8080",
"properties": {
"priority": 1200,
"sourceAddressPrefix": "*",
"protocol": "Tcp",
"destinationPortRange": "8080",
"access": "Allow",
"direction": "Inbound",
"sourcePortRange": "*",
"destinationAddressPrefix": "*"
}
}
]
}
},
{
"apiVersion": "2018-04-01",
"type": "Microsoft.Network/networkInterfaces",
"name": "[concat(variables('nicName'), copyIndex())]",
"location": "[parameters('location')]",
"copy": {
"name": "nicLoop",
"count": "[variables('numberOfVM')]"
},
"dependsOn": [
"ipLoop",
"[parameters('virtualNetworkName')]",
"[variables('networkSecurityGroupName')]"
],
"properties": {
"ipConfigurations": [
{
"name": "ipconfig1",
"properties": "[if(equals(variables('publicipName'), 'None'), variables('privateIp'), union(variables('privateIp'), variables('publicIPAddress')[copyIndex()]))]" }
],
"networkSecurityGroup": {
"id": "[resourceId('Microsoft.Network/networkSecurityGroups', variables('networkSecurityGroupName'))]"
}
}
},
{
"apiVersion": "2018-04-01",
"type": "Microsoft.Compute/virtualMachines",
"name": "[concat(parameters('vmName'), copyIndex())]",
"location": "[parameters('location')]",
"copy": {
"name": "virtualMachineLoop",
"count": "[variables('numberOfVM')]"
},
"dependsOn": [
"[parameters('storageAccountName')]",
"nicLoop"
],
"properties": {
"hardwareProfile": {
"vmSize": "[parameters('vmSize')]"
},
"osProfile": {
"computerName": "[concat(parameters('vmName'), copyIndex())]",
"adminUsername": "[parameters('adminUsername')]",
"adminPassword": "[parameters('adminPasswordOrKey')]",
"linuxConfiguration": {
"disablePasswordAuthentication": true,
"ssh": {
"publicKeys": [
{
"path": "[concat('/home/', parameters('adminUsername'), '/.ssh/authorized_keys')]",
"keyData": "[parameters('adminPasswordOrKey')]"
}
]
}
}
},
"storageProfile": {
"imageReference": {
"publisher": "[variables('publisher')]",
"offer": "[variables('offer')]",
"sku": "[variables('sku')]",
"version": "[variables('version')]"
},
"osDisk": {
"caching": "ReadWrite",
"createOption": "FromImage",
"diskSizeGB": "[variables('vmBootDiskSize')]"
},
"copy": [
{
"name": "dataDisks",
"count": 1,
"input": {
"caching": "ReadWrite",
"diskSizeGB": "[parameters('vmDataDiskSize')]",
"lun": "[copyIndex('dataDisks')]",
"name": "[concat(parameters('vmName'), '-datadisk', copyIndex(), copyIndex('dataDisks'))]",
"createOption": "Empty"
}
}
]
},
"networkProfile": {
"networkInterfaces": [
{
"id": "[resourceId('Microsoft.Network/networkInterfaces', concat(variables('nicName'), copyindex()))]"
}
]
},
"diagnosticsProfile": {
"bootDiagnostics": {
"enabled": true,
"storageUri": "[reference(resourceId(parameters('storageAccountResourceGroupName'), 'Microsoft.Storage/storageAccounts/', parameters('storageAccountName')), '2018-02-01').primaryEndpoints.blob]"
}
}
}
}
],
"outputs": {
}
}
在这里,我将json上下文放入if条件。如果需要附加公共IP,则它将json上下文传递给publicIPAddress,否则它将传递null值。
答案 0 :(得分:0)
您可以将union()
函数与预定义变量结合使用来实现。更加干净+我不确定您的方式是否适用于所有转义符(我认为您不允许使用json()
函数构建真正的json;至少我尝试过的-都失败了。)
"variables": {
"publicIP": {
"publicIPAddress": {
"id": "[resourceId('Microsoft.Network/publicIPAddresses', concat(variables('publicIpName')))]"
}
},
"privateIp": {
"privateIPAllocationMethod": "Dynamic",
"subnet": {
"id": "[resourceId(parameters('virtualNetworkResourceGroupName'), 'Microsoft.Network/virtualNetworks/subnets/', parameters('virtualNetworkName'), parameters('subnetName'))]"
}
}
}
,然后在ipConfigurations中执行以下操作:
"ipConfigurations": [
{
"name": "ipconfig1",
"properties": "[if(equals(variables('publicipName'), 'None'), variables('privateIp'), union(variables('privateIp'), variables('publicIp'))]"
}
],
在不需要公共IP时将使用privateIp变量,并在需要时添加公共IP。
编辑:如果将copyIndex与publicIp一起使用,则需要这样做:
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {},
"variables": {
"name": "testo",
"copy": [
{
"name": "publicIPAddress",
"count": 3,
"input": {
"publicIPAddress": {
"id": "[resourceId('Microsoft.Network/publicIPAddresses', concat(variables('name'), '-ip-', copyIndex('publicIPAddress')))]"
}
}
}
],
"privateIp": {
"privateIPAllocationMethod": "Dynamic",
"subnet": {
"id": "resourceId"
}
}
},
"resources": [
{
"apiVersion": "2015-06-15",
"type": "Microsoft.Network/publicIPAddresses",
"name": "[concat(variables('name'), '-ip-', copyIndex())]",
"location": "[resourceGroup().location]",
"copy": {
"name": "ipLoop",
"count": 3
},
"properties": {
"publicIPAllocationMethod": "Dynamic"
}
},
{
"apiVersion": "2018-04-01",
"type": "Microsoft.Network/networkInterfaces",
"name": "[concat(variables('name'), copyIndex())]",
"location": "[resourceGroup().location]",
"dependsOn": [
"ipLoop"
],
"copy": {
"name": "nicLoop",
"count": 3
},
"properties": {
"ipConfigurations": [
{
"name": "ipconfig1",
"properties": "[union(variables('privateIp'), variables('publicIPAddress')[copyIndex()])]"
}
]
}
}
]
}