簡體   English   中英

ARM嵌套模板:部署帶有嵌套模板的模板時,_artifactLocation參數未正確填充

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

我試圖弄清楚嵌套模板是如何工作的,我有以下模板。 我正在嘗試使用VS部署機制從VS進行部署:

  1. 右鍵單擊項目>部署>新建
  2. “工件存儲帳戶”字段中預填充了“自動創建存儲帳戶”,我將其保留為
  3. 單擊部署按鈕

如果您在變量的HelloWorldParent.json模板中查看,您將看到兩個變量“ nestedTemplateUri”和“ nestedTemplateUriWithBlobContainerName”。

據我了解,“ nestedTemplateUri”應包含“ blob容器名稱”,但事實並非如此。

如果我使用資源>屬性> templateLink>“ uri”進行部署:“ [variables('nestedTemplateUri')]”

  • 部署失敗,並顯示以下信息:

錯誤:代碼= InvalidContentLink; 消息=無法從'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"
    }
  }
}

不幸的是,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'))]"

兩者都應該起作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM