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

时间:2020-04-10 12:45:06

标签: azure azure-devops azure-resource-manager arm-template

是否可以在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

1 个答案:

答案 0 :(得分:3)

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

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,而这些API不会返回完整模板(目前)。

2)/ whatif api也将返回经过评估的JSON,但是如果您所追求的只是经过评估的模板,则还有很多事情要经过:https://docs.microsoft.com/en-us/azure/azure-resource-manager/templates/template-deploy-what-if

有帮助吗?