Azure 虛擬機器的使用者資料

使用者資料可讓您將自己的指令碼或中繼資料傳遞至虛擬機器。

什麼是「使用者資料」

使用者資料是一組指令碼或其他中繼資料,會在佈建時插入至 Azure 虛擬機器。 在佈建之後,虛擬機器上的任何應用程式都可以從 Azure Instance Metadata Service (IMDS) 存取使用者資料。

使用者資料是新版本的自訂資料,可提供附加的優點:

  • 佈建之後,可以從 Azure Instance Metadata Service (IMDS) 擷取使用者資料。

  • 使用者資料是持續性的。 可於 VM 存留期期間使用。

  • 使用者可以從 VM 外部更新使用者資料,而不需要停止或重新啟動 VM。

  • 您可以透過 GET VM/VMSS API 使用 $expand 選項來查詢使用者資料。

此外,如果未在佈建時新增使用者資料,您仍然可以在佈建之後新增。

安全性警告

警告

使用者資料將不會加密,因此 VM 上的任何進程都可以查詢此資料。 您不應該將機密資訊儲存在使用者資料中。

請確定您取得最新的 Azure Resource Manager API,以使用新的使用者資料功能。 內容應在傳遞至 API 之前先進行 base64 編碼。 大小不能超過 64 KB。

建立 Azure VM/VMSS 的使用者資料

建立新 VM 時新增使用者資料

使用此 Azure Resource Manager 範本來建立具有使用者資料的新 VM。 如果您使用 REST API,針對單一 VM,請使用 PUT 要求將 'UserData' 新增至 "properties" 區段以建立 VM。

{
  "name": "testVM",
  "location": "West US",
  "properties": {
    "hardwareProfile": {
      "vmSize": "Standard_A1"
    },
    "storageProfile": {
      "osDisk": {
        "osType": "Windows",
        "name": "osDisk",
        "createOption": "Attach",
        "vhd": {
          "uri": "http://myaccount.blob.core.windows.net/container/directory/blob.vhd"
        }
      }
    },
    "userData": "c2FtcGxlIHVzZXJEYXRh",
    "networkProfile": { "networkInterfaces" : [ { "name" : "nic1" } ] },
  }
}

建立新的虛擬機器擴展集時新增使用者資料

使用 REST API,在建立虛擬機器擴展集時,使用 PUT 要求將 'UserData' 新增至 "virtualMachineProfile" 區段。

{
  "location": "West US",
  "sku": {
    "name": "Standard_A1",
    "capacity": 1
  },
  "properties": {
    "upgradePolicy": {
      "mode": "Automatic"
    },
    "virtualMachineProfile": {
      "userData": "VXNlckRhdGE=",
      "osProfile": {
        "computerNamePrefix": "TestVM",
        "adminUsername": "TestUserName",
        "windowsConfiguration": {
          "provisionVMAgent": true,
          "timeZone": "Dateline Standard Time"
        }
      },
      "storageProfile": {
        "osDisk": {
          "createOption": "FromImage",
          "caching": "ReadOnly"
        },
        "imageReference": {
          "publisher": "publisher",
          "offer": "offer",
          "sku": "sku",
          "version": "1.2.3"
        }
      },
      "networkProfile": {"networkInterfaceConfigurations":[{"name":"nicconfig1","properties":{"ipConfigurations":[{"name":"ip1","properties":{"subnet":{"id":"vmssSubnet0"}}}]}}]},
      "diagnosticsProfile": {
        "bootDiagnostics": {
          "enabled": true,
          "storageUri": "https://crputest.blob.core.windows.net"
        }
      }
    },
    "provisioningState": 0,
    "overprovision": false,
    "uniqueId": "00000000-0000-0000-0000-000000000000"
  }
}

擷取使用者資料

在 VM 內執行的應用程式可以透過 IMDS 端點擷取使用者資料。 如需詳細料,請參閱這裡的 IMDS 範例程式碼

客戶可以使用 $expand=userData 端點,透過 REST API 擷取使用者資料的現有值 (要求本文可以保留空白)。

單一 VM:

GET "/subscriptions/{guid}/resourceGroups/{RGName}/providers/Microsoft.Compute/virtualMachines/{VMName}?$expand=userData"

虛擬機器擴展集:

GET "/subscriptions/{guid}/resourceGroups/{RGName}/providers/Microsoft.Compute/virtualMachineScaleSets/{VMSSName}?$expand=userData"

虛擬機器擴展集 VM:

GET "/subscriptions/{guid}/resourceGroups/{RGName}/providers/Microsoft.Compute/virtualMachineScaleSets/{VMSSName}/virtualmachines/{vmss instance id}?$expand=userData"

更新使用者資料

透過 REST API,您可以使用一般 PUT 或 PATCH 要求來更新使用者資料。 使用者資料會更新,而不需要停止或重新啟動 VM。

PUT "/subscriptions/{guid}/resourceGroups/{RGName}/providers/Microsoft.Compute/ virtualMachines/{VMName}

PATCH "/subscriptions/{guid}/resourceGroups/{RGName}/providers/Microsoft.Compute/ virtualMachines/{VMName}

這些要求中的 VM.Properties 應該包含您所需的 UserData 欄位,如下所示:

"properties": {
        "hardwareProfile": {
          "vmSize": "Standard_D1_v2"
        },
        "storageProfile": {
          "imageReference": {
            "sku": "2016-Datacenter",
            "publisher": "MicrosoftWindowsServer",
            "version": "latest",
            "offer": "WindowsServer"
          },
          "osDisk": {
            "caching": "ReadWrite",
            "managedDisk": {
              "storageAccountType": "StandardSSD_LRS"
            },
            "name": "vmOSdisk",
            "createOption": "FromImage"
          }
        },
        "networkProfile": {
          "networkInterfaces": [
            {
              "id": "/subscriptions/{subscription-id}/resourceGroups/myResourceGroup/providers/Microsoft.Network/networkInterfaces/{existing-nic-name}",
              "properties": {
                "primary": true
              }
            }
          ]
        },
        "osProfile": {
          "adminUsername": "{your-username}",
          "computerName": "{vm-name}",
          "adminPassword": "{your-password}"
        },
        "diagnosticsProfile": {
          "bootDiagnostics": {
            "storageUri": "http://{existing-storage-account-name}.blob.core.windows.net",
            "enabled": true
          }
        },
        "userData": "U29tZSBDdXN0b20gRGF0YQ=="
      } 

注意

如果您在此案例中針對 "userData" 傳入空字串,則會刪除使用者資料。

使用者資料和自訂資料

自訂資料會繼續以與現今相同的方式運作。 請注意,您無法從 IMDS 擷取自訂資料。

將使用者資料新增至現有的 VM

如果您有現有的 VM/VMSS 但沒有使用者資料,您仍然可以使用更新命令將使用者資料新增至此 VM,如更新使用者資料一節中所述。 請務必升級至最新版的 Azure Resource Manger API。

下一步

試用 Azure Instance Metadata Service,了解如何從其端點取得 VM 執行個體中繼資料和使用者資料。