ARM模板:通过复制输出实例资源名称

时间:2020-11-10 16:37:23

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

正在寻找有关如何输出应用服务实例名称的反馈。我使用过CopyIndex,但我不知道该怎么做。

首先是我的参数。请注意,我使用了一个数组作为参数的位置。

"parameters": {
    "location": {
        "type": "array",
        "metadata": {
            "description": "array of region"
        },
        "allowedValues": [
            "centralus",
            "eastus"
        ]
    } 

然后我实例化我的AppServices并在我的其他位置进行迭代

{ // App Services
        "type": "Microsoft.Web/sites",
        "name": "[concat(variables('appServiceName'), parameters('location')[copyIndex()])]",
        "apiVersion": "2018-11-01",
        "copy": {
            "name": "Copy website",
            "count": "[length(parameters('location'))]"
        },
        "location": "[parameters('location')[copyIndex()]]",
        "tags": {
            "cost": "[parameters('Stage')]"
        }
        ....

        

它运行良好,现在我要输出应用程序服务实例名称:

 "outputs": {
    "websitesname": {
        "type": "array",
        "copy": {
            "count": "[length(parameters('location'))]",
            "input":{
                "id": "here i struggle :("
            } 
        }
    }
}

我真的不知道如何获取实例名称? 首先我尝试返回变量的值,但是失败了。其次,我尝试返回了 reference 的值,但是再次失败了。

有什么主意吗?

更新1 我应用了Stringfellow提供的建议,与您的屏幕截图相同。 enter image description here

我可以使用$ deploymentResult.Outputs.keyVaultName.Value检索KeyVault名称

但是对于appServicename我没有运气。我尝试了$ deploymentResult.Outputs.websitesname.Value,然后尝试将值分开,但是失败了。我也尝试转换为json。我相信这是一个聪明的方法。根据我的示例,如何获取值AppService-Prod-centralus和AppService-Prod-eastus?

2 个答案:

答案 0 :(得分:1)

您可以使用复制运算符构造一个新变量,然后在此处定义资源。然后使用新数组复制资源并构建所需的输出,或者仅按原样输出新数组。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "location": {
      "type": "array"
    }
  },
  "variables": {
    "appServiceName": "prefix",
    "copy": [
      {
        "name": "webSiteNames",
        "count": "[length(parameters('location'))]",
        "input": {
          "name": "[concat(variables('appServiceName'), parameters('location')[copyIndex('webSiteNames')])]",
          "location": "[parameters('location')[copyIndex('webSiteNames')]]"
        }
      }
    ]
  },
  "resources": [
    { // App Services
        "type": "Microsoft.Web/sites",
        "name": "[variables('webSiteNames')[copyIndex()].name]",
        "apiVersion": "2018-11-01",
        "copy": {
            "name": "Copy website",
            "count": "[length(parameters('location'))]"
        },
        "location": "[variables('webSiteNames')[copyIndex()].location]",
        ...
    }
  ],
  "outputs": {
    "websitesname": {
      "type": "array",
      "copy": {
        "count": "[length(variables('webSiteNames'))]",
        "input": {
          "id": "[variables('webSiteNames')[copyIndex()].name]"
        }
      }
    },
    "webSiteName": {
      "type": "array",
      "value": "[variables('webSiteNames')]"
    }
  }
}

输出看起来像。 enter image description here

答案 1 :(得分:1)

如果您只想输出创建的网站的数组,则只需使用名称中使用的相同表达式即可,例如

    "outputs": {
        "sites": {
            "type": "array",
            "copy": {
                "count": "[length(parameters('location'))]",
                "input": "[concat(variables('appServiceName'), parameters('location')[copyIndex()])]"
            }
        },

有帮助吗?