ARM嵌套模板:部署带有嵌套模板的模板时,无法正确填充_artifactLocation参数

时间:2019-03-12 00:26:15

标签: azure azure-resource-manager

我试图弄清楚嵌套模板是如何工作的,我有以下模板。我正在尝试使用VS部署机制从VS进行部署:

  1. 右键单击项目>部署>新建
  2. “工件存储帐户”字段中预先填充了“自动创建存储帐户”,我将其保留为这样
  3. 单击“部署”按钮

如果您在变量的HelloWorldParent.json模板中查看,您将看到两个变量“ nestedTemplateUri”和“ nestedTemplateUriWithBlobContainerName”。

据我了解,“ nestedTemplateUri”应包含“ blob容器名称”,但事实并非如此。

如果我使用资源>属性> templateLink>“ uri”进行部署:“ [variables('nestedTemplateUri')]”

  • 部署失败并显示:
  

错误:代码= InvalidContentLink; Message =无法下载部署   来自的内容   'https://********.blob.core.windows.net/NestedTemplates/HelloWorld.json?sv = 2017-07-29&sr = c&sig = ZCJAoOdp08qDWxbzKbXSZzX1VBCf7%2FNSt4aIznFCTPQ%3D&se = 2019-03-12T03: 39:09Z&sp = r'

  • 创建存储帐户,上传模板,参数和PS1脚本
  • 未在资源组/部署中创建新的部署

如果我使用资源>属性> templateLink>“ uri”进行部署:“ [variables('nestedTemplateUriWithBlobContainerName')]”

  • 部署成功。

有什么主意吗?非常感谢您的帮助!

HelloWorldParent.json

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "_artifactsLocation": {
      "type": "string",
      "metadata": {
        "description": "The base URI where artifacts required by this template are located including a trailing '/'"
      }
    },
    "_artifactsLocationSasToken": {
      "type": "securestring",
      "metadata": {
        "description": "The sasToken required to access _artifactsLocation.  When the template is deployed using the accompanying scripts, a sasToken will be automatically generated. Use the defaultValue if the staging location is not secured."
      },
      "defaultValue": ""
    }
  },
  "variables": {
    "blobContainerName": "[concat(resourceGroup().name, '-stageartifacts/')]",
    "nestedTemplateUriWithBlobContainerName": "[uri(parameters('_artifactsLocation'), concat(variables('blobContainerName'), 'NestedTemplates/HelloWorld.json', parameters('_artifactsLocationSasToken')))]",
    "nestedTemplateUri": "[uri(parameters('_artifactsLocation'), concat('NestedTemplates/HelloWorld.json', parameters('_artifactsLocationSasToken')))]"
  },
  "resources": [
    {
      "apiVersion": "2017-05-10",
      "name": "linkedTemplate",
      "type": "Microsoft.Resources/deployments",
      "properties": {
        "mode": "incremental",
        "templateLink": {
          "uri": "[variables('nestedTemplateUri')]",
          "contentVersion": "1.0.0.0"
        }
      }
    }
  ],
  "outputs": {
    "messageFromLinkedTemplate": {
      "type": "string",
      "value": "[reference('linkedTemplate').outputs.greetingMessage.value]"
    },
    "_artifactsLocation": {
      "type": "string",
      "value": "[parameters('_artifactsLocation')]"
    }
  }
}

HelloWorldParent.parameters.json

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
  }
}

NestedTemplates / HelloWorld.json

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {},
  "variables": {},
  "resources": [],
  "outputs": {
    "greetingMessage": {
      "value": "Hello World (1)",
      "type": "string"
    }
  }
}

1 个答案:

答案 0 :(得分:1)

不幸的是,VS在支持您的方案方面有点“过时”……问题是您使用的是URI函数,而_artifactsLocation没有斜杠。因此,您可以选择以下几种解决方法:

1)在VS中的PS1文件中,有一行看起来像这样:

$OptionalParameters[$ArtifactsLocationName] = $StorageAccount.Context.BlobEndPoint + $StorageContainerName

如果将其更改为此(添加尾随/):

$OptionalParameters[$ArtifactsLocationName] = $StorageAccount.Context.BlobEndPoint + $StorageContainerName + "/"

它应该可以工作-或者,您也可以使用以下脚本替换整个脚本:https://github.com/Azure/azure-quickstart-templates/blob/master/Deploy-AzureResourceGroup.ps1

请注意,如果您还有其他不带斜线的模板都可以使用,那么这将是一个重大突破。

2)使用concat()创建uri而不是uri()函数。您仍然必须知道是否有斜杠,但是可以在模板而不是PS1文件中完成此更改。

   "nestedTemplateUri": "[concat(parameters('_artifactsLocation'), '/NestedTemplates/HelloWorld.json', parameters('_artifactsLocationSasToken'))]"

应该都可以。