Share via


在 Azure Stack Edge Pro 裝置上執行的 VM 上部署自訂腳本擴充功能

適用于: Yes for Pro GPU SKU Azure Stack Edge Pro - GPU Yes for Pro 2 SKU Azure Stack Edge Pro 2 Yes for Pro R SKU Azure Stack Edge Pro R Yes for Mini R SKU Azure Stack Edge Mini R

自訂腳本延伸模組會在 Azure Stack Edge Pro 裝置上執行的虛擬機器上下載和執行腳本或命令。 本文詳細說明如何使用 Azure Resource Manager 範本來安裝和執行自訂腳本擴充功能。

關於自訂腳本擴充功能

自訂腳本擴充功能適用于部署後設定、軟體安裝或任何其他設定/管理工作。 您可以從Azure 儲存體或其他可存取的網際網路位置下載腳本,也可以提供腳本或命令給延伸模組執行時間。

自訂腳本擴充功能會與 Azure Resource Manager 範本整合。 您也可以使用 Azure CLI、PowerShell 或 Azure 虛擬機器 REST API 來執行它。

自訂腳本擴充功能的 OS

Windows 上自訂腳本擴充功能支援的 OS

Windows 的自訂腳本擴充功能將在下列 OS 上執行。 其他版本可能會運作,但尚未在 Azure Stack Edge Pro 裝置上執行的 VM 內部測試。

散發 版本
Windows Server 2019 核心
Windows Server 2016 核心

Linux 上自訂腳本擴充功能支援的 OS

Linux 的自訂腳本擴充功能將在下列 OS 上執行。 其他版本可能會運作,但尚未在 Azure Stack Edge Pro 裝置上執行的 VM 內部測試。

散發 版本
Linux:Ubuntu 18.04 LTS
Linux:Red Hat Enterprise Linux 7.4, 7.5, 7.7

必要條件

  1. 將 VM 範本和參數檔案 下載到您的用戶端電腦。 將下載解壓縮到您將作為工作目錄的目錄。

  2. 您應該已在裝置上建立並部署 VM。 若要建立 VM,請遵循使用範本 在 Azure Stack Edge Pro 上部署 VM 中的所有步驟

    如果您需要從 GitHub 下載腳本,或從外部Azure 儲存體,同時設定計算網路,請啟用連線到網際網路的埠以進行計算。 這可讓您下載腳本。

    在下列範例中,埠 2 已連線到網際網路,並用來啟用計算網路。 如果您發現先前的步驟中不需要 Kubernetes,您可以略過 Kubernetes 節點 IP 和外部服務 IP 指派。

    Screenshot of the Compute pane for an Azure Stack Edge device. Compute settings for Port 2 are highlighted.

安裝自訂腳本擴充功能

視 VM 的作業系統而定,您可以安裝適用于 Windows 或 Linux 的自訂腳本擴充功能。

Windows 的自訂指令碼延伸模組

若要為裝置上執行的 VM 部署適用于 Windows 的自訂腳本擴充功能,請編輯 addCSExtWindowsVM.parameters.json 參數檔案,然後部署範本 addCSextensiontoVM.json

編輯參數檔案

addCSExtWindowsVM.parameters.json檔案會採用下列參數:

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "vmName": {
            "value": "<Name of VM>" 
        },
        "extensionName": {
            "value": "<Name of extension>" 
        },
        "publisher": {
            "value": "Microsoft.Compute" 
        },
        "type": {
            "value": "CustomScriptExtension" 
        },
        "typeHandlerVersion": {
            "value": "1.10" 
        },
        "settings": {
            "value": {
                "commandToExecute" : "<Command to execute>"
            }
        }
    }
}

提供 VM 名稱、擴充功能的名稱,以及您想要執行的命令。

以下是本文中使用的範例參數檔案。

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "vmName": {
            "value": "VM5" 
        },
        "extensionName": {
            "value": "CustomScriptExtension" 
        },
        "publisher": {
            "value": "Microsoft.Compute" 
        },
        "type": {
            "value": "CustomScriptExtension" 
        },
        "typeHandlerVersion": {
            "value": "1.10" 
        },
        "settings": {
            "value": {
                "commandToExecute" : "md C:\\Users\\Public\\Documents\\test"
            }
        }
    }
}

部署範本

部署範本 addCSextensiontoVM.json 。 此範本會將擴充功能部署至現有的 VM。 執行下列命令:

$templateFile = "<Path to addCSExtensiontoVM.json file>"
$templateParameterFile = "<Path to addCSExtWindowsVM.parameters.json file>"
$RGName = "<Resource group name>"
New-AzureRmResourceGroupDeployment -ResourceGroupName $RGName -TemplateFile $templateFile -TemplateParameterFile $templateParameterFile -Name "<Deployment name>"

注意

延伸模組部署是長時間執行的作業,需要大約 10 分鐘才能完成。

以下是範例輸出:

PS C:\WINDOWS\system32> $templateFile = "C:\12-09-2020\ExtensionTemplates\addCSExtensiontoVM.json"
PS C:\WINDOWS\system32> $templateParameterFile = "C:\12-09-2020\ExtensionTemplates\addCSExtWindowsVM.parameters.json"
PS C:\WINDOWS\system32> $RGName = "myasegpuvm1"
PS C:\WINDOWS\system32> New-AzureRmResourceGroupDeployment -ResourceGroupName $RGName -TemplateFile $templateFile -TemplateParameterFile $templateParameterFile -Name "deployment7"

DeploymentName          : deployment7
ResourceGroupName       : myasegpuvm1
ProvisioningState       : Succeeded
Timestamp               : 12/17/2020 10:07:44 PM
Mode                    : Incremental
TemplateLink            :
Parameters              :
                          Name             Type                       Value
                          ===============  =========================  ==========
                          vmName           String                     VM5
                          extensionName    String                     CustomScriptExtension
                          publisher        String                     Microsoft.Compute
                          type             String                     CustomScriptExtension
                          typeHandlerVersion  String                     1.10
                          settings         Object                     {
                            "commandToExecute": "md C:\\Users\\Public\\Documents\\test"
                          }

Outputs                 :
DeploymentDebugLogLevel :

PS C:\WINDOWS\system32>

追蹤部署

若要檢查指定 VM 的擴充功能部署狀態,請執行下列命令:

Get-AzureRmVMExtension -ResourceGroupName <Name of resource group> -VMName <Name of VM> -Name <Name of the extension>

以下是範例輸出:

PS C:\WINDOWS\system32> Get-AzureRmVMExtension -ResourceGroupName myasegpuvm1 -VMName VM5 -Name CustomScriptExtension

ResourceGroupName       : myasegpuvm1
VMName                  : VM5
Name                    : CustomScriptExtension
Location                : dbelocal
Etag                    : null
Publisher               : Microsoft.Compute
ExtensionType           : CustomScriptExtension
TypeHandlerVersion      : 1.10
Id                      : /subscriptions/947b3cfd-7a1b-4a90-7cc5-e52caf221332/resourceGroups/myasegpuvm1/providers/Microsoft.Compute/virtualMachines/VM5/extensions/CustomScriptExtension
PublicSettings          : {
                            "commandToExecute": "md C:\\Users\\Public\\Documents\\test"
                          }
ProtectedSettings       :
ProvisioningState       : Creating
Statuses                :
SubStatuses             :
AutoUpgradeMinorVersion : True
ForceUpdateTag          :

PS C:\WINDOWS\system32>

注意

部署完成時,會 ProvisioningState 變更為 Succeeded

擴充功能輸出會記錄至目標虛擬機器上下列資料夾中的檔案。

C:\WindowsAzure\Logs\Plugins\Microsoft.Compute.CustomScriptExtension

指定的檔案會下載到目標虛擬機器上的下列資料夾。

C:\Packages\Plugins\Microsoft.Compute.CustomScriptExtension\1.*\Downloads\<n>

其中 n 是十進位整數,可能會在延伸模組的執行之間變更。 1.* 值符合延伸模組的實際目前 typeHandlerVersion 值。 例如,這個實例中的實際目錄是 C:\Packages\Plugins\Microsoft.Compute.CustomScriptExtension\1.10.9\Downloads\0

在此實例中,針對自訂延伸模組執行的命令是: md C:\\Users\\Public\\Documents\\test 。 成功安裝擴充功能時,您可以確認已在命令中指定路徑的 VM 中建立目錄。

Linux 的自訂指令碼擴充功能

若要為裝置上執行的 VM 部署適用于 Windows 的自訂腳本擴充功能,請編輯 addCSExtLinuxVM.parameters.json 參數檔案,然後部署範本 addCSExtensiontoVM.json

編輯參數檔案

addCSExtLinuxVM.parameters.json檔案會採用下列參數:

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "vmName": {
            "value": "<Name of your VM>" 
        },
        "extensionName": {
            "value": "<Name of your extension>" 
        },
        "publisher": {
            "value": "Microsoft.Azure.Extensions" 
        },
        "type": {
            "value": "CustomScript" 
        },
        "typeHandlerVersion": {
            "value": "2.0" 
        },
        "settings": {
            "value": {
                "commandToExecute" : "<Command to execute>"
            }
        }
    }
}

提供 VM 名稱、擴充功能的名稱,以及您想要執行的命令。

以下是本文中使用的範例參數檔案:

$templateFile = "<Path to addCSExtensionToVM.json file>"
$templateParameterFile = "<Path to addCSExtLinuxVM.parameters.json file>"
$RGName = "<Resource group name>"
New-AzureRmResourceGroupDeployment -ResourceGroupName $RGName -TemplateFile $templateFile -TemplateParameterFile $templateParameterFile -Name "<Deployment name>"

注意

延伸模組部署是長時間執行的作業,需要大約 10 分鐘才能完成。

以下是範例輸出:

PS C:\WINDOWS\system32> $templateFile = "C:\12-09-2020\ExtensionTemplates\addCSExtensionToVM.json"
PS C:\WINDOWS\system32> $templateParameterFile = "C:\12-09-2020\ExtensionTemplates\addCSExtLinuxVM.parameters.json"
PS C:\WINDOWS\system32> $RGName = "myasegpuvm1"
PS C:\WINDOWS\system32> New-AzureRmResourceGroupDeployment -ResourceGroupName $RGName -TemplateFile $templateFile -TemplateParameterFile $templateParameterFile -Name "deployment99"

DeploymentName          : deployment99
ResourceGroupName       : myasegpuvm1
ProvisioningState       : Succeeded
Timestamp               : 12/18/2020 1:55:23 AM
Mode                    : Incremental
TemplateLink            :
Parameters              :
                          Name             Type                       Value
                          ===============  =========================  ==========
                          vmName           String                     VM6
                          extensionName    String                     LinuxCustomScriptExtension
                          publisher        String                     Microsoft.Azure.Extensions
                          type             String                     CustomScript
                          typeHandlerVersion  String                     2.0
                          settings         Object                     {
                            "commandToExecute": "sudo echo 'some text' >> /home/Administrator/file2.txt"
                          }

Outputs                 :
DeploymentDebugLogLevel :

PS C:\WINDOWS\system32>

commandToExecute已設定為在 /home/Administrator 目錄中建立檔案 file2.txt ,而檔案的內容為 some text 。 在此情況下,您可以確認檔案是在指定的路徑中建立的。

Administrator@VM6:~$ dir
file2.txt
Administrator@VM6:~$ cat file2.txt
some text
Administrator@VM6:

追蹤部署狀態

範本部署是長時間執行的作業。 若要檢查指定 VM 的擴充功能部署狀態,請開啟另一個 PowerShell 會話(以系統管理員身分執行)。 執行以下命令:

Get-AzureRmVMExtension -ResourceGroupName myResourceGroup -VMName <VM Name> -Name <Extension Name>

以下是範例輸出:

PS C:\WINDOWS\system32> Get-AzureRmVMExtension -ResourceGroupName myasegpuvm1 -VMName VM5 -Name CustomScriptExtension

ResourceGroupName       : myasegpuvm1
VMName                  : VM5
Name                    : CustomScriptExtension
Location                : dbelocal
Etag                    : null
Publisher               : Microsoft.Compute
ExtensionType           : CustomScriptExtension
TypeHandlerVersion      : 1.10
Id                      : /subscriptions/947b3cfd-7a1b-4a90-7cc5-e52caf221332/resourceGroups/myasegpuvm1/providers/Microsoft.Compute/virtualMachines/VM5/extensions/CustomScriptExtension
PublicSettings          : {
                            "commandToExecute": "md C:\\Users\\Public\\Documents\\test"
                          }
ProtectedSettings       :
ProvisioningState       : Creating
Statuses                :
SubStatuses             :
AutoUpgradeMinorVersion : True
ForceUpdateTag          :

PS C:\WINDOWS\system32>

注意

部署完成時,會 ProvisioningState 變更為 Succeeded

延伸模組執行輸出會記錄到下列檔案: /var/lib/waagent/custom-script/download/0/

移除自訂腳本擴充功能

若要移除自訂腳本擴充功能,請使用下列命令:

Remove-AzureRmVMExtension -ResourceGroupName <Resource group name> -VMName <VM name> -Name <Extension name>

以下是範例輸出:

PS C:\WINDOWS\system32> Remove-AzureRmVMExtension -ResourceGroupName myasegpuvm1 -VMName VM6 -Name LinuxCustomScriptExtension
Virtual machine extension removal operation
This cmdlet will remove the specified virtual machine extension. Do you want to continue?
[Y] Yes  [N] No  [S] Suspend  [?] Help (default is "Y"): Yes
RequestId IsSuccessStatusCode StatusCode ReasonPhrase
--------- ------------------- ---------- ------------
                         True         OK OK

下一步

Azure Resource Manager Cmdlet