json - 创建 JSON 意外字符序列时出错

标签 json azure scripting azure-rm-template

我一直在尝试为我们的睾丸创建一个基本的 json,以便在需要时抛出一台机器。我完成了大部分工作,但出现以下错误:

Line 158 Unexpected character sequence in member name
Line 167 A member with the name 'Properties' already exists

我知道 67 需要是唯一的,但每次我更改和删除 { 时,都会产生更多错误。

第 157 行的 { 是一个意想不到的字符,我猜它应该是一个字符串,也许?

我的模板如下。

{
  "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "location": {
      "type": "string"
    },
    "virtualMachineName": {
      "type": "string"
    },
    "offer": {
      "type": "string",
      "defaultvalue": "Windows",
      "allowedValues": [
        "windows",
        "centos",
        "RHEL"
      ]
    },
    "sku": {
      "type": "string",
      "defaultvalue": "2012-r2-datacenter",
      "allowedValues": [
        "2012-R2-Datacenter",
        "2012-R2",
        "68",
        "72",
        "73",
        "74"
      ]
    },
    "virtualMachineSize": {
      "type": "string",
      "defaultvalue": "standard_A0",
      "allowedValues": [
        "Standard_A0",
        "Standard_A1",
        "standard_A2"
      ]
    },
    "adminUsername": {
      "type": "string"
    },
    "ipaddress": {
      "type": "string"
    },
    "virtualNetworkName": {
      "type": "string"
    },
    "networkInterfaceName": {
      "type": "string"
    },
    "networkSecurityGroupName": {
      "type": "string"
    },
    "adminPassword": {
      "type": "securestring"
    },
    "storageAccountName": {
      "type": "string"
    },
    "diagnosticsStorageAccountName": {
      "type": "string"
    },
    "diagnosticsStorageAccountId": {
      "type": "string"
    },
    "subnetName": {
      "type": "string"
    },
    "ContainerName": {
      "type": "string"
    },
    "autoShutdownStatus": {
      "type": "string"
    },
    "autoShutdownTime": {
      "type": "string"
    },
    "autoShutdownTimeZone": {
      "type": "string"
    },
    "autoShutdownNotificationStatus": {
      "type": "string"
    }
  },
  "variables": {
    "vnetId": "[resourceId('avset','Microsoft.Network/virtualNetworks', parameters('virtualNetworkName'))]",
    "subnetRef": "[concat(variables('vnetId'), '/subnets/', parameters('subnetName'))]",
    "stdvhdcontainername": "[concat(parameters('Containername'))]",
    "vmosidiskname": "[concat(parameters('virtualMachineName'),'-osdisk')]"
  },
  "resources": [
    {
      "name": "[parameters('virtualMachineName')]",
      "type": "Microsoft.Compute/virtualMachines",
      "apiVersion": "2016-04-30-preview",
      "location": "[parameters('location')]",
      "dependsOn": [
        "[concat('Microsoft.Network/networkInterfaces/', parameters('networkInterfaceName'))]"
      ],
      "properties": {
        "diagnosticsProfile": {
          "bootDiagnostics": {
            "enabled": true,
            "storageUri": "[concat (parameters('diagnosticsStorageAccountName')), ['blob']]"
          }
        },
        "hardwareProfile": {
          "vmSize": "[parameters('virtualMachineSize')]"
        },
        "networkProfile": {
          "networkInterfaces": [
            {
              "id": "[resourceId('Microsoft.Network/networkInterfaces', parameters('networkInterfaceName'))]"
            }
          ]
        },
        "osProfile": {
          "computerName": "[parameters('virtualMachineName')]",
          "adminUsername": "[parameters('adminUsername')]",
          "adminPassword": "[parameters('adminPassword')]"
        },
        "storageProfile": {
          "imageReference": {
            "offer": "[concat(parameters('offer'))]",
            "sku": "[concat(parameters('offer'))]",
            "version": "latest"
          }
        },
        "osDisk": {
          "vhd": {
            "name": "[variables('vmosidiskname')]",
            "uri": "[concat('http://', parameters('storageAccountName'), '.blob.core.windows.net/', variables('stdvhdcontainername'),'/', variables('vmosidiskname'), '.vhd')]"
          },
          "name": "[parameters('virtualMachineName')]"
        },
        "name": "[concat('shutdown-computevm-', parameters('virtualMachineName'))]",
        "type": "Microsoft.DevTestLab/schedules",
        "apiVersion": "2017-04-26-preview",
        "location": "[parameters('location')]",
        "properties": {
          "status": "[parameters('autoShutdownStatus')]",
          "taskType": "ComputeVmShutdownTask",
          "dailyRecurrence": {
            "time": "[parameters('autoShutdownTime')]"
          },
          "timeZoneId": "[parameters('autoShutdownTimeZone')]",
          "targetResourceId": "[resourceId('Microsoft.Compute/virtualMachines', parameters('virtualMachineName'))]",
          "notificationSettings": {
            "status": "[parameters('autoShutdownNotificationStatus')]",
            "timeInMinutes": "30"
          },
          "dependsOn": [
            "[concat('Microsoft.Compute/virtualMachines/', parameters('virtualMachineName'))]"
          ]
        },
        {
          "name": "[parameters('networkInterfaceName')]",
          "type": "Microsoft.Network/networkInterfaces",
          "apiVersion": "2016-09-01",
          "location": "[parameters('location')]"
        },
        "dependsOn": [
          "[concat('Microsoft.Network/networkSecurityGroups/', parameters('networkSecurityGroupName'))]"
        ],
        "properties": {
          "ipConfigurations": [
            {
              "name": "ipconfig1",
              "properties": {
                "subnet": {
                  "id": "[variables('subnetRef')]"
                },
                "privateipaddress": "[concat(parameters('ipaddress'))]",
                "privateIPAllocationMethod": "static"
              }
            }
          ]
        }

      },
      "outputs": [
        {
          "adminUsername": {
            "type": "string",
            "value": "[parameters('adminUsername')]"
          }

        }
      ]
    }
  ]
}


Any pointers would be greatly appreciated.. thanks for your help :)

参数如下:)

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "location": {
      "value": "westeurope"
    },
    "virtualMachineName": {
      "value": "cent-os-vm2"
    },
    "virtualMachineSize": {
      "value": "Standard_A0"
    },
    "adminUsername": {
      "value": "localadmin"
    },
    "virtualNetworkName": {
      "value": "avset-vnet"
    },
    "networkInterfaceName": {
      "value": "cent-os-vm01652"
    },
    "networkSecurityGroupName": {
      "value": "cent-os-vm01-nsg"
    },
    "storageAccountName": {
      "value": "avsetdisks419"
    },
    "diagnosticsStorageAccountName": {
      "value": "avsetdiag112"
    },
    "diagnosticsStorageAccountId": {
      "value": "avsetdiag117"
    },
    "subnetName": {
      "value": "default"
    },
    "autoShutdownStatus": {
      "value": "Enabled"
    },
    "autoShutdownTime": {
      "value": "19:00"
    },
    "autoShutdownTimeZone": {
      "value": "UTC"
    },
    "autoShutdownNotificationStatus": {
      "value": "Disabled"
    },
    "ContainerName": {
      "value": "vhd"
    },
    "offer": {
      "value": "windows"
    },
    "sku": {
      "value": "2012-R2-Datacenter"
    },
    "ipaddress": {
      "value": "10.0.1.9"
    }
  }
}

最佳答案

您的模板中有很多错误。以下模板应该可以工作。

{
  "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "location": {
      "type": "string"
    },
    "virtualMachineName": {
      "type": "string"
    },
    "offer": {
      "type": "string",
      "defaultvalue": "windows",
      "allowedValues": [
        "windows",
        "centos",
        "RHEL"
      ]
    },
    "sku": {
      "type": "string",
      "defaultvalue": "2012-R2-Datacenter",
      "allowedValues": [
        "2012-R2-Datacenter",
        "2012-R2",
        "68",
        "72",
        "73",
        "74"
      ]
    },
    "virtualMachineSize": {
      "type": "string",
      "defaultvalue": "Standard_A0",
      "allowedValues": [
        "Standard_A0",
        "Standard_A1",
        "standard_A2"
      ]
    },
    "adminUsername": {
      "type": "string"
    },
    "ipaddress": {
      "type": "string"
    },
    "virtualNetworkName": {
      "type": "string"
    },
    "networkInterfaceName": {
      "type": "string"
    },
    "networkSecurityGroupName": {
      "type": "string"
    },
    "adminPassword": {
      "type": "securestring"
    },
    "storageAccountName": {
      "type": "string"
    },
    "diagnosticsStorageAccountName": {
      "type": "string"
    },
    "diagnosticsStorageAccountId": {
      "type": "string"
    },
    "subnetName": {
      "type": "string"
    },
    "ContainerName": {
      "type": "string"
    },
    "autoShutdownStatus": {
      "type": "string"
    },
    "autoShutdownTime": {
      "type": "string"
    },
    "autoShutdownTimeZone": {
      "type": "string"
    },
    "autoShutdownNotificationStatus": {
      "type": "string"
    }
  },
  "variables": {
    "vnetId": "[resourceId('avset','Microsoft.Network/virtualNetworks', parameters('virtualNetworkName'))]",
    "subnetRef": "[concat(variables('vnetId'), '/subnets/', parameters('subnetName'))]",
    "stdvhdcontainername": "[concat(parameters('Containername'))]",
    "vmosidiskname": "[concat(parameters('virtualMachineName'),'-osdisk')]"
  },
  "resources": [
    {
      "name": "[parameters('virtualMachineName')]",
      "type": "Microsoft.Compute/virtualMachines",
      "apiVersion": "2016-04-30-preview",
      "location": "[parameters('location')]",
      "dependsOn": [
        "[concat('Microsoft.Network/networkInterfaces/', parameters('networkInterfaceName'))]"
      ],
      "properties": {
            "diagnosticsProfile": {
            "bootDiagnostics": {
                "enabled": true,
                "storageUri": "[concat (parameters('diagnosticsStorageAccountName')), ['blob']]"
            }
            },
            "hardwareProfile": {
            "vmSize": "[parameters('virtualMachineSize')]"
            },
            "networkProfile": {
                "networkInterfaces": [
                {
                "id": "[resourceId('Microsoft.Network/networkInterfaces', parameters('networkInterfaceName'))]"
                }
            ]
            },
            "osProfile": {
                "computerName": "[parameters('virtualMachineName')]",
                "adminUsername": "[parameters('adminUsername')]",
                "adminPassword": "[parameters('adminPassword')]"
            },
            "storageProfile": {
            "imageReference": {
                "offer": "[concat(parameters('offer'))]",
                "sku": "[concat(parameters('offer'))]",
                "version": "latest"
            }
            },
            "osDisk": {
            "vhd": {
                "name": "[variables('vmosidiskname')]",
                "uri": "[concat('http://', parameters('storageAccountName'), '.blob.core.windows.net/', variables('stdvhdcontainername'),'/', variables('vmosidiskname'), '.vhd')]"
            },
            "name": "[parameters('virtualMachineName')]"
            }
            }
        },
        {
            "name": "[concat('shutdown-computevm-', parameters('virtualMachineName'))]",
            "type": "Microsoft.DevTestLab/schedules",
            "apiVersion": "2017-04-26-preview",
            "location": "[parameters('location')]",
            "properties": {
            "status": "[parameters('autoShutdownStatus')]",
            "taskType": "ComputeVmShutdownTask",
            "dailyRecurrence": {
                "time": "[parameters('autoShutdownTime')]"
            },
            "timeZoneId": "[parameters('autoShutdownTimeZone')]",
            "targetResourceId": "[resourceId('Microsoft.Compute/virtualMachines', parameters('virtualMachineName'))]",
            "notificationSettings": {
                "status": "[parameters('autoShutdownNotificationStatus')]",
                "timeInMinutes": "30"
            },
                "dependsOn": [
                "[concat('Microsoft.Compute/virtualMachines/', parameters('virtualMachineName'))]"
                ]
            }
        },

        {
            "name": "[parameters('networkInterfaceName')]",
            "type": "Microsoft.Network/networkInterfaces",
            "apiVersion": "2016-09-01",
            "location": "[parameters('location')]",
            "dependsOn": [
            "[concat('Microsoft.Network/networkSecurityGroups/', parameters('networkSecurityGroupName'))]"
            ],
        "properties": {
          "ipConfigurations": [
            {
              "name": "ipconfig1",
              "properties": {
                "subnet": {
                  "id": "[variables('subnetRef')]"
                },
                "privateipaddress": "[concat(parameters('ipaddress'))]",
                "privateIPAllocationMethod": "static"
              }
            }
          ]
        }

      }


  ],
    "outputs": {

         "adminUsername": {
           "type": "string",
           "value": "[parameters('adminUsername')]"
          }  
      }
}

在第 162 行,它丢失了一个 ,。资源 Microsoft.Compute/virtualMachinesMicrosoft.DevTestLab/schedulesMicrosoft.Network/networkInterfaces 是并行的。但在您的模板中,Microsoft.Compute/virtualMachines 包含其他内容,因此您会收到错误日志名称为“Properties”已存在

注意:您还需要检查您的默认值。请注意区分大小写。

更新: Ipconfigurations 是错误的,应该如下所示:

   "properties": {
        "ipConfigurations": [
          {
            "name": "ipconfig1",
            "properties": {
              "privateIPAllocationMethod": "Dynamic",
              "publicIPAddress": {
                "id": "[resourceId('Microsoft.Network/publicIPAddresses',variables('publicIPAddressName'))]"
              },
              "subnet": {
                "id": "[variables('subnetRef')]"
              }
            }
          }

更新2:

template.jsonparameters.json对我有用。也许您可以根据此示例修改您的模板。

关于json - 创建 JSON 意外字符序列时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47397318/

相关文章:

groovy - 如何枚举 groovy 脚本中所有定义的变量

c# - 如何在asp.net core应用程序中集成像office 365这样的多单点登录

linux - bash 中使用的 -ex 选项是什么 | #!/bin/bash -ex 意思

javascript - 如何使用node.js比较两个json数组

从列表 json.decoder.JSONDecodeError : Expecting value: line 1 column 1 (char 0) 循环时出现 python 错误

php - Laravel+Dingo 中一致的 REST API 响应

java - 如何使用Gson保留列表名称

azure - 使用托管标识从 VM 访问 Azure 存储

azure - 逻辑应用程序 - Azure blob 存储 - 提取到存档文件夹操作 - RequestEntityTooLarge

linux - 在 unix 或 shell 中将新行打印为字符串文字