部署失败后获取解析的ARM模板

问题描述 投票:1回答:1

是否可以在运行时在Azure Portal中获得ARM模板,并解析变量和参数?

以下示例:

AzureDeploy.json

{
 "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
 "contentVersion": "1.0.0.0",
 "parameters": {
   "environment": {
     "type": "string",
     "defaultValue": "dev",
   },
   "storageSKU": {
     "type": "string",
     "defaultValue": "Standard_LRS",
     "allowedValues": [
       "Standard_LRS",
       "Standard_GRS",
       "Standard_RAGRS",
       "Standard_ZRS",
       "Premium_LRS",
       "Premium_ZRS",
       "Standard_GZRS",
       "Standard_RAGZRS"
     ]
   },
   "location": {
     "type": "string",
     "defaultValue": "[resourceGroup().location]"
   }
 },
  "variables": {
    "storageAccountName": "[concat('companyname',parameters('environment'),'sa01'))]"
  },
 "resources": [
   {
     "type": "Microsoft.Storage/storageAccounts",
     "apiVersion": "2019-04-01",
     "name": "[variables('storageName')]",
     "location": "[parameters('location')]",
     "sku": {
       "name": "[parameters('storageSKU')]"
     },
     "kind": "StorageV2",
     "properties": {
       "supportsHttpsTrafficOnly": true
     }
   }
 ]
}

AzureDeploy.parameters.json

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

如果该部署由于名称或SKU之类的问题而失败,我是否能够访问门户或以某种方式查看运行脚本时如何解析这些值?

部署发生在AzureDevops的CD管道中,我可以控制变量组等,因此我知道传递的是什么,但不知道它如何解析。在一个更复杂的模板中,我有一个错误,声称未在Logic App API连接上设置Id,但我无法确定该错误是否是由于我在concat函数中使用的变量所致,还是该值确实是不正确的(解析可以根据传入的数据)。

[如果有人熟悉如何通过Azure中的部署刀片对这些问题进行故障排除,那么您可能会掌握一些有关如何查看更详细视图的提示。

谢谢,

编辑:

下面的代码在Visual Studio 2019中触发Intellisense,但已确认在部署期间可以正常工作。根据注释,VS Code中没有警告。为简洁起见,省略了大多数代码。

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "environment": {
          "type": "string",
          "defaultValue": "dev"
        },
        "increment": {
          "type": "string",
          "defaultValue": "01"
        },
        "keyvaultName": {
          "type": "string",
          "defaultValue": "randomKeyVaultName",
          "metadata": {
            "description": "Keyvault Name for deployment"
          }
        }
    },
    "variables": {
        "uniqueKeyVaultName": "[parameters('keyvaultName')]"
    },
    "resources": [
        {
          "type": "Microsoft.KeyVault/vaults/secrets",
          "apiVersion": "2016-10-01",
          "name": "[concat(variables('uniqueKeyVaultName'), '/407045A0-1B78-47B5-9090-59C0AE9A96F6')]",
          "location": "northeurope",
          "dependsOn": [
            "[resourceId('Microsoft.Resources/deployments', 'cosmosdb_linkedtemplate')]"
          ],
          "properties": {
            "contentType": "Graph",
            "value": "[concat('{''D'': ''DatabaseName'', ''U'': ''https://randomcosmosdb-',parameters('environment'),'-cdb-',parameters('increment'),'.documents.azure.com'', ''C'': ''CollectionName'', ''K'': ''',reference('cosmosdb_linkedtemplate').outputs.accountKey.value,'''}')]",
            "attributes": {
              "enabled": true
            }
          }
        }
    ],
    "outputs": {}
}

Visual Studio Version

azure azure-devops azure-resource-manager arm-template
1个回答
2
投票

如果您想查看评估后的模板,可以做一些事情,而无需进行部署:

1)调用/ validate api:https://docs.microsoft.com/en-us/rest/api/resources/deployments/validate-但您目前需要使用较旧的apiVersion(例如2017-05-01)...响应将包含经过全面评估的模板。如果您使用的是较旧版本的PowerShell或CLI,则可以使用-debug开关查看其余API的响应。但是请记住,PS / CLI的最新版本将使用较新的apiVersion,而这些版本不返回完整模板(目前)。

2)/ whatif api也将返回评估后的JSON,但是如果您所需要的只是评估后的模板,则还有更多的工作要做:https://docs.microsoft.com/en-us/azure/azure-resource-manager/templates/template-deploy-what-if

有帮助吗?

© www.soinside.com 2019 - 2024. All rights reserved.