使用 Azure 资源管理器开发 Azure Stack Hub 的模板Develop templates for Azure Stack Hub with Azure Resource Manager

开发应用时,请务必确保模板可在 Azure 和 Azure Stack Hub 之间移植。As you develop your app, it is important to have template portability between Azure and Azure Stack Hub. 本文提供了有关开发 Azure 资源管理器模板的注意事项。This article provides considerations for developing Azure Resource Manager templates. 使用这些模板,可以在 Azure 中创建应用原型并测试部署,而无需访问 Azure Stack Hub 环境。With these templates, you can prototype your app and test deployment in Azure without access to an Azure Stack Hub environment.

资源提供程序可用性Resource provider availability

计划部署的模板只能使用 Azure Stack Hub 中已经可用或处于预览状态的 Azure 服务。The template you plan to deploy must only use Azure services that are already available, or in preview, in Azure Stack Hub.

公共命名空间Public namespaces

由于 Azure Stack Hub 托管在数据中心中,它的服务终结点命名空间与 Azure 公有云不同。Because Azure Stack Hub is hosted in your datacenter, it has different service endpoint namespaces than the Azure public cloud. 因此,如果尝试将 Azure 资源管理器模板部署到 Azure Stack Hub,这些模板中的硬编码公共终结点会失败。As a result, hard-coded public endpoints in Azure Resource Manager templates fail when you try to deploy them to Azure Stack Hub. 可以使用 referenceconcatenate 函数动态构建服务终结点,以便在部署期间从资源提供程序检索值。You can dynamically build service endpoints using the reference and concatenate functions to retrieve values from the resource provider during deployment. 例如,不需要在模板中硬编码 blob.core.chinacloudapi.cn,检索 primaryEndpoints.blob 即可动态设置 osDisk.URI 终结点:For example, instead of hard-coding blob.core.chinacloudapi.cn in your template, retrieve the primaryEndpoints.blob to dynamically set the osDisk.URI endpoint:

"osDisk": {"name": "osdisk","vhd": {"uri":
"[concat(reference(concat('Microsoft.Storage/storageAccounts/', variables('storageAccountName')), '2015-06-15').primaryEndpoints.blob, variables('vmStorageAccountContainerName'),
 '/',variables('OSDiskName'),'.vhd')]"}}

API 版本控制API versioning

Azure 服务版本在 Azure 和 Azure Stack Hub 之间可能有所不同。Azure service versions may differ between Azure and Azure Stack Hub. 每个资源都需要有 apiVersion 属性,用于定义所提供的功能。Each resource requires the apiVersion attribute, which defines the capabilities offered. 为了确保 API 版本在 Azure Stack Hub 中兼容,以下 API 版本对于每个资源提供程序有效:To ensure API version compatibility in Azure Stack Hub, the following API versions are valid for each resource provider:

资源提供程序Resource Provider apiVersionapiVersion
计算Compute 2015-06-152015-06-15
网络Network 2015-06-152015-05-01-preview2015-06-15, 2015-05-01-preview
存储Storage 2016-01-012015-06-152015-05-01-preview2016-01-01, 2015-06-15, 2015-05-01-preview
KeyVaultKeyVault 2015-06-012015-06-01
应用服务App Service 2015-08-012015-08-01

模板函数Template functions

Azure 资源管理器函数提供生成动态模板所需的功能。Azure Resource Manager functions provide the capabilities required to build dynamic templates. 例如,可以对如下任务使用函数:As an example, you can use functions for tasks such as:

  • 连接或修整字符串。Concatenating or trimming strings.
  • 引用其他资源的值。Referencing values from other resources.
  • 对资源进行迭代以部署多个实例。Iterating on resources to deploy multiple instances.

以下函数在 Azure Stack Hub 中不可用:These functions are not available in Azure Stack Hub:

  • 跳过Skip
  • TakeTake

资源位置Resource location

在部署过程中,Azure 资源管理器模板使用 location 属性来放置资源。Azure Resource Manager templates use a location attribute to place resources during deployment. 在 Azure 中,位置是指中国北部或中国东部之类的区域。In Azure, locations refer to a region such as China North or China East. 在 Azure Stack Hub 中,位置有所不同,因为 Azure Stack Hub 在数据中心内。In Azure Stack Hub, locations are different because Azure Stack Hub is in your datacenter. 若要确保模板可在 Azure 和 Azure Stack Hub 之间转移,在部署单个资源时应引用资源组位置。To ensure templates are transferable between Azure and Azure Stack Hub, you should reference the resource group location as you deploy individual resources. 可以使用 [resourceGroup().Location] 执行此操作,以确保所有资源均继承资源组位置。You can do this using [resourceGroup().Location] to ensure all resources inherit the resource group location. 以下代码是在部署存储帐户时使用此函数的示例:The following code is an example of using this function while deploying a storage account:

"resources": [
{
  "name": "[variables('storageAccountName')]",
  "type": "Microsoft.Storage/storageAccounts",
  "apiVersion": "[variables('apiVersionStorage')]",
  "location": "[resourceGroup().location]",
  "comments": "This storage account is used to store the VM disks",
  "properties": {
  "accountType": "Standard_GRS"
  }
}
]

后续步骤Next steps