【Azure 环境】部署ARM Linked Template时候 Blob SAS Token不能正常工作 - LuBu0505/My-Code GitHub Wiki

问题描述

通过AZ CLI指令部署ARM模板,使用 Linked Template ,并且把模板文件存放到Storage Account中。然后调用 az deployment group create  指令部署模板。

部署指令:

$sasToken =  '  ‘

az deployment group create   --name DeployLinkedTemplate   --resource-group "armtest-rg"  --template-uri "https://.blob.core.chinacloudapi.cn/arm/azuredeploy.json" --parameters projectName=armtestpjname    --query-string $sasToken   --verbose

但是,在Windows环境中执行,会遇见如下错误:

Unable to retrieve url https://.blob.core.chinacloudapi.cn/arm/azuredeploy.json?sp=r 'st' is not recognized as an internal or external command, operable program or batch file. 'se' is not recognized as an internal or external command, operable program or batch file. 'spr' is not recognized as an internal or external command, operable program or batch file. 'sv' is not recognized as an internal or external command, operable program or batch file. 'sr' is not recognized as an internal or external command, operable program or batch file. 'sig' is not recognized as an internal or external command, operable program or batch file.

问题解答

需要使用单引号来包含SAS Token的内容,并且由于内容中包含了&符号,而 “&” 在 CMD/Powershell中是与运算符,需要使用双引号进行转移 [   “&”   ],  所以需要把包含&符号的字符串修改为 

$sasToken =  'sp=r"&"st=2025-02-14T07:46:18Z"&"se=2025-02-14T15:46:18Z"&"spr=https"&"sv=2022-11-02"&"sr=c"&"sig=xxxxxxxxxxxxxxxxxxxxxxxxxxx'

修改之后,就能成功执行! image.png

附录:存放Linked Template文件的存储账号不能启用防火墙

原因在于当执行 az deployment group create 指令时,会由 ARM Provider 的运行环境来获取存储在Blob中的模板文件,由于ARM Provider主机的IP地址由IPv4,IPv6,且资源如果与存储账号在同一个区域时,会由Azure内部的骨干网直接内网访问,没有公网IP地址。

当为存储账号启用防火墙后,因为如下两个原因无法访问模板文件:

一:防火墙白名单中无法配置IPv6地址,内网地址

二:ARM Provider资源不支持VNET配置且由Azure管理,该资源没有被加入到Storage Account受信任的访问列表中 image.png

参考资料

  1. az deployment does not work with SAS tokens for template-uri : https://stackoverflow.com/questions/53337127/az-deployment-does-not-work-with-sas-tokens-for-template-uri
  2. 无法链接到位于 Azure 存储防火墙后面的存储帐户中的模板 : https://docs.azure.cn/zh-cn/azure-resource-manager/templates/linked-templates?tabs=azure-powershell#securing-an-external-template
  3. Trusted access for resources registered in your Microsoft Entra tenant: https://learn.microsoft.com/en-us/azure/storage/common/storage-network-security?tabs=azure-portal#trusted-access-for-resources-registered-in-your-microsoft-entra-tenant
  4. Azure IP Ranges and Service Tags – China Cloud : https://www.microsoft.com/en-us/download/details.aspx?id=57062

当在复杂的环境中面临问题,格物之道需:浊而静之徐清,安以动之徐生。 云中,恰是如此!

⚠️ **GitHub.com Fallback** ⚠️