【问题标题】:Azure templates running through an array通过数组运行的 Azure 模板
【发布时间】:2020-03-10 11:26:16
【问题描述】:

我对使用 azure 模板进行部署相当陌生,并且一直在开发一个部署一组虚拟机的模板。在模板的第一次迭代中,我将其设置为读取参数文件并将虚拟机名称指定为参数 - 此参数用于形成 NIC 名称以及 OS 磁盘名称。

我现在尝试做的是设置模板以根据参数中的数组创建多个虚拟机。

我已经设置了参数

    "virtualMachineName": {
        "type": "array",
        "defaultValue": [
            "VM-WEB01",
            "VM-WEB02"
        ]

然后在模板文件中我引用它类似于此

"networkInterfaceName":  "[concat(toUpper(parameters('virtualMachineName')[copyIndex()]),'-NIC')]"
"name": "[concat(parameters('virtualMachineName')[copyIndex()],'-osDisk')]",

等等。

然后我在资源部分的末尾使用了复制选项,但在模板中的输出部分之前

 "copy": {
        "name": "vmcopy",
        "count": "[length(parameters('virtualMachineName'))]"
      }

在尝试部署模板时,我收到一条错误消息,指出 virtualMachineName 参数为空。

任何我出错的地方都将不胜感激。

这是完整的模板

{
"$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
    "location": {
        "type": "string"
    },
    "networkSecurityGroupName": {
        "type": "string"
    },
    "networkSecurityGroupRules": {
        "type": "array"
    },
    "subnetName": {
        "type": "string"
    },
    "virtualNetworkId": {
        "type": "string"
    },
    "virtualMachineName": {
        "type": "array",
        "defaultValue": [
            "PROD-VM-RDGW01",
            "PROD-VM-RDGW02"
        ]
    },
    "virtualMachineRG": {
        "type": "array"
    },
    "osDiskType": {
        "type": "string"
    },
    "virtualMachineSize": {
        "type": "string"
    },
    "adminUsername": {
        "type": "string"
    },
    "adminPassword": {
        "type": "securestring"
    },
    "diagnosticsStorageAccountName": {
        "type": "string"
    },
    "diagnosticsStorageAccountId": {
        "type": "string"
    },
    "availabilitySetName": {
        "type": "string"
    },
    "availabilitySetPlatformFaultDomainCount": {
        "type": "int"
    },
    "availabilitySetPlatformUpdateDomainCount": {
        "type": "int"
    },
    "backupVaultName": {
        "type": "string"
    },
    "backupFabricName": {
        "type": "string"
    },
    "backupVaultRGName": {
        "type": "string"
    },
    "backupVaultRGIsNew": {
        "type": "bool"
    },
    "backupPolicyName": {
        "type": "string"
    },
    "backupItemName": {
        "type": "string"
    }
},
"variables": {
    "networkInterfaceName":  "[concat(toUpper(parameters('virtualMachineName')[copyIndex()]),'-NIC')]",
    "nsgId": "[resourceId(resourceGroup().name, 'Microsoft.Network/networkSecurityGroups', parameters('networkSecurityGroupName'))]",
    "vnetId": "[parameters('virtualNetworkId')]",
    "subnetRef": "[concat(variables('vnetId'), '/subnets/', parameters('subnetName'))]"
},
"resources": [
    {
        "name": "[variables('networkInterfaceName')]",
        "type": "Microsoft.Network/networkInterfaces",
        "apiVersion": "2019-07-01",
        "location": "[parameters('location')]",
        "dependsOn": [
            "[concat('Microsoft.Network/networkSecurityGroups/', parameters('networkSecurityGroupName'))]"
        ],
        "properties": {
            "ipConfigurations": [
                {
                    "name": "ipconfig1",
                    "properties": {
                        "subnet": {
                            "id": "[variables('subnetRef')]"
                        },
                        "privateIPAllocationMethod": "Dynamic"
                    }
                }
            ],
            "networkSecurityGroup": {
                "id": "[variables('nsgId')]"
            }
        },
        "tags": {
            "billTO": "IT",
            "Environment": "Production",
            "projectName": "RDS"
        },
        "copy": {
            "name": "vmNICCopy",
            "count": "[length(parameters('virtualMachineName'))]"
        }
    },
    {
        "name": "[parameters('networkSecurityGroupName')]",
        "type": "Microsoft.Network/networkSecurityGroups",
        "apiVersion": "2019-02-01",
        "location": "[parameters('location')]",
        "properties": {
            "securityRules": "[parameters('networkSecurityGroupRules')]"
        },
        "tags": {
            "billTO": "IT",
            "Environment": "Production",
            "projectName": "RDS"
        }
    },
    {
        "name": "[concat(parameters('virtualMachineName')[copyIndex())]",
        "type": "Microsoft.Compute/virtualMachines",
        "apiVersion": "2019-07-01",
        "location": "[parameters('location')]",
        "dependsOn": [
            "[concat('Microsoft.Network/networkInterfaces/', variables('networkInterfaceName'))]",
            "[concat('Microsoft.Compute/availabilitySets/', parameters('availabilitySetName'))]"
        ],
        "properties": {
            "hardwareProfile": {
                "vmSize": "[parameters('virtualMachineSize')]"
            },
            "storageProfile": {
                "osDisk": {
                    "createOption": "fromImage",
                    "name": "[concat(parameters('virtualMachineName')[copyIndex()],'-osDisk')]",
                    "managedDisk": {
                        "storageAccountType": "[parameters('osDiskType')]"
                    }
                },
                "imageReference": {
                    "publisher": "MicrosoftWindowsServer",
                    "offer": "WindowsServer",
                    "sku": "2019-Datacenter",
                    "version": "latest"
                }
            },
            "networkProfile": {
                "networkInterfaces": [
                    {
                        "id": "[resourceId('Microsoft.Network/networkInterfaces', variables('networkInterfaceName'))]"
                    }
                ]
            },
            "osProfile": {
                "computerName": "[concat(parameters('virtualMachineName')[copyIndex())]",
                "adminUsername": "[parameters('adminUsername')]",
                "adminPassword": "[parameters('adminPassword')]",
                "windowsConfiguration": {
                    "enableAutomaticUpdates": true,
                    "provisionVmAgent": true
                }
            },
            "licenseType": "Windows_Server",
            "diagnosticsProfile": {
                "bootDiagnostics": {
                    "enabled": true,
                    "storageUri": "[concat('https://', parameters('diagnosticsStorageAccountName'), '.blob.core.windows.net/')]"
                }
            },
            "availabilitySet": {
                "id": "[resourceId('Microsoft.Compute/availabilitySets', parameters('availabilitySetName'))]"
            }
        },

        "tags": {
            "billTO": "IT",
            "Environment": "Production",
            "projectName": "RDS"
        },
        "copy": {
            "name": "vmCopy",
            "count": "[length(parameters('virtualMachineName'))]"
        }
    },
    {
        "name": "[parameters('availabilitySetName')]",
        "type": "Microsoft.Compute/availabilitySets",
        "apiVersion": "2019-07-01",
        "location": "[parameters('location')]",
        "properties": {
            "platformFaultDomainCount": "[parameters('availabilitySetPlatformFaultDomainCount')]",
            "platformUpdateDomainCount": "[parameters('availabilitySetPlatformUpdateDomainCount')]"
        },
        "sku": {
            "name": "Aligned"
        },
        "tags": {
            "billTO": "IT",
            "Environment": "Production",
            "projectName": "RDS"
        }
    },
    {
        "apiVersion": "2017-05-10",
        "name": "[concat(parameters('virtualMachineName')[copyIndex()], '-' , 'BackupIntent')]",
        "type": "Microsoft.Resources/deployments",
        "resourceGroup": "[parameters('backupVaultRGName')]",
        "properties": {
            "mode": "Incremental",
            "template": {
                "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
                "contentVersion": "1.0.0.0",
                "resources": [
                    {
                        "name": "[concat(parameters('backupVaultName'), '/', parameters('backupFabricName'), '/', parameters('backupItemName'))]",
                        "apiVersion": "2017-07-01",
                        "type": "Microsoft.RecoveryServices/vaults/backupFabrics/backupProtectionIntent",
                        "properties": {
                            "friendlyName": "[concat(parameters('virtualMachineName')[copyIndex()], 'BackupIntent')]",
                            "protectionIntentItemType": "AzureResourceItem",
                            "policyId": "[resourceId(parameters('backupVaultRGName'), 'Microsoft.RecoveryServices/vaults/backupPolicies', parameters('backupVaultName'), parameters('backupPolicyName'))]",
                            "sourceResourceId": "[resourceId(parameters('virtualMachineRG'), 'Microsoft.Compute/virtualMachines', parameters('virtualMachineName')[copyIndex()])]"
                        }
                    }
                ]
            },
            "copy": {
                "name": "vmNICCopy",
                "count": "[length(parameters('virtualMachineName'))]"
            }
        },
        "dependsOn": [
            "[resourceId(parameters('virtualMachineRG'), 'Microsoft.Compute/virtualMachines', [concat(parameters('virtualMachineName')[copyIndex())])]]"
        ]
    }
],
"outputs": {
    "adminUsername": {
        "type": "string",
        "value": "[parameters('adminUsername')]"
    }
}

}

【问题讨论】:

    标签: azure templates azure-resource-manager


    【解决方案1】:

    原则上这应该没问题,因为您没有提供整个模板,因此很难准确判断出了什么问题,但是您应该能够通过修改 here 的基本示例并使用您的位来制作一个工作示例示例中的一个(您还需要将参数固定为数组):

    "copy": {
        "name": "vmcopy",
        "count": "[length(parameters('virtualMachineName'))]"
    }
    

    修复应该是这样的:

    {
        "apiVersion": "2017-05-10",
        "name": "[concat(parameters('virtualMachineName')[copyIndex()], '-' , 'BackupIntent')]",
        "type": "Microsoft.Resources/deployments",
        "resourceGroup": "[parameters('backupVaultRGName')]",
        "properties": {
            "mode": "Incremental",
            "template": {
                "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
                "contentVersion": "1.0.0.0",
                "resources": [
                    {
                        "name": "[concat(parameters('backupVaultName'), '/', parameters('backupFabricName'), '/', parameters('backupItemName'))]",
                        "apiVersion": "2017-07-01",
                        "type": "Microsoft.RecoveryServices/vaults/backupFabrics/backupProtectionIntent",
                        "properties": {
                            "friendlyName": "[concat(parameters('virtualMachineName')[copyIndex()], 'BackupIntent')]",
                            "protectionIntentItemType": "AzureResourceItem",
                            "policyId": "[resourceId(parameters('backupVaultRGName'), 'Microsoft.RecoveryServices/vaults/backupPolicies', parameters('backupVaultName'), parameters('backupPolicyName'))]",
                            "sourceResourceId": "[resourceId(parameters('virtualMachineRG'), 'Microsoft.Compute/virtualMachines', parameters('virtualMachineName')[copyIndex()])]"
                        }
                    }
                ]
            }
        },
        "copy": {
            "name": "vmNICCopy",
            "count": "[length(parameters('virtualMachineName'))]"
        },
        "dependsOn": [
            "[resourceId(parameters('virtualMachineRG'), 'Microsoft.Compute/virtualMachines', [concat(parameters('virtualMachineName')[copyIndex())])]]"
        ]
    }
    

    【讨论】:

    • 谢谢 - 用完整的模板更新了问题
    • 谢谢 - 我仍然收到此错误 New-AzResourceGroupDeployment : 14:08:35 - 错误: Code=InvalidDeploymentParameterValue; Message=部署参数“virtualMachineName”的值为空。请指定值或使用参数
    • 好的,根据example 判断,您需要为每个备份项目创建一个非常长的名称(并且每个项目的名称必须不同),所以这肯定是个问题
    • 我尝试将备份部分全部删除,但仍然显示相同的错误
    • 我不确定“备份部分”到底是什么意思。如果您完全删除模板中的最后一个资源(部署资源)会怎样?没有它还能用吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 2020-06-13
    • 2023-04-09
    • 1970-01-01
    相关资源
    最近更新 更多