Azure VM ARM模板

时间:2019-02-08 15:03:39

标签: azure virtual-machine arm-template azure-template

我的代码中出现错误,因为部署模板验证失败:“ 105”行和“ 9”列的模板资源“ myVMnic”无效:模板函数“ reourceId”无效。有关使用的详细信息,请参见https://aka.ms/arm-template-expressions。有关使用的详细信息,请参见https://aka.ms/arm-template-expressions。 (代码:InvalidTemplate)。

我已尝试解决此错误,但我不会

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

        "scriptURL": {
            "type":"string"

        },
        "adminUsername": {
            "type":"string"
        },
        "adminPassword": {
            "type":"string"
        },

        "envPrefixName": {
            "type": "string",
            "metadata": {
                "description": "Prefix for the environment (2-5 characters)"
            },
            "defaultValue": "cust1",
            "minLength": 2,
            "maxLength": 11
        }
    },
    "variables": {
       "scriptURL":" https://raw.githubusercontent.com/rt7055/simpledevbox1/master/simpledevbox.ps1 ",
       "VMname": "[parameters('envprefixName')]",
       "storageAccountName":"[concat(uniqueString(resourceGroup().id),'soinvm')]",
       "virtualNetworkName": "MyVNET",
       "vnetAddressRange": "10.0.0.0/16",
       "subnetAddressRange": "10.0.0.0/24",
       "subnetName": "Subnet",
        "subnetRef": "[resourceId('Microsoft.Network/virtualNetworks/subnets', variables('virtualNetworkName'), variables('subnetName'))]",
       "imagePublisher": "MicrosoftWindowsServer",
       "imageOffer": "WindowsServer",
      "imageSku": "2012-R2-Datacenter",
      "publicIPAddressName":"mypublicIP",
      "nicName":"myVMnic"
    },
    "resources":[
        {

            "type":"Microsoft.Storage/storageAccounts",
            "name": "[variables('storageAccountName')]",
            "apiversion":"2015-06-15",
            "location":"[resourceGroup().location]",
            "tags":{
            "displayName":"[variables('storageAccountName')] Storage Account"
            },
            "properties":{
              "accountType":"Standard_LRS"
            }


            },
            {
                "type": "Microsoft.Network/publicIPAddresses",
                "name": "[variables('publicIPAddressName')]",
                "location": "[resourceGroup().location]",
                "apiVersion": "2018-10-01",
                "properties": {
                  "publicIPAllocationMethod": "Dynamic"
                                 }
              },
            {
                "apiversion":"2017-06-01",
                "type":"Microsoft.Network/virtualNetworks",
                "name": "[variables('virtualNetworkName')]",
                "location":"[resourceGroup().location]",
                "tags":{
                    "displayname":"Virtual Networks"
                },
                "properties":{
                    "addressSpace":{
                        "addressPrefixes":[
                            "[variables('vnetAddressRange')]"
                        ] 
                    },
                    "subnets":[
                        {
                            "name": "[variables('subnetName')]",
                            "properties":{
                                "addressPrefix": "[variables('subnetAddressRange')]"
                            }

                        }
                    ]
                }
            },
            {
                "apiVersion": "2017-06-01",
                "type": "Microsoft.Network/networkInterfaces",
                "name": "[variables('nicName')]",
                "location":"[resourceGroup().location]",
                "dependsOn": [
                    "[reourceId('Microsoft.Network/publicIPAddresses/', variables('publicIPAddressName'))]",
                    "[resourceId('Microsoft.Network/virtualNetworks/', variables('virtualNetworkName'))]"
                ],
                "tags": {
                   "displayname":" Server Network Interface" 
                },
                "properties":{
                    "ipConfigurations":[
                        {
                            "name": "Ipconfig1",
                            "properties":{
                                "privateIPAllocationMethod":"Dynamic",
                                "publicIPAddress":{
                                    "id": "[resourceId('Microsoft.Network/publicIPAddresses', variables('publicIPAddressName'))]"
                                },
                                "subnet":{
                                    "id": "[variables('subnetRef')]"
                                }
                            }
                        }
                    ]

                }
            },
            {
                "apiVersion": "2017-03-30",
                "type": "Microsoft.Compute/virtualMachines",
                "name": "[parameters('envprefixName')]",
                "location": "[resourceGroup().location]",
                "dependsOn": [
                    "[resourceId('Microsoft.Storage/storageAccounts/',variables('storageAccountName'))]",
                    "[resourceId('Microsoft.Network/networkInterfaces/',variables('nicName'))]"
                ],
                "tags": {
                    "displayname" :"My Virtual Machine"
                },
                "properties":{
                    "hardwareProfile":{
                        "vmSize":"Standard_A1"
                    },
                    "osProfile":{
                        "computerName": "[variables('VMname')]",
                        "adminUsername": "[parameters('adminUsername')]",
                        "adminPassword": "[parameters('adminPassword')]"

                    },
                    "storageProfile":{
                        "imageReference":{
                            "publisher": "[variables('imagePublisher')]",
                            "offer": "[variables('imageOffer')]",
                            "sku": "[variables('imageSku')]",
                            "version": "latest"
                        },

                    "osDisk":{
                        "createOption":"FromImage"
                        } 
                },
                "networkProfile":{
                    "networkInterfaces":[
                        {
                            "id": "[resourceId('Microsoft.Network/networkInterfaces',variables('nicName'))]"
                        }
                    ]
                },
                "diagnosticsProfile":{
                    "bootDiagnostics":{
                        "enabled":true,
                        "storageUri": "[concat('http://',variables('storageAccountName'),'.blob.core.windows.net')]"
                    }
                }
            },
            "resources": [
                {
                    "apiVersion": "2017-03-30",
                    "type": "extensions",
                    "name":"config-app",
                    "location": "[resourceGroup().location]",
                    "dependsOn":[
                        "[concat('Microsoft.Compute/virtualMachines/',variables('VMname'))]"
                    ],
                    "tags":{
                        "displayName":"config-app"
                    },
                    "properties":{
                        "publisher": "Microsoft.Compute",
                        "type": "CustomScriptExtension",
                        "typeHandlerVersion": "1.9",
                        "autoUpgradeMinorVersion": true,
                        "settings": {
                            "fileUris":[
                                "[variables('scriptURL')]"
                            ]                             
                        },
                        "protectedSettings":{
                            "commandToExecute": "[concat('powershell -ExecutionPolicy Unrestricted -File ', './simpledevbox.ps1')]"
                        }                        
                    }
                }
            ]
        }
    ],
    "outputs": {}

}

结果是,这将使内置软件的虚拟机启动。

2 个答案:

答案 0 :(得分:0)

您在2个地方有错别字(第92和46行),可以将模板与此模板进行比较以找出它们:

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "scriptURL": {
            "type": "string"
        },
        "adminUsername": {
            "type": "string"
        },
        "adminPassword": {
            "type": "string"
        },
        "envPrefixName": {
            "type": "string",
            "metadata": {
                "description": "Prefix for the environment (2-5 characters)"
            },
            "defaultValue": "cust1",
            "minLength": 2,
            "maxLength": 11
        }
    },
    "variables": {
        "scriptURL": " https://raw.githubusercontent.com/rt7055/simpledevbox1/master/simpledevbox.ps1 ",
        "VMname": "[parameters('envprefixName')]",
        "storageAccountName": "[concat(uniqueString(resourceGroup().id),'soinvm')]",
        "virtualNetworkName": "MyVNET",
        "vnetAddressRange": "10.0.0.0/16",
        "subnetAddressRange": "10.0.0.0/24",
        "subnetName": "Subnet",
        "subnetRef": "[resourceId('Microsoft.Network/virtualNetworks/subnets', variables('virtualNetworkName'), variables('subnetName'))]",
        "imagePublisher": "MicrosoftWindowsServer",
        "imageOffer": "WindowsServer",
        "imageSku": "2012-R2-Datacenter",
        "publicIPAddressName": "mypublicIP",
        "nicName": "myVMnic"
    },
    "resources": [
        {
            "type": "Microsoft.Storage/storageAccounts",
            "name": "[variables('storageAccountName')]",
            "apiversion": "2015-06-15",
            "location": "[resourceGroup().location]",
            "tags": {
                "displayName": "[variables('storageAccountName')]"
            },
            "properties": {
                "accountType": "Standard_LRS"
            }
        },
        {
            "type": "Microsoft.Network/publicIPAddresses",
            "name": "[variables('publicIPAddressName')]",
            "location": "[resourceGroup().location]",
            "apiVersion": "2018-10-01",
            "properties": {
                "publicIPAllocationMethod": "Dynamic"
            }
        },
        {
            "apiversion": "2017-06-01",
            "type": "Microsoft.Network/virtualNetworks",
            "name": "[variables('virtualNetworkName')]",
            "location": "[resourceGroup().location]",
            "tags": {
                "displayname": "Virtual Networks"
            },
            "properties": {
                "addressSpace": {
                    "addressPrefixes": [
                        "[variables('vnetAddressRange')]"
                    ]
                },
                "subnets": [
                    {
                        "name": "[variables('subnetName')]",
                        "properties": {
                            "addressPrefix": "[variables('subnetAddressRange')]"
                        }
                    }
                ]
            }
        },
        {
            "apiVersion": "2017-06-01",
            "type": "Microsoft.Network/networkInterfaces",
            "name": "[variables('nicName')]",
            "location": "[resourceGroup().location]",
            "dependsOn": [
                "[resourceId('Microsoft.Network/publicIPAddresses/', variables('publicIPAddressName'))]",
                "[resourceId('Microsoft.Network/virtualNetworks/', variables('virtualNetworkName'))]"
            ],
            "tags": {
                "displayname": " Server Network Interface"
            },
            "properties": {
                "ipConfigurations": [
                    {
                        "name": "Ipconfig1",
                        "properties": {
                            "privateIPAllocationMethod": "Dynamic",
                            "publicIPAddress": {
                                "id": "[resourceId('Microsoft.Network/publicIPAddresses', variables('publicIPAddressName'))]"
                            },
                            "subnet": {
                                "id": "[variables('subnetRef')]"
                            }
                        }
                    }
                ]
            }
        },
        {
            "apiVersion": "2017-03-30",
            "type": "Microsoft.Compute/virtualMachines",
            "name": "[parameters('envprefixName')]",
            "location": "[resourceGroup().location]",
            "dependsOn": [
                "[resourceId('Microsoft.Storage/storageAccounts/',variables('storageAccountName'))]",
                "[resourceId('Microsoft.Network/networkInterfaces/',variables('nicName'))]"
            ],
            "tags": {
                "displayname": "My Virtual Machine"
            },
            "properties": {
                "hardwareProfile": {
                    "vmSize": "Standard_A1"
                },
                "osProfile": {
                    "computerName": "[variables('VMname')]",
                    "adminUsername": "[parameters('adminUsername')]",
                    "adminPassword": "[parameters('adminPassword')]"
                },
                "storageProfile": {
                    "imageReference": {
                        "publisher": "[variables('imagePublisher')]",
                        "offer": "[variables('imageOffer')]",
                        "sku": "[variables('imageSku')]",
                        "version": "latest"
                    },
                    "osDisk": {
                        "createOption": "FromImage"
                    }
                },
                "networkProfile": {
                    "networkInterfaces": [
                        {
                            "id": "[resourceId('Microsoft.Network/networkInterfaces',variables('nicName'))]"
                        }
                    ]
                },
                "diagnosticsProfile": {
                    "bootDiagnostics": {
                        "enabled": true,
                        "storageUri": "[concat('http://',variables('storageAccountName'),'.blob.core.windows.net')]"
                    }
                }
            },
            "resources": [
                {
                    "apiVersion": "2017-03-30",
                    "type": "extensions",
                    "name": "config-app",
                    "location": "[resourceGroup().location]",
                    "dependsOn": [
                        "[concat('Microsoft.Compute/virtualMachines/',variables('VMname'))]"
                    ],
                    "tags": {
                        "displayName": "config-app"
                    },
                    "properties": {
                        "publisher": "Microsoft.Compute",
                        "type": "CustomScriptExtension",
                        "typeHandlerVersion": "1.9",
                        "autoUpgradeMinorVersion": true,
                        "settings": {
                            "fileUris": [
                                "[variables('scriptURL')]"
                            ]
                        },
                        "protectedSettings": {
                            "commandToExecute": "[concat('powershell -ExecutionPolicy Unrestricted -File ', './simpledevbox.ps1')]"
                        }
                    }
                }
            ]
        }
    ]
}

使用具有适当扩展名的任何编辑器,例如VSCode或Visual Studio(我听说eclipse都具有arm模板扩展名),您也可以轻松捕获此类错误。

答案 1 :(得分:0)

有关ARM语法的信息可以通过Azure客户端获得:

az deployment group validate --resource-group {} --template-file {} --debug

以您的情况说明:

json.decoder.JSONDecodeError: Expecting ','
ValidationError: Failed to parse 'test.json', please check whether it is a valid JSON format

打开IDE后,问题出在哪里就很明显了:)