当前位置: 首页 > 知识库问答 >
问题:

Azure虚拟机ARM模板

简学文
2023-03-14

当部署模板验证失败时,我的代码中出现了一个错误:行105和列9处的模板资源myVMć无效:模板函数reourceId无效。使用详情请看https://aka.ms/arm-template-expressions。使用详情请见https://aka.ms/arm-template-expressions。(代码:InvalidTemboard)。

我已经尝试解决这个错误但我不会

{
    "$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个答案

沃学
2023-03-14

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

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后,问题就显而易见了:)

艾鹭洋
2023-03-14

您有两处输入错误(第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模板扩展)。

 类似资料:
  • 如果您没有在Azure中设置的虚拟机规模上配置Insights,并且从Azure门户访问Insights窗格,您将得到以下通知: “通过设置Azure虚拟机规模,您可以立即获得主机CPU、磁盘和VMSS的up/down状态。” 当您单击Azure Portal内部的虚拟机规模集的Metrics窗格时,这似乎是正确的,因为在那里您可以开箱即用地显示各种平台指标(如CPU百分比等)并处理这些数据。 如

  • 在Azure中,有2个选项可用于创建虚拟机。A、 普通VM B.经典VM 有人知道这两种选择有什么区别吗?我们什么时候使用一个而不是另一个?

  • 我在Azure windows server 2012虚拟机上安装了IIS 8.0中的FTP。 按照这篇文章中的说明(http://itq.nl/walkthrough-hosting-ftp-on-iis-7-5-a-windows-azure-vm-2/),我已经能够使FTP在被动模式下工作正常,但是当试图从FileZilla在主动模式下连接时失败了。FTP客户端可以在活动模式下连接到服务器,

  • 查看虚拟机相关的监控告警信息。 监控菜单下的虚拟机页面主要用于查看虚拟机相关的监控告警信息。 入口:在云管平台单击左上角导航菜单,在弹出的左侧菜单栏中单击 “监控/资源/虚拟机” 菜单项,进入虚拟机页面。 查看虚拟机列表 该功能用于查看虚拟机的监控告信息。 在虚拟机页面,支持查看以下信息: 名称:虚拟机的名称。 IP:虚拟机的IP地址。 监控状态:虚拟机是否设置告警以及发生告警。 状态:虚拟机的当

  • 主机回收站用于存放用户删除的虚拟机和裸金属文件。 主机回收站用于存放用户删除的虚拟机和裸金属文件。回收站中主机文件默认保存3天,如有误删除的主机需要在3天内进行恢复操作,可以将其恢复到原来位置,超过3天后,文件将被彻底删除。 入口:在云管平台单击左上角导航菜单,在弹出的左侧菜单栏中单击 “主机/回收站/主机” 菜单项,进入主机回收站列表。 清除 当确定回收站中的主机无用后,可使用清除功能立即彻底删

  • 虚拟机是采用虚拟化技术构建的运行在宿主机上的虚拟机实例。 虚拟机是采用虚拟化技术构建的运行在宿主机上的虚拟机实例,包括CPU、内存、操作系统、硬盘、网卡等完整的虚拟硬件基础环境。 虚拟机来源: 当云管平台对接其他平台云账号后,将会自动同步其他平台上的虚拟机到云管平台上进行管理。 新建虚拟机。 入口:在云管平台单击左上角导航菜单,在弹出的左侧菜单栏中单击 “主机/主机/虚拟机” 菜单项,进入虚拟机页