使用ARM模板在现有资源组创建Azure容器注册表遇语法报错
错误原因
这个语法报错的触发原因和配套逻辑问题如下:
- 直接触发语法校验失败的点:你定义的嵌套部署资源
nestedDeployment下,properties.template节点里写了裸占位文本resource-group-resources,不符合JSON语法规则:JSON对象内的所有成员名必须用双引号包裹为字符串,直接写无引号的裸文本会直接抛出Member name should be a string in double quotes错误。 - 模板逻辑设计问题:你把Azure容器注册表(ACR)资源定义在了根模板的资源数组中,根模板的资源默认会部署到执行部署命令时指定的资源组,和嵌套部署里标注的
internet-suite资源组没有关联,就算修完语法错误,ACR也不会部署到你指定的目标资源组。
修正方案
如果你的需求仅为把ACR部署到已有的internet-suite资源组,完全不需要编写嵌套部署,直接删除冗余的嵌套部署资源即可,修正后的最简模板如下:
{ "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", "contentVersion": "1.0.0.0", "parameters": {}, "functions": [], "variables": {}, "resources": [ { "name": "containerRegistry1", "type": "Microsoft.ContainerRegistry/registries", "apiVersion": "2019-05-01", "location": "West Europe", "sku": { "name": "Basic" }, "properties": { "adminUserEnabled": true } } ], "outputs": {} }
部署时直接指定目标资源组为internet-suite即可,常用部署命令参考:
- Azure CLI:
az deployment group create --resource-group internet-suite --template-file <你的模板文件本地路径> - Azure PowerShell:
New-AzResourceGroupDeployment -ResourceGroupName internet-suite -TemplateFile <你的模板文件本地路径>
如果你确实需要在单个模板中实现跨资源组部署(比如同时给多个资源组下发不同资源),才需要保留嵌套部署结构,此时要把ACR资源移动到嵌套部署的内层模板资源数组中,删掉无效的占位文本,修正后的跨资源组版本模板如下:
{ "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", "contentVersion": "1.0.0.0", "parameters": {}, "functions": [], "variables": {}, "resources": [ { "type": "Microsoft.Resources/deployments", "apiVersion": "2021-04-01", "name": "nestedDeployment", "resourceGroup": "internet-suite", "properties": { "mode": "Incremental", "template": { "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", "contentVersion": "1.0.0.0", "resources": [ { "name": "containerRegistry1", "type": "Microsoft.ContainerRegistry/registries", "apiVersion": "2019-05-01", "location": "West Europe", "sku": { "name": "Basic" }, "properties": { "adminUserEnabled": true } } ] } } } ], "outputs": {} }
额外注意事项
- Azure容器注册表的名称是全局唯一的,如果你使用的
containerRegistry1已经被其他Azure用户占用,部署时会抛出名称冲突错误,需要替换为自定义的唯一名称。 - 嵌套部署的内层模板必须补全
$schema和contentVersion字段,否则会触发模板校验警告。
内容的提问来源于stack exchange,提问作者mightycode Newton
相关产品推荐
相关产品推荐

