如何在 Azure 中使用 ARM 模板打印链接模板的输出

How to print output of linked template using ARM template in Azure

我正在使用主模板和链接模板进行部署。我想在部署后打印链接模板的输出。

当我部署以下模板时。我收到以下错误,

The template output 'vmpublicIPName' is not valid: The language expression property 'publicIPName' doesn't exist, available properties are ''.. (Code: DeploymentOutputEvaluationFailed)

  1. 如何打印链接模板中存在的变量的输出?
  2. 有没有办法在主模板中打印所有链接模板部署参数值?

storage.json

{
    "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "storageAccountName": {
            "type": "string"
        },
        "storageAccountType": {
            "type": "string",
            "defaultValue": "Standard_LRS"
        }
    },
    "variables": {
        "location": "[resourceGroup().location]",
        "resourceGroupName": "[resourceGroup().name]",
        "subscriptionId": "[subscription().subscriptionId]"
    },
    "resources": [
        {
            "name": "[concat(parameters('storageAccountName'), '1rmtest')]",
            "type": "Microsoft.Storage/storageAccounts",
            "apiVersion": "2015-06-15",
            "location": "[variables('location')]",
            "properties": {
                "accountType": "[parameters('storageAccountType')]"
            },
            "tags": {
                "BuildName": "StorageARM"
            }
        },
        { 
            "apiVersion": "2017-03-01", 
            "name": "TestTemplate", 
            "type": "Microsoft.Resources/deployments", 
            "properties": { 
                "mode": "incremental", 
                "templateLink": {
                    "uri":"https://gist.githubusercontent.com/public-ip-template.json",
                    "contentVersion":"1.0.0.0"
                },
                "parameters": {                    
                    "publicIpAddressName": {
                        "value": "public-ip-test"
                    }
                }
            } 
        }

    ],
    "outputs": {
        "vmpublicIPName": {
            "type": "object",
            "value": "[reference('TestTemplate').outputs.publicIPName]"
        },
        "vmlocation": {
            "type": "object",
            "value": "[reference('TestTemplate').outputs.location]"
        }
    }
}

链接模板:-

{
    "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "publicIpAddressName": {
            "type": "string"
        }
    },
    "variables": {
        "location": "[resourceGroup().location]",
        "resourceGroupName": "[resourceGroup().name]"
    },
    "resources": [
        {
            "name": "[parameters('publicIpAddressName')]",
            "type": "Microsoft.Network/publicIpAddresses",
            "apiVersion": "2016-09-01",
            "location": "[variables('location')]",
            "properties": {
                "publicIpAllocationMethod": "Static"
            }
        }  

    ],
    "outputs": {
        "publicIPName": {
            "type": "string",
            "value": "[parameters('publicIpAddressName')]"
        },
        "location": {
            "type": "string",
            "value": "[variables('location')]"
        }
    }
}
"someName": {
    "type": "string",
    "value": "[variables('somevar')]"
},
  1. 和你输出参数的方式一样

  2. 不,没有办法。

问题是什么,你的模板看起来不错。我已经测试过了并且有效

ps。您模板上的 link 是错误的,这是唯一不起作用的东西

您确保您的链接模板 URI 正确且可访问吗?根据这个official document

The URI value for the linked parameter file cannot be a local file, and must include either http or https.

我在我的实验室测试,我只替换你的 URI,如下所示:

 "templateLink": {
                    "uri":"https://gist.githubusercontent.com/Walter-Shui/d5387c0fc92f2e8df1c7157a2d5e54aa/raw/722d4a58107b2f617996ae237ceae445ef4342d9/test.json",
                    "contentVersion":"1.0.0.0"
                },

你的模板适合我。

How can I print the output of variables present in linked template?

是的,这是可能的。就像你的模板一样。

Is there any way to print all the linked template deployment parameters values in main template?

您可以使用 Azure cli 2.0 获取链接的参数值。

az group deployment create --name shuitest1 --resource-group shuitest --template-file test.json --parameters '{"storageAccountName":{"value":"shuitest"}}'

{
  "id": "/subscriptions/********/resourceGroups/shuitest/providers/Microsoft.Resources/deployments/shuitest1",
  "name": "shuitest1",
  "properties": {
    "correlationId": "dbe16f35-0807-4627-b4b5-86c0a25c49ba",
    "debugSetting": null,
    "dependencies": [],
    "mode": "Incremental",
    "outputs": {
      "vmlocation": {
        "type": "Object",
        "value": {
          "type": "String",
          "value": "centralus"
        }
      },
      "vmpublicIPName": {
        "type": "Object",
        "value": {
          "type": "String",
          "value": "public-ip-test"
        }
      }
    },
    "parameters": {
      "storageAccountName": {
        "type": "String",
        "value": "shuitest"
      },
      "storageAccountType": {
        "type": "String",
        "value": "Standard_LRS"
      }
    },
    "parametersLink": null,
    "providers": [
      {
        "id": null,
        "namespace": "Microsoft.Storage",
        "registrationState": null,
        "resourceTypes": [
          {
            "aliases": null,
            "apiVersions": null,
            "locations": [
              "centralus"
            ],
            "properties": null,
            "resourceType": "storageAccounts"
          }
        ]
      },
      {
        "id": null,
        "namespace": "Microsoft.Resources",
        "registrationState": null,
        "resourceTypes": [
          {
            "aliases": null,
            "apiVersions": null,
            "locations": [
              null
            ],
            "properties": null,
            "resourceType": "deployments"
          }
        ]
      }
    ],
    "provisioningState": "Succeeded",
    "template": null,
    "templateLink": null,
    "timestamp": "2017-04-19T02:09:55.064156+00:00"
  },
  "resourceGroup": "shuitest"
}