將清查標記套用至已啟用 Azure Arc 的伺服器Apply inventory tagging to Azure Arc enabled servers

本文提供如何使用已啟用 Azure Arc 之伺服器的指引,以提供跨混合式多重雲端和內部部署環境的伺服器清查管理功能。This article provides guidance on how to use Azure Arc enabled servers to provide server inventory management capabilities across hybrid multicloud and on-premises environments.

啟用 azure Arc 的伺服器可讓您在公司網路或其他雲端提供者上,管理裝載于 Azure 外部的 Windows 和 Linux 機器。Azure Arc enabled servers enable you to manage your Windows and Linux machines hosted outside of Azure on your corporate network or other cloud providers. 這類似于您在 Azure 中管理原生虛擬機器的方式。This is similar to how you manage native virtual machines in Azure. 混合式機器連線到 Azure 時就會變成已連線的機器,並且視為 Azure 中的資源。When a hybrid machine is connected to Azure, it becomes a connected machine and is treated as a resource in Azure. 每個連線的機器都有資源識別碼,可作為訂用帳戶內資源群組的一部分來管理,並可從標準的 Azure 結構(例如 Azure 原則和套用標記)獲益。Each connected machine has a resource ID, is managed as part of a resource group inside a subscription, and benefits from standard Azure constructs such as Azure Policy and applying tags. 使用 Azure 作為管理引擎輕鬆地組織及管理伺服器清查的能力,可大幅降低系統管理的複雜度,並為混合式和多重雲端環境提供一致的策略。The ability to easily organize and manage server inventory using Azure as a management engine greatly reduces administrative complexity and provides a consistent strategy for hybrid and multicloud environments.

下列程式使用 Resource Graph ExplorerAzure CLI 來示範如何從 Azure 中的單一窗格,在多個雲端上標記和查詢伺服器清查。The following procedures use Resource Graph Explorer and Azure CLI to demonstrate how to tag and query server inventory across multiple clouds from a single pane of glass in Azure.

重要

本文中的程式假設您已經部署 Vm,或是在內部部署或其他雲端上執行的伺服器,而且您已將這些 Vm 連線到 Azure Arc。如果您尚未這麼做,下列資訊可協助您將這項工作自動化。The procedures in this article assumes you've already deployed VMs, or servers that are running on-premises or on other clouds, and you have connected them to Azure Arc. If you haven't, the following information can help you automate this.

必要條件Prerequisites

  1. 複製 Azure Arc Jumpstart 存放庫。Clone the Azure Arc Jumpstart repository.

    git clone https://github.com/microsoft/azure_arc
    
  2. 安裝或更新 AZURE CLI 至2.7 版或更新版本Install or update Azure CLI to version 2.7 or later. 使用下列命令來檢查目前安裝的版本。Use the following command to check the current installed version.

    az --version
    

確認您的 Azure Arc 連線伺服器已準備好進行標記Verify that your Azure Arc connected servers are ready for tagging

使用 Resource Graph Explorer 來查詢和查看 Azure 中的資源。Use Resource Graph Explorer to query and view resources in Azure.

  1. 在 Azure 入口網站的頂端搜尋列中輸入 Resource Graph Explorer ,然後選取它。Enter Resource Graph Explorer in the top search bar in the Azure portal and select it.

    Azure 入口網站中 Resource Graph Explorer 的螢幕擷取畫面。

  2. 在查詢視窗中,輸入下列查詢,然後選取 [ 執行查詢In the query window, enter the following query, and then select Run Query:

    Resources
    | where type =~ 'Microsoft.HybridCompute/machines'
    
  3. 如果您已正確建立啟用 Azure Arc 的伺服器,這些伺服器會列在 Resource Graph Explorer 的結果窗格中。If you have correctly created Azure Arc enabled servers, they are listed in the results pane of Resource Graph Explorer. 您也可以從 Azure 入口網站查看已啟用 Azure Arc 的服務。You can also view the Azure Arc enabled serves from the Azure portal.

    Resource Graph Explorer 查詢的螢幕擷取畫面。

    Azure 入口網站中已啟用 Azure Arc 之伺服器詳細資料的螢幕擷取畫面。

建立基本的 Azure 標記分類法Create a basic Azure tag taxonomy

開啟 Azure CLI 並執行下列命令,以建立基本的分類結構,讓您可以輕鬆地查詢和報表服務器資源的裝載位置 (無論在 Azure、AWS、GCP 或內部部署) 。Open Azure CLI and run the following commands to create a basic taxonomy structure that lets you easily query and report on where your server resources are hosted (whether in Azure, AWS, GCP, or on-premises). 如需建立標記分類法的詳細指引,請參閱 資源命名和標記決策指南For more guidance on building out a tag taxonomy please review the resource naming and tagging decision guide.

az tag create --name "Hosting Platform"
az tag add-value --name "Hosting Platform" --value "Azure"
az tag add-value --name "Hosting Platform" --value "AWS"
az tag add-value --name "Hosting Platform" --value "GCP"
az tag add-value --name "Hosting Platform" --value "On-premises"

' Az tag create ' 命令輸出的螢幕擷取畫面。

標記您的 Azure Arc 資源Tag your Azure Arc resources

在您建立基本的分類結構之後,請將標記套用至已啟用 Azure Arc 的伺服器資源。After you've created a basic taxonomy structure, apply tags to the Azure Arc enabled server resources. 下列程式示範如何標記 AWS 和 GCP 中的資源。The following procedure demonstrates tagging resources in both AWS and GCP. 如果您只有其中一個提供者的資源,則可以跳至 AWS 或 GCP 的適當區段。If you only have resources in one of these providers, you can skip to the appropriate section for AWS or GCP.

標記已連線到 Azure Arc 的 AWS Ubuntu EC2 實例Tag the Azure Arc connected AWS Ubuntu EC2 instance

在 CLI 中,執行下列命令以將 Hosting Platform : AWS 標記套用至已啟用 Azure Arc 的 AWS 伺服器。In CLI, run the following commands to apply the Hosting Platform : AWS tag to your AWS Azure Arc enabled servers.

注意

如果您使用 Azure 教學課程中所述的方法來連接您的 AWS EC2 實例,則需要調整和的值, awsResourceGroup awsMachineName 以符合您環境的特定值。If you connected your AWS EC2 instances using a method other than the one described in the Azure tutorial, then you need to adjust the values for awsResourceGroup and awsMachineName to match values specific to your environment.

export awsResourceGroup="arc-aws-demo"
export awsMachineName="arc-aws-demo"
export awsMachineResourceId="$(az resource show --resource-group $awsResourceGroup --name $awsMachineName --resource-type "Microsoft.HybridCompute/machines" --query id)"
export awsMachineResourceId="$(echo $awsMachineResourceId | tr -d "\"" | tr -d '\r')"
az resource tag --ids $awsMachineResourceId --tags "Hosting Platform"="AWS"

「Az 資源標記」命令之一個輸出的螢幕擷取畫面。

標記已連線到 Azure Arc 的 GCP Ubuntu serverTag Azure Arc connected GCP Ubuntu server

在 CLI 中,執行下列命令以將 Hosting Platform : GCP 標記套用至已啟用 Azure Arc 的 GCP 伺服器。In CLI, run the following commands to apply the Hosting Platform : GCP tag to your GCP Azure Arc enabled servers.

注意

如果您使用相關的 Azure Arc Terraform 教學課程中所述的方法來連接您的 GCP 實例,則需要調整和的值, gcpResourceGroup gcpMachineName 以符合您環境的特定值。If you connected your GCP instances using a method other than the one described in the related Azure Arc Terraform tutorial, then you will need to adjust the values for gcpResourceGroup and gcpMachineName to match values specific to your environment.

export gcpResourceGroup="arc-gcp-demo"
export gcpMachineName="arc-gcp-demo"
export gcpMachineResourceId="$(az resource show --resource-group $gcpResourceGroup --name $gcpMachineName --resource-type "Microsoft.HybridCompute/machines" --query id)"
export gcpMachineResourceId="$(echo $gcpMachineResourceId | tr -d "\"" | tr -d '\r')"
az resource tag --resource-group $gcpResourceGroup --ids $gcpMachineResourceId --tags "Hosting Platform"="GCP"

' Az resource tag ' 命令的另一個輸出的螢幕擷取畫面。

使用 Resource Graph Explorer 依標記查詢資源Query resources by tag using Resource Graph Explorer

將標記套用至裝載于多個雲端的資源之後,請使用 Resource Graph Explorer 來查詢它們,並深入瞭解您的多重雲端環境。After you apply tags to the resources that are hosted in multiple clouds, use Resource Graph Explorer to query them and get insight into your multicloud landscape.

  1. 在查詢視窗中,輸入下列查詢︰In the query window, enter the following query:

    Resources
    | where type =~ 'Microsoft.HybridCompute/machines'
    | where isnotempty(tags['Hosting Platform'])
    | project name, location, resourceGroup, tags
    

    Resource Graph Explorer 查詢詳細資料的螢幕擷取畫面。

  2. 按一下 [ 執行查詢 ],然後選取 格式化的結果 切換。Click Run Query and then select the Formatted Results toggle. 如果正確完成,您應該會看到所有啟用 Azure Arc 的伺服器及其指派的 Hosting Platform 標記值。If done correctly, you should see all Azure Arc enabled servers and their assigned Hosting Platform tag values.

    Resource Graph Explorer 查詢結果的螢幕擷取畫面。

    我們也可以從 Azure 入口網站查看投射的伺服器上的標記。We can also view the tags on the projected servers from Azure portal.

    啟用 Azure Arc 之伺服器上一組標記的螢幕擷取畫面。

    啟用 Azure Arc 之伺服器上另一組標記的螢幕擷取畫面。

清除您的環境Clean up your environment

請完成下列步驟來清除您的環境。Complete the following steps to clean up your environment.

  1. 遵循每個指南的終止指示,從每個環境移除虛擬機器。Remove the virtual machines from each environment by following the teardown instructions from each guide.

  2. 藉由在 Azure CLI 中執行下列腳本,移除作為本指南一部分建立的標記。Remove tags created as part of this guide by executing the following script in Azure CLI.

    az tag remove-value --name "Hosting Platform" --value "Azure"
    az tag remove-value --name "Hosting Platform" --value "AWS"
    az tag remove-value --name "Hosting Platform" --value "GCP"
    az tag remove-value --name "Hosting Platform" --value "On-premises"
    az tag create --name "Hosting Platform"