【问题标题】:ARM template: output instances resources name with CopyARM 模板:使用 Copy 输出实例资源名称
【发布时间】:2020-11-13 07:58:52
【问题描述】:

寻找一些关于如何输出我的应用服务实例名称的反馈。我与 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 :("
            } 
        }
    }
}

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

有什么想法吗?

更新 1 我应用了 Stringfellow 提供的建议,我得到的和你的截图一样。

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

但是对于 appServicename 我没有运气。我尝试了 $deploymentResult.Outputs.websitesname.Value 然后尝试分离该值,但它失败了。我也尝试转换为 json。我相信它存在一种聪明的方式。根据我的示例,我如何检索值 AppService-Prod-centralus 和 AppService-Prod-eastus ?

【问题讨论】:

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


    【解决方案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')]"
        }
      }
    }
    

    输出看起来像这样。

    【讨论】:

      【解决方案2】:

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

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

      有帮助吗?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-07-02
        • 2021-11-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-02-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多