在 Azure Stack Hub 上安装 Azure CLIInstall Azure CLI on Azure Stack Hub

可以使用 Windows 或 Linux 计算机安装 Azure CLI 以管理 Azure Stack Hub。You can install the Azure CLI to manage Azure Stack Hub with a Windows or Linux machines. 本文将指导你完成安装和设置 Azure CLI 的步骤。This article walks you through the steps of installing and setting up Azure CLI.

安装 Azure CLIInstall Azure CLI

  1. 登录到开发工作站并安装 CLI。Sign in to your development workstation and install CLI. Azure Stack Hub 需要 Azure CLI 2.0 版或更高版本。Azure Stack Hub requires version 2.0 or later of Azure CLI.

  2. 可以使用安装 Azure CLI 一文中所述的步骤安装 CLI。You can install the CLI by using the steps described in the Install the Azure CLI article.

  3. 若要验证安装是否成功,请打开终端或命令提示符窗口,并运行以下命令:To verify whether the installation was successful, open a terminal or command prompt window and run the following command:

    az --version
    

    应会看到 Azure CLI 的版本,以及计算机上安装的其他依赖库。You should see the version of Azure CLI and other dependent libraries that are installed on your computer.

    Azure Stack Hub Python 位置上的 Azure CLI

  4. 记下 CLI 的 Python 位置。Make a note of the CLI's Python location. 如果正在运行 ASDK,则需要使用此位置来添加证书。If you're running the ASDK, you need to use this location to add your certificate. 有关如何设置证书以在 ASDK 上安装 CLI 的说明,请参阅在 Azure Stack 开发工具包中为 Azure CLI 设置证书For instructions on setting up certificates for installing the CLI on the ASDK, see Setting up certificates for Azure CLI on Azure Stack Development Kit.

使用 Azure CLI 进行连接Connect with Azure CLI

如果使用 Azure AD 作为标识管理服务,并在 Windows 计算机上使用 CLI,可以参考本部分完成 CLI 设置过程。This section walks you through setting up CLI if you're using Azure AD as your identity management service, and are using CLI on a Windows machine.

连接到 Azure Stack HubConnect to Azure Stack Hub

  1. 如果使用的是 ASDK,请信任 Azure Stack Hub CA 根证书。If you are using the ASDK, trust the Azure Stack Hub CA root certificate. 有关说明,请参阅信任证书For instruction, see Trust the certificate.

  2. 运行 az cloud register 命令注册 Azure Stack Hub 环境。Register your Azure Stack Hub environment by running the az cloud register command.

  3. 注册环境。Register your environment. 在运行 az cloud register 时使用以下参数:Use the following parameters when running az cloud register:

    Value 示例Example 说明Description
    环境名称Environment name AzureStackUserAzureStackUser 对于用户环境,请使用 AzureStackUserUse AzureStackUser for the user environment. 如果你是操作员,请指定 AzureStackAdminIf you're operator, specify AzureStackAdmin.
    资源管理器终结点Resource Manager endpoint https://management.contoso.partner.onmschina.cn ASDK 中的 ResourceManagerUrl 为:https://management.local.azurestack.external/集成系统中的 ResourceManagerUrl 为:https://management.<region>.<fqdn>/ 如果对集成系统终结点有疑问,请与云操作员联系。The ResourceManagerUrl in the ASDK is: https://management.local.azurestack.external/ The ResourceManagerUrl in integrated systems is: https://management.<region>.<fqdn>/ If you have a question about the integrated system endpoint, contact your cloud operator.
    存储终结点Storage endpoint local.contoso.partner.onmschina.cnlocal.contoso.partner.onmschina.cn local.azurestack.external 适用于 ASDK。local.azurestack.external is for the ASDK. 对于集成系统,请使用适用于系统的终结点。For an integrated system, use an endpoint for your system.
    KeyVault 后缀Keyvault suffix .vault.contoso.partner.onmschina.cn.vault.contoso.partner.onmschina.cn .vault.local.azurestack.external 适用于 ASDK。.vault.local.azurestack.external is for the ASDK. 对于集成系统,请使用适用于系统的终结点。For an integrated system, use an endpoint for your system.
    终结点 active directory graph 资源 IDEndpoint active directory graph resource ID https://graph.chinacloudapi.cn/ Active Directory 资源 ID。The Active Directory resource ID.
    az cloud register `
        -n <environmentname> `
        --endpoint-resource-manager "https://management.<region>.<fqdn>" `
        --suffix-storage-endpoint "<fqdn>" `
        --suffix-keyvault-dns ".vault.<fqdn>" `
        --endpoint-active-directory-graph-resource-id "https://graph.chinacloudapi.cn/"
    

    可以在 Azure CLI 参考文档中找到 register 命令的参考。You can find a reference for the register command in the Azure CLI reference documentation.

  4. 使用以下命令设置活动环境。Set the active environment by using the following commands.

    az cloud set -n <environmentname>
    
  5. 将环境配置更新为使用 Azure Stack Hub 特定的 API 版本配置文件。Update your environment configuration to use the Azure Stack Hub specific API version profile. 若要更新配置,请运行以下命令:To update the configuration, run the following command:

    az cloud update --profile 2019-03-01-hybrid
    
  6. 使用 az login 命令登录到 Azure Stack Hub 环境。Sign in to your Azure Stack Hub environment by using the az login command.

    可以使用用户凭据或云运营商提供的服务主体 (SPN) 登录到 Azure Stack Hub 环境。You can sign in to the Azure Stack Hub environment using your user credentials, or with a service principal (SPN) provided to you by your cloud operator.

    • 以用户身份登录:Sign in as a user:

      可以直接在 az login 命令中指定用户名和密码,或使用浏览器进行身份验证。You can either specify the username and password directly within the az login command, or authenticate by using a browser. 如果帐户已启用多重身份验证,则必须采用后一种方法。You must do the latter if your account has multi-factor authentication enabled:

      az login -u "user@contoso.partner.onmschina.cn" -p 'Password123!' --tenant contoso.partner.onmschina.cn
      

      备注

      如果用户帐户已启用多重身份验证,请使用不带 -u 参数的 az login 命令。If your user account has multi-factor authentication enabled, use the az login command without providing the -u parameter. 运行此命令会提供一个 URL 以及身份验证时必须使用的代码。Running this command gives you a URL and a code that you must use to authenticate.

    • 以服务主体身份登录:Sign in as a service principal:

      在登录之前,请通过 Azure 门户或 CLI 创建一个服务主体,并为其分配角色。Before you sign in, create a service principal through the Azure portal or CLI and assign it a role. 接下来,使用以下命令登录:Now, sign in by using the following command:

      az login `
        --tenant <Azure Active Directory Tenant name. `
                  For example: myazurestack.partner.onmschina.cn> `
      --service-principal `
        -u <Application Id of the Service Principal> `
        -p <Key generated for the Service Principal>
      
  7. 验证环境是否设置正确,以及环境是否为活动云。Verify that your environment is set correctly and that your environment is the active cloud.

        az cloud list --output table
    

应当看到环境被列出,并且“IsActive”为 trueYou should see that your environment is listed and IsActive is true. 例如:For example:

IsActive    Name               Profile
----------  -----------------  -----------------
False       AzureCloud         2019-03-01-hybrid
False       AzureChinaCloud    latest
False       AzureUSGovernment  latest
False       AzureGermanCloud   latest
True        AzureStackUser     2019-03-01-hybrid

测试连接Test the connectivity

完成所有设置后,使用 CLI 在 Azure Stack Hub 中创建资源。With everything set up, use CLI to create resources within Azure Stack Hub. 例如,可以创建应用的资源组并添加 VM。For example, you can create a resource group for an app and add a VM. 使用以下命令创建名为“MyResourceGroup”的资源组:Use the following command to create a resource group named "MyResourceGroup":

az group create -n MyResourceGroup -l local

如果成功创建了资源组,则上述命令会输出新建资源的以下属性:If the resource group is created successfully, the previous command outputs the following properties of the newly created resource:

资源组创建输出

后续步骤Next steps