你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

在 Bicep 配置文件中添加模块设置

在 bicepconfig.json 文件中,你可以为模块路径创建别名,并配置用于发布和还原模块的配置文件和凭据优先级。

本文介绍可用于处理 Bicep 模块的设置。

模块的别名

若要简化用于链接到模块的路径,可以在配置文件中创建别名。 别名会引用模块注册表或包含模板规格的资源组。

配置文件具有 moduleAliases 属性。 此属性包含你定义的所有别名。 在此属性下,别名会根据它们是引用注册表还是模板规格进行划分。

若要为“Bicep 注册表”创建别名,请添加 br 属性。 若要为“模板规格”添加别名,请使用 ts 属性。

{
  "moduleAliases": {
    "br": {
      <add-registry-aliases>
    },
    "ts": {
      <add-template-specs-aliases>
    }
  }
}

br 属性中,根据需要添加任意数量的别名。 为每个别名提供名称和以下属性:

  • 注册表(必需):注册表登录服务器名
  • modulePath(可选):存储模块的注册表存储库

ts 属性中,根据需要添加任意数量的别名。 为每个别名提供名称和以下属性:

  • 订阅(必需):托管模板规格的订阅 ID
  • resourceGroup(必需):包含模板规格的资源组名

下面的示例演示了一个配置文件,该文件定义了一个模块注册表的两个别名,以及一个包含模板规范的资源组的别名。

{
  "moduleAliases": {
    "br": {
      "ContosoRegistry": {
        "registry": "contosoregistry.azurecr.io"
      },
      "CoreModules": {
        "registry": "contosoregistry.azurecr.io",
        "modulePath": "bicep/modules/core"
      }
    },
    "ts": {
      "CoreSpecs": {
        "subscription": "00000000-0000-0000-0000-000000000000",
        "resourceGroup": "CoreSpecsRG"
      }
    }
  }
}

在模块引用中使用别名时,必须使用以下格式:

br/<alias>:<file>:<tag>
ts/<alias>:<file>:<tag>

为包含模块的文件夹或资源组定义别名,而不是为文件本身定义别名。 此文件名必须包括在对模块的引用中。

如果不使用别名,则可使用完整路径链接到注册表中的模块。

module stgModule 'br:contosoregistry.azurecr.io/bicep/modules/core/storage:v1' = {

如果使用别名,则可通过使用注册表的别名来简化链接。

module stgModule 'br/ContosoRegistry:bicep/modules/core/storage:v1' = {

或者,你可以通过使用指定注册表和模块路径的别名来简化链接。

module stgModule  'br/CoreModules:storage:v1' = {

对于模板规格,请使用以下代码:

module stgModule  'ts/CoreSpecs:storage:v1' = {

已为公共模块注册表预定义别名。 若要引用公共模块,可以使用以下格式:

br/public:<file>:<tag>

可以在 bicepconfig.json 文件中重写公共模块注册表别名定义:

{
  "moduleAliases": {
    "br": {
      "public": {
        "registry": "<your_module_registry>",
        "modulePath": "<optional_module_path>"
      }
    }
  }
}

配置配置文件和凭据

要将模块发布到专用模块注册表或要将外部模块还原到本地缓存,该帐户必须具有访问注册表的正确权限。 可在 Bicep 配置文件中配置 currentProfilecredentialPrecedence,以便对注册表进行身份验证。

{
  "cloud": {
    "currentProfile": "AzureCloud",
    "profiles": {
      "AzureCloud": {
        "resourceManagerEndpoint": "https://management.azure.com",
        "activeDirectoryAuthority": "https://login.microsoftonline.com"
      },
      "AzureChinaCloud": {
        "resourceManagerEndpoint": "https://management.chinacloudapi.cn",
        "activeDirectoryAuthority": "https://login.chinacloudapi.cn"
      },
      "AzureUSGovernment": {
        "resourceManagerEndpoint": "https://management.usgovcloudapi.net",
        "activeDirectoryAuthority": "https://login.microsoftonline.us"
      }
    },
    "credentialPrecedence": [
      "AzureCLI",
      "AzurePowerShell"
    ]
  }
}

可用的配置文件有:

  • AzureCloud
  • AzureChinaCloud
  • AzureUSGovernment

默认情况下,Bicep 使用 AzureCloud 配置文件以及已在 Azure CLI 或 Azure PowerShell 中通过身份验证的用户的凭据。 你可以自定义这些配置文件,也可以为本地环境添加新的配置文件。 如果要将模块发布或还原到国家/地区云环境(例如 AzureUSGovernment),必须设置 "currentProfile": "AzureUSGovernment",即使已在 Azure CLI 中选择了该云配置文件也是如此。 Bicep 无法根据 Azure CLI 设置自动确定当前云配置文件。

Bicep 使用 Azure.Identity SDK 进行身份验证。 可用凭据类型为:

注意

vscode 中的 Bicep deploy 命令使用 Azure 帐户扩展进行身份验证。 它不使用 bicepconfig.json 中的云配置文件。

后续步骤