将数组值传递给变量

Pass Array value to a Variable

我想改变传递给 Bicep 模板的 Array 参数的一个元素。这是参数文件的简化版本:

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "location": {
            "value": "uksouth"
        },
        "networkSecurityGroupRules": {
            "value": [
                {
                    "name": "RDP",
                    "properties": {
                        "priority": 300,
                        "protocol": "TCP",
                        "access": "Allow",
                        "direction": "Inbound",
                        "sourceAddressPrefix": "*",
                        "sourcePortRange": "*",
                        "destinationAddressPrefix": "*",
                        "destinationPortRange": "3389"
                    }
                }
            ]
        },
        "vnetName": {
            "value": "Dtldevopsagent"
        },
        "subnetName": {
            "value": "sandbox_subnet_2"
        },
        "virtualMachineName": {
            "value": "test-db"
        },
        "osDiskType": {
            "value": "Premium_LRS"
        },
        "dataDisks": {
            "value": [
                {
                    "lun": 0,
                    "createOption": "attach",
                    "caching": "ReadOnly",
                    "writeAcceleratorEnabled": false,
                    "id": null,
                    "name": "[concat(parameters('virtualMachineName'), '-_DataDisk_0')]'",
                    "storageAccountType": null,
                    "diskSizeGB": null,
                    "diskEncryptionSet": null
                },
                {
                    "lun": 1,
                    "createOption": "attach",
                    "caching": "None",
                    "writeAcceleratorEnabled": false,
                    "id": null,
                    "name": "[concat(parameters('virtualMachineName'),'-DataDisk_1')]",
                    "storageAccountType": null,
                    "diskSizeGB": null,
                    "diskEncryptionSet": null
                }
            ]
        },
        "dataDiskResources": {
           "value": [
            {
                "name": "[concat(parameters('virtualMachineName'),'-DataDisk_0')]",
                "sku": "Premium_LRS",
                "properties": {
                    "diskSizeGB": 32,
                    "creationData": {
                        "createOption": "empty"
                    }
                }
            },
            {
                "name": "[concat(parameters('virtualMachineName'),'-DataDisk_1')]",
                "sku": "Premium_LRS",
                "properties": {
                    "diskSizeGB": 32,
                    "creationData": {
                        "createOption": "empty"
                    }
                }
            }
        ]
      },
    }
}

我尝试设置的数组参数值是 dataDisks.value.namedataDiskResources.value.name。我尝试使用 "[concat(parameters('virtualMachineName'), '-_DataDisk_0')]'" 来设置虚拟机磁盘的名称,但出现错误“参数 disk.name 的值无效。”。

有没有一种方法可以将这些值传递到我的 Bicep 模板文件中的变量,并且只编辑这两个参数中每一个的“名称”值?或者有其他方法可以编辑它吗?

参数中你不能concat。它将出错 "The value of parameter disk.name is invalid."相反,您可以在 Bicep ARMJSON ARM.

中使用变量

因此您的 ARM 模板将是 link 下面的内容:

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "location": {
            "type": "string",
            "defaultValue": "uksouth"
        },
        "networkSecurityGroupRules": {
            "type": "array",
            "defaultValue": [
                {
                    "name": "RDP",
                    "properties": {
                        "priority": 300,
                        "protocol": "TCP",
                        "access": "Allow",
                        "direction": "Inbound",
                        "sourceAddressPrefix": "*",
                        "sourcePortRange": "*",
                        "destinationAddressPrefix": "*",
                        "destinationPortRange": "3389"
                    }
                }
            ]
        },
        "vnetName": {
            "type": "string",
            "defaultValue": "Dtldevopsagent"
        },
        "subnetName": {
            "type": "string",
            "defaultValue": "sandbox_subnet_2"
        },
        "virtualMachineName": {
            "type": "string",
            "defaultValue": "test-db"
        },
        "osDiskType": {
            "type": "string",
            "defaultValue": "Premium_LRS"
        }
    },
    "variables":{
        "dataDisks": [ 
                {
                    "lun": 0,
                    "createOption": "attach",
                    "caching": "ReadOnly",
                    "writeAcceleratorEnabled": false,
                    "id": null,
                    "name": "[concat(parameters('virtualMachineName'), '-_DataDisk_0')]",
                    "storageAccountType": null,
                    "diskSizeGB": null,
                    "diskEncryptionSet": null
                },
                {
                    "lun": 1,
                    "createOption": "attach",
                    "caching": "None",
                    "writeAcceleratorEnabled": false,
                    "id": null,
                    "name": "[concat(parameters('virtualMachineName'),'-DataDisk_1')]",
                    "storageAccountType": null,
                    "diskSizeGB": null,
                    "diskEncryptionSet": null
                }
            ],
        "dataDiskResources": [
            {
                "name": "[concat(parameters('virtualMachineName'),'-DataDisk_0')]",
                "sku": "Premium_LRS",
                "properties": {
                    "diskSizeGB": 32,
                    "creationData": {
                        "createOption": "empty"
                    }
                }
            },
            {
                "name": "[concat(parameters('virtualMachineName'),'-DataDisk_1')]",
                "sku": "Premium_LRS",
                "properties": {
                    "diskSizeGB": 32,
                    "creationData": {
                        "createOption": "empty"
                    }
                }
            }
        ]
    },
    "resources":[],
    "outputs": {
        "DatadiskName": {
          "value": "[variables('dataDisks')[0].name]",
          "type": "string"
        },
        "DatadiskResourceName": {
          "value": "[variables('dataDiskResources')[0].name]",
          "type": "string"
        }
    }
}

输出:

您可以参考Variables in Bicep - Azure Resource Manager | Microsoft Docs & Variables in templates - Azure Resource Manager | Microsoft Docs了解更多信息。