在指定资源组的特定订阅下部署没有 DependsOn 的 ARM 模板

Deploy ARM Templates Without DependsOn under Specific Subscription of Specified resource group

我想单独部署 ARM 模板而不是链接 templates/nested templates/multiple 资源。

例如:部署 Sql 服务器和数据库

我为 Sql 服务器和 Sql 数据库创建了单独的模板。我部署成功并且工作正常。

  1. 部署Sql服务器ARM模板(专为sql服务器设计)
  2. 部署Sql服务器数据库(专门为sql数据库设计,在参数文件中提到上述sql服务器名称)

在部署 Sql 数据库 arm 模板时,我在步骤 2 的参数文件中指定了准确的 sql 服务器名称(在步骤 1 中部署),但我没有在资源部分和直接部署。在我部署过程中选择的资源组下成功创建数据库。

我的查询:

如何确保 Sql 数据库 arm 模板仅在没有 dependson 参数的情况下部署在特定服务器名称下(第 1 步)?

如何在没有依赖的情况下在资源部分使用现有的参考资源id?

sql 服务器的 step1(resourceid) 的输出会有帮助吗?

Sql ARM 模板部署 JSON:

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "location": {
            "type": "string",
            "defaultValue": "[resourceGroup().location]",
            "metadata": {
                "description": "Location for all resources."
            }
        },
        "collation": {
            "type": "string",
            "defaultValue": "SQL_Latin1_General_CP1_CI_AS",
            "metadata": {
                "description": "The collation of the database."
            }
        },
        "edition": {
            "type": "string",
            "metadata": {
                "description": "The edition of the database. The DatabaseEditions enumeration contains all the valid editions. e.g. Basic, Premium."
            },
            "defaultValue": "Basic"
        },
        "sqlservername": {
            "type": "string",
            "metadata": {
                "description": "The name of the sql server."
            }
        },
        "databasename": {
            "type": "string",
            "metadata": {
                "description": "The name of the database to be operated on (updated or created)."
            },
            "minLength": 7,
            "maxLength": 128
        },
        "maxSizeBytes": {
            "type": "string",
            "metadata": {
                "description": "The max size of the database expressed in bytes."
            }
        },
        "serviceobjectivename": {
            "type": "string",
            "metadata": {
                "description": "The configured service level objective ID of the database. This is the service level objective that is in the process of being applied to the database."
            },
            "defaultValue": "Basic"
        },
        "tagsArray": {
            "type": "object",
            "metadata": {
                "description": "Resource Tags helps to indentify the use of service"
            }
        }
    },
    "functions": [],
    "variables": {
        "sqldatabasename": "[concat(parameters('sqlservername'),'/',parameters('databasename'))]"
    },
    "resources": [
        {
            "name": "[variables('sqldatabasename')]",
            "type": "Microsoft.Sql/servers/databases",
            "apiVersion": "2014-04-01",
            "location": "[parameters('location')]",
            "tags": "[parameters('tagsArray')]",
            "properties": {
                "collation": "[parameters('collation')]",
                "edition": "[parameters('edition')]",
                "maxSizeBytes": "[parameters('maxSizeBytes')]",
                "requestedServiceObjectiveName": "[parameters('serviceobjectivename')]"
            }
        }
    ],
    "outputs": {
        "sqldatabaseresourceId": {
            "type": "object",
            "value": "[reference(resourceId('Microsoft.Sql/servers/databases',parameters('sqlservername'), parameters('databasename')),'2014-04-01')]"
        }
    }
}

模板参数:

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "tagsArray": {
            "value": {
                "Environment": "POC"
            }
        },
        "servername": {
            "value": "sql-test"
        },
        "sqlAdministratorLogin": {
            "value": "sqladmin"
        },
        "sqlAdministratorLoginPassword": {
            "value": "myPassword586@"
        },
        "firewallIpAddresses": {
            "value": [
                {
                    "start": "1.1.1.0",
                    "end": "1.1.1.1",
                    "clientName": "Clienttest1"
                },
                {
                    "start": "1.2.3.4",
                    "end": "1.2.3.16",
                    "clientName": "Clienttest2"
                }
            ]
        },
        "location": {
            "value": ""
        }
    }
}

了解 dependsOn 使您能够将一个资源定义为依赖于一个或多个资源 仅在您的模板中 很重要,并且 不应该 用于映射资源之间的关系。如 ARM Template documentation 中所述,dependsOn 不是记录资源互连方式的正确方法。

How can I make sure Sql database arm template is deploying under specific server name(Step1) only without dependson parameter?

Microsoft.Sql/servers/databasesMicrosoft.Sql/servers 的子资源,它是 定义与父资源的连接的子资源的名称

如果在 相同的 ARM 模板 中的父资源之后部署子资源,则必须设置 dependsOn 属性,因为隐式部署依赖不会在它们之间自动创建。在这种情况下指定 dependsOn 将确保父资源在子资源之前部署(并存在)。

也就是说,当定义 outside of the parent resource 时,您使用斜杠格式化类型和名称以包括父类型和名称。

"type": "{resource-provider-namespace}/{parent-resource-type}/{child-resource-type}",
"name": "{parent-resource-name}/{child-resource-name}",

因此,SQL 数据库可能定义为:

{
  "type": "Microsoft.Sql/servers/databases",
  "name": "[concat(variables('sqlServerName'), '/', parameters('databaseName'))]",
  ...

进入你的下一个问题:

How to use existing reference resource id in resources section without dependson?

Will output of step1(resourceid) of sql server will be any helpful?

因为您已经在不同的模板中部署了父资源(数据库服务器),所以不要设置依赖项。相反,将子资源(数据库)部署到 same 资源组并提供父资源的 name。应该够了。

示例:

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "sqlServerName": {
            "type": "string",
            "defaultValue": "sqlserver"
        },
        "databaseName": {
            "type": "string",
            "defaultValue": "mydb"
        }
    },
    "resources": [
        {
            "type": "Microsoft.Sql/servers/databases",
            "apiVersion": "2014-04-01",
            "name": "[concat(parameters('sqlServerName'), '/', parameters('databaseName'))]",
            "location": "[resourceGroup().location]",
            "properties": {
                "collation": "SQL_Latin1_General_CP1_CI_AS",
                "edition": "Basic"
            }
        }
    ],
    "outputs": {}
}