Azure ARM - 嵌套变量和资源,副本无法正常工作

时间:2017-10-19 09:27:36

标签: azure templates nested resources

我正在尝试使用https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-group-create-multiple#serial-copy

中提到的带有复制选项的嵌套资源

我在嵌套资源变量块中定义了“lbApiVersion”,但是有些嵌套变量块无法识别。

下面是我正在尝试的ARM模板,这只是实际场景中的示例我想将数组传递给arm模板然后在循环中创建多组资源,所以在这种情况下我需要嵌套变量块。

ARM -

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "numberToDeploy": {
      "type": "int",
      "minValue": 2,
      "defaultValue": 2
    }
  },
  "resources": [
    {
      "apiVersion": "2015-01-01",
      "type": "Microsoft.Resources/deployments",
      "name": "[concat('loop-', copyIndex())]",
      "copy": {
        "name": "iterator",
        "count": "[parameters('numberToDeploy')]",
        "mode": "serial",
        "batchSize": 1
      },
      "properties": {
        "mode": "Incremental",
        "template": {
          "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
          "contentVersion": "1.0.0.0",
          "parameters": {},
          "variables": {
            "lbApiVersion": "2015-06-15"
          },
          "resources": [
                    {
                        "apiVersion": "[variables('lbApiVersion')]",
                        "type": "Microsoft.Network/loadBalancers",
                        "name": "[concat('LB','-', copyIndex())]",
                        "location": "[parameters('clusterLocation')]",
                        "dependsOn": [
                           
                        ],
                        "properties": {
                              "frontendIPConfigurations": [
                                 
                              ],
                              "backendAddressPools": [
                                  
                              ],
                              "loadBalancingRules": [
                                                           
                              ],
                              "probes": [
                                  
                              ],
                              "inboundNatPools": [
                                  
                              ]
                          },
                        "tags": {
                            "resourceType": "Service Fabric"
                        }
                    }
          ],
          "outputs": {
          }
        }
      }
    }
  ],
  "outputs": {
  }
}

错误 - “消息”:“无法处理资源的模板语言表达式”/subscriptions/*************/resourceGroups/cluv2/providers/Microsoft.Resources/deployments/loop-在'14'行和'10'列'0'。'模板 找不到变量'lbApiVersion'。

2 个答案:

答案 0 :(得分:1)

根据我的经验,当您内联定义嵌套模板时(在现有模板的代码中),他们从父模板中获取参数和变量值,因此只需将变量定义移动到父模板

答案 1 :(得分:0)

不幸的是,您无法在嵌套模板中将变量和参数用作indicated by the documentation。您可以在外部模板中使用它们。

如果要在嵌入式模板中部署多个资源,请在主模板中声明对象类型的变量或参数,如下所示:

"variables" : {
    "loadBalancers": [
      {
        "version": "2015-06-15"
      },
      {
        "version": "2015-06-15"
      }
    ]
}

您在Microsoft.Resources / deployments资源上的副本将如下所示:

  "copy": {
    "name": "loadBalancerLoop",
    "count": "[length(variables('loadBalancers'))]"
  }

然后,在您的嵌套资源中,使用copyIndex()来获取版本

"apiVersion": "[variables('loadBalancers')[copyIndex()].version]"
相关问题