我可以拥有一个COPY数组为0到N的ARM模板资源吗? [英] Can I have an ARM template resource with a COPY array of 0 to N

查看:77
本文介绍了我可以拥有一个COPY数组为0到N的ARM模板资源吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在部署一个ARM模板,该模板使用复制资源块将1个或多个数据磁盘部署到VM.我想将其更改为0或更大.

I'm deploying an ARM template that uses a copy resource block to deploy 1 or more data disks to a VM. What I'd like to do is change this to 0 or more.

我使用的参数是

    "VirtualMachineDiskSizeArray": {
        "type": "array",
        "defaultValue": [ "100" ]
    },

然后在资源中调用哪个:

Which is then called in a resource:

   "resources": [
    {
        "name": "[parameters('virtualMachineName')]",
        "type": "Microsoft.Compute/virtualMachines",
        "apiVersion": "2016-04-30-preview",
        "location": "[parameters('rgLocation')]",
        "dependsOn": [
            "[concat('Microsoft.Storage/storageAccounts/', parameters('rgStorageAccountName'))]"
        ],
        "properties": {
            "osProfile": { ... },
            "hardwareProfile": { ... },
            "storageProfile": {
                "imageReference": { ... },
                "osDisk": { ... },
                "copy": [
                    {
                        "name": "dataDisks",
                        "count": "[length(parameters('VirtualMachineDiskSizeArray'))]",
                        "input": {
                            "lun": "[copyIndex('dataDisks')]",
                            "name": "[concat(parameters('vmDataDiskNameStub'), add(copyIndex('dataDisks'),1), '.vhd')]",
                            "diskSizeGB": "[parameters('VirtualMachineDiskSizeArray')[copyIndex('dataDisks')]]",
                            "createOption": "Empty",
                            "vhd": {
                                "uri": "[concat(concat(reference(resourceId(parameters('rgName'), 'Microsoft.Storage/storageAccounts', parameters('rgStorageAccountName')), '2015-06-15').primaryEndpoints['blob'], 'vhds/'), concat(parameters('vmDataDiskNameStub'),  add(copyIndex('dataDisks'),1), '.vhd') )]"
                            }
                        }
                    }
                ]
            }
        }
    },

但是,当我传入一个包含0个元素的数据磁盘阵列时,却出现了此错误,正如预期的那样:

However, when I pass in an array of data disks with 0 elements, I get this error, as expected:

Validation returned the following errors:
: Deployment template validation failed: 'The template 'copy' definition at line '0' and column '0' has an invalid copy count. The co
py count must be a postive integer value and cannot exceed '800'. Please see https://aka.ms/arm-copy for usage details.'.

Template is invalid.

我想尝试以某种方式解决此问题-我尝试在副本上添加条件:

I would like to try to work around this somehow - I tried adding a condition on the copy:

"condition": "[  greater(length(parameters('VirtualMachineDiskSizeArray')), 0)]",

但这返回了相同的错误.

But that returned the same error.

我正在研究嵌套模板,但这对于资源的部分来说并不好.

I'm researching nested templates, but that doesn't look good for a section of a resource.

推荐答案

因此,在时间兴趣中,我已更改了此方法,但并不十分喜欢...

So in the Interest of Time, I have changed my approach to this, but don't really like it...

我现在有两个部署json文件,VMDeploy.jsonVMDeploy-NoDataDisks.json.

I now have two deploy json files, VMDeploy.json and VMDeploy-NoDataDisks.json.

除了VM资源的storageProfile部分之外,它们是相同的:

They are identical other than the storageProfile section of the VM resource:

VMDeploy.json:

"storageProfile": {
  "imageReference": { ... },
  "osDisk": { ... },
  "copy": [
    {
    "name": "dataDisks",
    "count": "[length(parameters('VirtualMachineDiskSizeArray'))]",
    "input": {
      "lun": "[copyIndex('dataDisks')]",
      "name": "[concat(parameters('vmDataDiskNameStub'), add(copyIndex('dataDisks'),1), '.vhd')]",
      "diskSizeGB": "[parameters('VirtualMachineDiskSizeArray')[copyIndex('dataDisks')]]",
      "createOption": "Empty",
      "vhd": {
        "uri": "[concat(concat(reference(resourceId(parameters('rgName'), 'Microsoft.Storage/storageAccounts', parameters('rgStorageAccountName')), '2015-06-15').primaryEndpoints['blob'], 'vhds/'), concat(parameters('vmDataDiskNameStub'),  add(copyIndex('dataDisks'),1), '.vhd') )]"
        }
      }
    }
  ]
}

VMDeploy-NoDataDisks.json:

"storageProfile": {
  "imageReference": { ... },
  "osDisk": { ... },
  "dataDisks": []
}

我有一个Powershell块在两个json文件之间切换:

And I have a Powershell block switches between the two json files:

if ($DriveArray.Count -eq 0) {
    $TemplateFile = $TemplateFile.Replace('.json','-NoDataDisks.json')
}

这篇关于我可以拥有一个COPY数组为0到N的ARM模板资源吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆