简体   繁体   English

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

[英]ARM nested templates: _artifactLocation parameter is not populated correctly when deploying template with nested templates

I am trying to figure out how nested templates work and I have the below templates. 我试图弄清楚嵌套模板是如何工作的,我有以下模板。 I am trying to deploy from VS using the VS deploy mechanism: 我正在尝试使用VS部署机制从VS进行部署:

  1. Right click on the project > Deploy > New 右键单击项目>部署>新建
  2. "Artifact storage account" field is prepopulated with "Automatically create a storage account" and I leave it like that “工件存储帐户”字段中预填充了“自动创建存储帐户”,我将其保留为
  3. Click on Deploy button 单击部署按钮

If you take a look in HelloWorldParent.json template in variables you will see two variables "nestedTemplateUri" and "nestedTemplateUriWithBlobContainerName". 如果您在变量的HelloWorldParent.json模板中查看,您将看到两个变量“ nestedTemplateUri”和“ nestedTemplateUriWithBlobContainerName”。

It is my understanding that "nestedTemplateUri" should contain the "blob container name" but that doesn't seem to be the case. 据我了解,“ nestedTemplateUri”应包含“ blob容器名称”,但事实并非如此。

If I deploy with resources > properties > templateLink > "uri": "[variables('nestedTemplateUri')]" 如果我使用资源>属性> templateLink>“ uri”进行部署:“ [variables('nestedTemplateUri')]”

  • The deployment fails with: 部署失败,并显示以下信息:

Error: Code=InvalidContentLink; 错误:代码= InvalidContentLink; Message=Unable to download deployment content from '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' 消息=无法从'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”

  • The Storage Account is created, the templates, parameters and PS1 script are uploaded 创建存储帐户,上传模板,参数和PS1脚本
  • A new deployment is not created in resource group / deployments 没有在资源组/部署中创建新的部署

If I deploy with resources > properties > templateLink > "uri": "[variables('nestedTemplateUriWithBlobContainerName')]" 如果我使用资源>属性> templateLink>“ uri”进行部署:“ [variables('nestedTemplateUriWithBlobContainerName')]”

  • The deployments succeeds. 部署成功。

Any idea? 任何想法? Any help is highly appreciated! 任何帮助深表感谢!

HelloWorldParent.json 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 HelloWorldParent.parameters.json

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

NestedTemplates/HelloWorld.json 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"
    }
  }
}

Unfortunately VS is a bit "dated" in it's support for your scenario... the problem is that you're using the URI function and the _artifactsLocation does not have a trailing slash. 不幸的是,VS在支持您的场景方面有点“过时”……问题是您使用的是URI函数,而_artifactsLocation没有斜杠。 So you have a few options to fix: 因此,您有几种解决方法:

1) in the PS1 file in VS there is a line that looks like this: 1)在VS中的PS1文件中,有一行如下所示:

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

If you change it to this (add a trailing /): 如果将其更改为此(添加尾随/):

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

It should work - alternatively you can just replace the entire script with this one: https://github.com/Azure/azure-quickstart-templates/blob/master/Deploy-AzureResourceGroup.ps1 它应该可以工作-或者,您也可以只用以下脚本替换整个脚本: https : //github.com/Azure/azure-quickstart-templates/blob/master/Deploy-AzureResourceGroup.ps1

Note that if you have other templates that work without the trailing slash, this will be a breaking change. 请注意,如果您还有其他不带斜杠的模板都可以使用,那么这将是一项重大突破。

2) use concat() to create the uri instead of the uri() function. 2)使用concat()创建uri而不是uri()函数。 You still have to know whether there is a trailing slash but this change can be done in the template and not the PS1 file. 您仍然必须知道是否有斜杠,但是可以在模板而不是PS1文件中完成此更改。

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

Either should work. 两者都应该起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM