ARM 模板:模板中未定义资源

问题描述 投票:0回答:1

任何人都可以帮助我解决我在尝试通过 ARM 模板部署多个资源时遇到的问题吗? 由于我想要部署范围不同级别的资源,因此我使用嵌套的 ARM 模板。我想要部署的资源结构如下所示。 structure of resources

当我尝试从 Azure CLI 部署 ARM 模板时,出现以下错误。

{"code": "InvalidTemplate", "target": "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Resources/deployments/deploymentResourceGroupNetwork", "message": "Deployment template validation failed: 'The resource 'Microsoft.Storage/storageAccounts/{myStorageAccountName}' is not defined in the template. Please see https://aka.ms/arm-syntax for usage details.'.", "additionalInfo": [{"type": "TemplateViolation", "info": {"lineNumber": 0, "linePosition": 0, "path": ""}}]}

我找到了以下类似问题的解决方案,但我不确定它是否适用于我的案例。 具有多个范围的嵌套臂模板

这是我在 Azure CLI 中使用的 ARM 模板文件、参数文件和命令。

ARM template(nested_template.json)

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "env":{
            "type": "String",
            "defaultValue": "devl",
            "allowedValues": [
                "devl",
                "prod"
            ]
        },
        "resourceGroupNetwork": {
            "type": "String",
            "defaultValue": "resourceGroupNetwork_dev",
            "allowedValues": [
                "resourceGroupNetwork_dev",
                "resourceGroupNetwork_prod"
            ]
        },
        "resourceGroupOther": {
            "type": "String",
            "defaultValue": "resourceGroupOther_dev",
            "allowedValues": [
                "resourceGroupOther_dev",
                "resourceGroupOther_prod"
            ]
        }
    },
    "variables": {
        "starageAccountsEnvSettings": {
            "devl": {
                "subscription": "{subscriptionId}",
                "resouceGroupName": "[parameters('resourceGroupOther')]",
                "instanceName": "{instanceName}"
            },
            "prod": {
                "subscription": "{subscriptionId}",
                "resouceGroupName": "[parameters('resourceGroupOther')]",
                "instanceName": "{instanceName}"
            }
        },
        "privateEndpointsStrageBlobEnvSetting": {
            "devl": {
                "subscription": "{subscriptionId}",
                "resouceGroupName": "[parameters('resourceGroupNetwork')]",
                "instanceName": "{instanceName}",
                "networkInterfaceName": "{nicName}",
                "virtualNetworks": "{vNetName}",
                "subnets": "{subnetsName}"
            },
            "prod": {
                "subscription": "{subscriptionId}",
                "resouceGroupName": "[parameters('resourceGroupNetwork')]",
                "instanceName": "{instanceName}",
                "networkInterfaceName": "{nicName}",
                "virtualNetworks": "{vNetName}",
                "subnets": "{subnetsName}"
            }
        },
        "privateEndpointsStrageFilesEnvSetting": {
            "devl": {
                "subscription": "{subscriptionId}",
                "resouceGroupName": "[parameters('resourceGroupNetwork')]",
                "instanceName": "{instanceName}",
                "networkInterfaceName": "{nicName}",
                "virtualNetworks": "{vNetName}",
                "subnets": "{subnetsName}"
            },
            "prod": {
                "subscription": "{subscriptionId}",
                "resouceGroupName": "[parameters('resourceGroupNetwork')]",
                "instanceName": "{instanceName}",
                "networkInterfaceName": "{nicName}",
                "virtualNetworks": "{vNetName}",
                "subnets": "{subnetsName}"
            }
        }
    },
    "resources": [
      {
        "type": "Microsoft.Resources/deployments",
        "apiVersion": "2021-04-01",
        "subscriptionId": "[variables('starageAccountsEnvSettings')[parameters('env')].subscription]",
        "resourceGroup": "[variables('starageAccountsEnvSettings')[parameters('env')].resouceGroupName]",
        "name": "deploymentResourceGroupOther",
        "properties": {
          "mode": "Incremental",
          "template": {
            "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
            "contentVersion": "1.0.0.0",
            "resources": [
                {
                    "type": "Microsoft.Storage/storageAccounts",
                    "apiVersion": "2023-05-01",
                    "name": "[variables('starageAccountsEnvSettings')[parameters('env')].instanceName]",
                    "location": "eastus2",
                    "tags": {},
                    "sku": {
                        "name": "Standard_ZRS",
                        "tier": "Standard"
                    },
                    "kind": "StorageV2",
                    "properties": {
                        "dnsEndpointType": "Standard",
                        "defaultToOAuthAuthentication": true,
                        "publicNetworkAccess": "Disabled",
                        "allowCrossTenantReplication": false,
                        "minimumTlsVersion": "TLS1_2",
                        "allowBlobPublicAccess": false,
                        "allowSharedKeyAccess": true,
                        "largeFileSharesState": "Enabled",
                        "networkAcls": {
                            "bypass": "AzureServices",
                            "virtualNetworkRules": [],
                            "ipRules": [],
                            "defaultAction": "Deny"
                        },
                        "supportsHttpsTrafficOnly": true,
                        "encryption": {
                            "requireInfrastructureEncryption": false,
                            "services": {
                                "file": {
                                    "keyType": "Account",
                                    "enabled": true
                                },
                                "table": {
                                    "keyType": "Account",
                                    "enabled": true
                                },
                                "queue": {
                                    "keyType": "Account",
                                    "enabled": true
                                },
                                "blob": {
                                    "keyType": "Account",
                                    "enabled": true
                                }
                            },
                            "keySource": "Microsoft.Storage"
                        },
                        "accessTier": "Hot"
                   }
                },
                {
                    "type": "Microsoft.Storage/storageAccounts/blobServices",
                    "apiVersion": "2023-05-01",
                    "name": "[concat(variables('starageAccountsEnvSettings')[parameters('env')].instanceName, '/default')]",
                    "dependsOn": [
                        "[resourceId('Microsoft.Storage/storageAccounts', variables('starageAccountsEnvSettings')[parameters('env')].instanceName)]"
                    ],
                    "sku": {
                        "name": "Standard_ZRS",
                        "tier": "Standard"
                    },
                    "properties": {
                        "containerDeleteRetentionPolicy": {
                            "enabled": true,
                            "days": 7
                        },
                        "cors": {
                            "corsRules": []
                        },
                        "deleteRetentionPolicy": {
                            "allowPermanentDelete": false,
                            "enabled": true,
                            "days": 7
                        }
                    }
                },
                {
                    "type": "Microsoft.Storage/storageAccounts/fileServices",
                    "apiVersion": "2023-05-01",
                    "name": "[concat(variables('starageAccountsEnvSettings')[parameters('env')].instanceName, '/default')]",
                    "dependsOn": [
                        "[resourceId('Microsoft.Storage/storageAccounts', variables('starageAccountsEnvSettings')[parameters('env')].instanceName)]"
                    ],
                    "sku": {
                        "name": "Standard_ZRS",
                        "tier": "Standard"
                    },
                    "properties": {
                        "protocolSettings": {
                            "smb": {}
                        },
                        "cors": {
                            "corsRules": []
                        },
                        "shareDeleteRetentionPolicy": {
                            "enabled": true,
                            "days": 7
                        }
                    }
                },
                {
                    "type": "Microsoft.Storage/storageAccounts/privateEndpointConnections",
                    "apiVersion": "2023-05-01",
                    "name": "[concat(variables('starageAccountsEnvSettings')[parameters('env')].instanceName, '/', variables('starageAccountsEnvSettings')[parameters('env')].instanceName, '-strageAccounts')]",
                    "dependsOn": [
                        "[resourceId('Microsoft.Storage/storageAccounts', variables('starageAccountsEnvSettings')[parameters('env')].instanceName)]"
                    ],
                    "properties": {
                        "privateEndpoint": {},
                        "privateLinkServiceConnectionState": {
                            "status": "Approved",
                            "description": "Auto-Approved",
                            "actionRequired": "None"
                        }
                    }
                },
                {
                    "type": "Microsoft.Storage/storageAccounts/queueServices",
                    "apiVersion": "2023-05-01",
                    "name": "[concat(variables('starageAccountsEnvSettings')[parameters('env')].instanceName, '/default')]",
                    "dependsOn": [
                        "[resourceId('Microsoft.Storage/storageAccounts', variables('starageAccountsEnvSettings')[parameters('env')].instanceName)]"
                    ],
                    "properties": {
                        "cors": {
                            "corsRules": []
                        }
                    }
                },
                {
                    "type": "Microsoft.Storage/storageAccounts/tableServices",
                    "apiVersion": "2023-05-01",
                    "name": "[concat(variables('starageAccountsEnvSettings')[parameters('env')].instanceName, '/default')]",
                    "dependsOn": [
                        "[resourceId('Microsoft.Storage/storageAccounts', variables('starageAccountsEnvSettings')[parameters('env')].instanceName)]"
                    ],
                    "properties": {
                        "cors": {
                            "corsRules": []
                        }
                    }
                }
            ]
          }
        }
      },
      {
        "type": "Microsoft.Resources/deployments",
        "apiVersion": "2021-04-01",
        "name": "deploymentResourceGroupNetwork",
        "subscriptionId": "[variables('privateEndpointsStrageBlobEnvSetting')[parameters('env')].subscription]",
        "resourceGroup": "[variables('privateEndpointsStrageBlobEnvSetting')[parameters('env')].resouceGroupName]",
        "properties": {
          "mode": "Incremental",
          "template": {
            "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
            "contentVersion": "1.0.0.0",
            "resources": [
                {
                    "type": "Microsoft.Network/privateEndpoints",
                    "apiVersion": "2024-01-01",
                    "name": "[variables('privateEndpointsStrageBlobEnvSetting')[parameters('env')].instanceName]",
                    "location": "eastus2",
                    "dependsOn": [
                        "[resourceId(variables('starageAccountsEnvSettings')[parameters('env')].subscription, variables('starageAccountsEnvSettings')[parameters('env')].resouceGroupName, 'Microsoft.Storage/storageAccounts', variables('starageAccountsEnvSettings')[parameters('env')].instanceName)]"
                    ],
                    "tags": {},
                    "properties": {
                        "privateLinkServiceConnections": [
                            {
                                "name": "[variables('privateEndpointsStrageBlobEnvSetting')[parameters('env')].instanceName]",
                                "id": "[concat(resourceId('Microsoft.Network/privateEndpoints', variables('privateEndpointsStrageBlobEnvSetting')[parameters('env')].instanceName), concat('/privateLinkServiceConnections/', variables('privateEndpointsStrageBlobEnvSetting')[parameters('env')].instanceName))]",
                                "properties": {
                                    "privateLinkServiceId": "[resourceId(variables('starageAccountsEnvSettings')[parameters('env')].subscription, variables('starageAccountsEnvSettings')[parameters('env')].resouceGroupName, 'Microsoft.Storage/storageAccounts', variables('starageAccountsEnvSettings')[parameters('env')].instanceName)]",
                                    "groupIds": [
                                        "blob"
                                    ],
                                    "privateLinkServiceConnectionState": {
                                        "status": "Approved",
                                        "description": "Auto-Approved",
                                        "actionsRequired": "None"
                                    }
                                }
                            }
                        ],
                        "manualPrivateLinkServiceConnections": [],
                        "customNetworkInterfaceName": "[variables('privateEndpointsStrageBlobEnvSetting')[parameters('env')].networkInterfaceName]",
                        "subnet": {
                            "id": "[concat(variables('privateEndpointsStrageBlobEnvSetting')[parameters('env')].instanceName, '/subnets/', variables('privateEndpointsStrageBlobEnvSetting')[parameters('env')].subnets)]"
                        },
                        "ipConfigurations": [],
                        "customDnsConfigs": []
                    }
                },
                {
                    "type": "Microsoft.Network/privateEndpoints",
                    "apiVersion": "2024-01-01",
                    "name": "[variables('privateEndpointsStrageFilesEnvSetting')[parameters('env')].instanceName]",
                    "location": "eastus2",
                    "dependsOn": [
                        "[resourceId(variables('starageAccountsEnvSettings')[parameters('env')].subscription, variables('starageAccountsEnvSettings')[parameters('env')].resouceGroupName, 'Microsoft.Storage/storageAccounts', variables('starageAccountsEnvSettings')[parameters('env')].instanceName)]"
                    ],
                    "tags": {},
                    "properties": {
                        "privateLinkServiceConnections": [
                            {
                                "name": "[variables('privateEndpointsStrageFilesEnvSetting')[parameters('env')].instanceName]",
                                "id": "[concat(resourceId('Microsoft.Network/privateEndpoints', variables('privateEndpointsStrageFilesEnvSetting')[parameters('env')].instanceName), concat('/privateLinkServiceConnections/', variables('privateEndpointsStrageFilesEnvSetting')[parameters('env')].instanceName))]",
                                "properties": {
                                    "privateLinkServiceId": "[resourceId(variables('starageAccountsEnvSettings')[parameters('env')].subscription, variables('starageAccountsEnvSettings')[parameters('env')].resouceGroupName, 'Microsoft.Storage/storageAccounts', variables('starageAccountsEnvSettings')[parameters('env')].instanceName)]",
                                    "groupIds": [
                                        "file"
                                    ],
                                    "privateLinkServiceConnectionState": {
                                        "status": "Approved",
                                        "description": "Auto-Approved",
                                        "actionsRequired": "None"
                                    }
                                }
                            }
                        ],
                        "manualPrivateLinkServiceConnections": [],
                        "customNetworkInterfaceName": "[variables('privateEndpointsStrageFilesEnvSetting')[parameters('env')].networkInterfaceName]",
                        "subnet": {
                            "id": "[concat(variables('privateEndpointsStrageFilesEnvSetting')[parameters('env')].instanceName, '/subnets/', variables('privateEndpointsStrageFilesEnvSetting')[parameters('env')].subnets)]"
                        },
                        "ipConfigurations": [],
                        "customDnsConfigs": []
                    }
                },   
                {
                    "type": "Microsoft.Network/privateEndpoints/privateDnsZoneGroups",
                    "apiVersion": "2024-01-01",
                    "name": "privatelink.file.core.windows.net/default",
                    "dependsOn": [
                        "[resourceId(variables('privateEndpointsStrageFilesEnvSetting')[parameters('env')].subscription, variables('privateEndpointsStrageFilesEnvSetting')[parameters('env')].resouceGroupName,'Microsoft.Network/privateEndpoints', variables('privateEndpointsStrageFilesEnvSetting')[parameters('env')].instanceName)]"
                    ],
                    "properties": {
                        "privateDnsZoneConfigs": [
                            {
                                "properties": {
                                    "privateDnsZoneId": "[concat('/subscriptions/', variables('privateEndpointsStrageBlobEnvSetting')[parameters('env')].subscription, '/resourceGroups/', variables('privateEndpointsStrageBlobEnvSetting')[parameters('env')].resouceGroupName, '/providers/Microsoft.Network/privateDnsZones/privatelink.file.core.windows.net')]"
                                }
                            }
                        ]
                    }
                }
                
            ]
          }
        }
      }
    ]
  }

parameter file(template_parameter.json)

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "env": {
            "value": "devl"
        },
        "resourceGroupNetwork": {
            "value": "{resourceGroupNetworkName}"
        },
        "resourceGroupOther": {
            "value": "{resourceGroupOtherName}"
        }
    }
}

deploy command

az deployment group create 
    --resource-group {resouceGroupName} 
    --template-file ".\nested_template.json" 
    --parameters "@.\template_parameter.json"
azure azure-resource-manager azure-rm-template
1个回答
0
投票

ARM 模板:模板中未定义资源问题

根据错误描述,您似乎声明了与存储帐户名称相关的错误信息,您需要在“Microsoft.Storage/storageAccounts/{myStorageAccountName}”中进行更改。

我尝试了一个模板,其中根据配置提到了资源并重写它,并且能够提供您正在寻找的配置。

nested_templete.json

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "env": {
            "type": "String",
            "defaultValue": "dev",
            "allowedValues": [
                "dev",
                "prod"
            ]
        },
        "resourceGroupNetwork": {
            "type": "String"
        },
        "resourceGroupStorage": {
            "type": "String"
        }
    },
    "variables": {
        "storageAccountName": "[concat('storacc', uniqueString(resourceGroup().id))]",
        "vnetName": "[concat('vnet-', parameters('env'))]",
        "subnetName": "default"
    },
    "resources": [
        {
            "type": "Microsoft.Resources/deployments",
            "apiVersion": "2021-04-01",
            "name": "nestedStorageDeployment",
            "resourceGroup": "[parameters('resourceGroupStorage')]",
            "properties": {
                "mode": "Incremental",
                "template": {
                    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
                    "contentVersion": "1.0.0.0",
                    "resources": [
                        {
                            "type": "Microsoft.Storage/storageAccounts",
                            "apiVersion": "2023-01-01",
                            "name": "[variables('storageAccountName')]",
                            "location": "[resourceGroup().location]",
                            "sku": {
                                "name": "Standard_LRS"
                            },
                            "kind": "StorageV2",
                            "properties": {
                                "accessTier": "Hot"
                            }
                        }
                    ]
                }
            }
        },
        {
            "type": "Microsoft.Resources/deployments",
            "apiVersion": "2021-04-01",
            "name": "nestedNetworkDeployment",
            "resourceGroup": "[parameters('resourceGroupNetwork')]",
            "properties": {
                "mode": "Incremental",
                "template": {
                    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
                    "contentVersion": "1.0.0.0",
                    "resources": [
                        {
                            "type": "Microsoft.Network/virtualNetworks",
                            "apiVersion": "2022-01-01",
                            "name": "[variables('vnetName')]",
                            "location": "[resourceGroup().location]",
                            "properties": {
                                "addressSpace": {
                                    "addressPrefixes": [
                                        "10.0.0.0/16"
                                    ]
                                },
                                "subnets": [
                                    {
                                        "name": "[variables('subnetName')]",
                                        "properties": {
                                            "addressPrefix": "10.0.0.0/24"
                                        }
                                    }
                                ]
                            }
                        }
                    ]
                }
            }
        }
    ]
}

template_parameter.json:

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "env": {
            "value": "dev"
        },
        "resourceGroupNetwork": {
            "value": "demoNetworkvkResourceGroup"
        },
        "resourceGroupStorage": {
            "value": "demoStoragevkResourceGroup"
        }
    }
}

部署:

enter image description here

enter image description here

参考:

https://learn.microsoft.com/en-us/azure/templates/microsoft.storage/storageaccounts?pivots=deployment-language-arm-template

© www.soinside.com 2019 - 2024. All rights reserved.