Azure ARM 模板依赖于:资源未在模板中定义

Azure ARM Template dependsOn: Resource Not Defined in Template

我想用 visual studio 2015 在 azure 中为资源组创建一个新的虚拟机。 新虚拟机依赖于同一资源组中的现有资源,该资源未在模板中声明。但我得到了“资源 'Microsoft.Storage/storageAccounts/***' 是 02:21:10 - 未在模板中定义

"resources": [
{
  "apiVersion": "2015-06-15",
  "type": "Microsoft.Compute/virtualMachines",
  "name": "[variables('vmName')]",
  "location": "[resourceGroup().location]",
  "tags": {
    "displayName": "VirtualMachine"
  },
  "dependsOn": [
    "[resourceId('0abb7c58-93b4-45f4-b1be-61a98ac347a3','securitydata','Microsoft.Storage/storageAccounts', parameters('storageAccounts_simscitestrg6892_name'))]"
  ],

DependsOn 只能引用同一 ARM 模板中的资源?

感谢任何帮助。

此致,弗兰克。

不,这没有意义,dependsOn 属性 是为了跟踪 ARM 模板内部的依赖关系,因此它可以按特定顺序提供资源。如果存在资源,则跟踪它是没有意义的。它已经在那里了。你用的时候参考就好了

是的。当您创建依赖于您通过同一模板创建的另一个资源的资源时,将使用 DependsOn。如果资源已经创建,那么您只需添加对它的引用。在您的情况下,您可以像这样为 VM 添加属性键:

"properties": {
                "hardwareProfile": {
                    "vmSize": "Standard_DS1"
                },
                "storageProfile": {
                    "imageReference": {
                        "publisher": "MicrosoftWindowsServerHPCPack",
                        "offer": "WindowsServerHPCPack",
                        "sku": "2012R2",
                        "version": "latest"
                    },
                    "osDisk": {
                        "name": "[parameters('virtualMachines_APP01_name')]",
                        "createOption": "FromImage",
                        "vhd": {
                            "uri": "[concat('https', '://', parameters('storageAccounts_vmdkstorageacct_name'), '.blob.core.windows.net', concat('/vhds/', parameters('virtualMachines_APP01_name'),'APP01.vhd'))]"
                        },
                        "caching": "ReadWrite"
                    },
                    "dataDisks": []
                },
                "osProfile": {
                    "computerName": "[parameters('virtualMachines_APP01_name')]",
                    "adminUsername": "vmadmin",
                    "windowsConfiguration": {
                        "provisionVMAgent": true,
                        "enableAutomaticUpdates": true
                    },
                    "secrets": [],
                    "adminPassword": "[parameters('virtualMachines_APP01_adminPassword')]"
                },
                "networkProfile": {
                    "networkInterfaces": [
                        {
                            "id": "[resourceId('Microsoft.Network/networkInterfaces', parameters('networkInterfaces_app01_name'))]"
                        }
                    ]
                }
            }

DependsOn can only refer to resources in the same ARM template ?

从官方document关于在Azure资源管理器模板中定义依赖关系,我们可以找到如下内容:

Resource Manager evaluates the dependencies between resources, and deploys them in their dependent order. When resources are not dependent on each other, Resource Manager deploys them in parallel. You only need to define dependencies for resources that are deployed in the same template.

根据我的测试,我可以重现这个问题。您需要在模板中添加存储资源,如下所示:

{
    "name": "[parameters('storageAccounts_simscitestrg6892_name')]",
    "type": "Microsoft.Storage/storageAccounts",
    "location": "[resourceGroup().location]",
    "apiVersion": "2015-06-15",
    "dependsOn": [],
    "tags": {
      "displayName": "StorageAccountResourceName"
    },
    "properties": {
      "accountType": "[parameters('StorageAccountType')]"
    }
}

对于您的 VM 资源,您可以在 "properties > storageProfile" 部分下配置 osDisk,如下所示:

"osDisk": {
  "name": "Your-VMOSDisk",
  "vhd": {
    "uri": "[concat('https://', parameters('storageAccounts_simscitestrg6892_name'), '.blob.core.windows.net/', variables('Your-VMStorageAccountContainerName'), '/', variables('Your-VMOSDiskName'), '.vhd')]"
  },
  "caching": "ReadWrite",
  "createOption": "FromImage"
}

存储资源将在与您的 VM 相同的位置下创建(如果不存在)。