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

标签 azure templates azure-rm-template

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

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

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

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

存储.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')]"
        }
    }
}

最佳答案

您是否确保链接的模板 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"
                },

你的模板适合我。

enter image description here

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"
}

关于azure - 如何在 Azure 中使用 ARM 模板打印链接模板的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43463870/

相关文章:

C++ 模板编程 - 延迟函数调用

json - azure ARM : Remove invalid characters

azure - 二头肌矫正任务循环

Azure API 管理返回 "Setting ' config.db.connection' 找不到。”

c++ - 成员函数实例化

node.js - 从字符串渲染哈巴狗模板?

azure - ARM模板,服务器名称已经存在,但实际上不存在

web-services - 如何合理地将需要长时间初始化的 Azure 云服务转换为 Azure Service Fabric 服务?

azure - Web Api 上传到 Azure 存储 - 请求实体太大

azure - 如何在 ARM 模板的输出部分获取 key 的最新版本?