Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过阵列运行的Azure模板_Azure_Templates_Azure Resource Manager - Fatal编程技术网

通过阵列运行的Azure模板

通过阵列运行的Azure模板,azure,templates,azure-resource-manager,Azure,Templates,Azure Resource Manager,我对使用azure模板进行部署相当陌生,并且一直在开发一个部署一组虚拟机的模板。在模板的第一次迭代中,我将其设置为读取参数文件,并将虚拟机名称指定为参数-此参数用于形成NIC名称以及OS磁盘名称 我现在试图做的是设置模板,根据参数中的数组创建多个虚拟机 我已经设置了参数 "virtualMachineName": { "type": "array", "defaultValue": [ "VM-WEB01",

我对使用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参数为null

如果有人能指出我的错误,我们将不胜感激

这是完整的模板

{
"$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')]"
    }
}

}

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

解决方法应该是:

{
    "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;消息=部署参数“virtualMachineName”的值为空。请指定值或使用参数OK,根据您需要为每个备份项创建一个很长的名称(每个项的名称必须不同),因此这是一个确定的问题。我试图同时删除备份部分,但仍然显示相同的错误我不确定“备份部分”到底是什么意思。如果删除模板中的最后一个资源(部署资源),会发生什么情况?没有它能工作吗?
{
    "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())])]]"
    ]
}