gpt4 book ai didi

使用 Bicep 的 Azure SQL 故障转移组

转载 作者:行者123 更新时间:2023-12-02 23:35:40 27 4
gpt4 key购买 nike

我正在尝试使用 Bicep 模板实现 Azure SQL 故障转移组。我遇到的问题是,我无法弄清楚如何引用作为故障转移组的数据库属性中的参数传递的数据库数组。当将数组值分配给数据库属性时,它给我一个错误,即期望字符串值,而分配的是数组,这当然是不正确的。

resource symbolicname 'Microsoft.Sql/servers/failoverGroups@2021-11-01-preview' = {
name: 'string'
tags: {
tagName1: 'tagValue1'
tagName2: 'tagValue2'
}
parent: resourceSymbolicName
properties: {
databases: [
'string'
]
partnerServers: [
{
id: 'string'
}
]
readWriteEndpoint: {
failoverPolicy: 'string'
failoverWithDataLossGracePeriodMinutes: int
}
}
}

有人有任何使用 Bicep 模板的 Azure SQL 故障转移组的工作示例,或者为我指出解决方案的正确方向吗?

编辑 1 - main.bicep :这是添加托马斯的更改后我正在尝试的代码:

param databases array
param primarySqlServerName string
param drSqlServerName string
param failovergroupName string

module primarySql '***************' = {
Deploy primary SQL Server and database
}
module drSql '****************' = {
Deploy secondary SQL Server
}

resource sqlServerFailoverGroup 'Microsoft.Sql/servers/failoverGroups@2020-11-01-preview' = {
name: '${primarySqlServerName}/${failovergroupName}'
properties: {
databases: [for database in databases: resourceId('Microsoft.Sql/servers/databases', primarySqlServerName, database)]
readWriteEndpoint: {
failoverPolicy: 'Automatic'
failoverWithDataLossGracePeriodMinutes: 60
}
readOnlyEndpoint: {
failoverPolicy: 'Enabled'
}
partnerServers: [
{
id: resourceId('Microsoft.Sql/servers', drSqlServerName)
}
]
}
dependsOn: [
primarySql
drSql
]
}

在上面部署时出现以下错误:

Unable to process template language expressions for resource at line'1' and column '1289'. 'Unable to evaluate template language function'resourceId': all function arguments must be string literals. Pleasesee aka.ms/arm-template-expressions/#resourceid for usage details.

最佳答案

databases 属性是一个字符串。 partnerServers 属性是一个对象数组。

两者都需要数据库和合作伙伴服务器的资源 ID。

这是一个使用故障转移组部署 2 个 SQL 服务器和数据库的简单示例

sql.bicep:

param location string = resourceGroup().location
param sqlServerName string
param sqlServerPrincipalType string
param sqlServerAadAdminName string
param sqlServerAadAdminId string
param databaseNames array

// Create the Server
resource sqlServer 'Microsoft.Sql/servers@2020-11-01-preview' = {
name: sqlServerName
location: location
tags: {}
properties: {
administrators: {
administratorType: 'ActiveDirectory'
principalType: sqlServerPrincipalType
login: sqlServerAadAdminName
sid: sqlServerAadAdminId
azureADOnlyAuthentication: true
}
}
}

// We create the database only in the primary region
resource database 'Microsoft.Sql/servers/databases@2020-08-01-preview' = [for databaseName in databaseNames: if (!empty(databaseNames)) {
name: empty(databaseNames) ? 'placeholder' : databaseName
parent: sqlServer
location: location
sku: {
name: 'S0'
tier: 'Standard'
}
properties: {
sourceDatabaseId: sqlServer.id
}
}]

和 main.bicep 文件:

param failoverGroupName string
param primarySqlServerName string
param primaryLocation string
param secondarySqlServerName string
param secondaryLocation string
param sqlServerPrincipalType string
param sqlServerAadAdminName string
param sqlServerAadAdminId string

param databaseNames array

module primarySql 'sql.bicep' = {
name: 'primarySql'
params: {
location: primaryLocation
sqlServerName: primarySqlServerName
sqlServerPrincipalType: sqlServerPrincipalType
sqlServerAadAdminName: sqlServerAadAdminName
sqlServerAadAdminId: sqlServerAadAdminId
databaseNames: databaseNames
}
}

module secondarySql 'sql.bicep' = {
name: 'secondarySql'
params: {
location: secondaryLocation
sqlServerName: secondarySqlServerName
sqlServerPrincipalType: sqlServerPrincipalType
sqlServerAadAdminName: sqlServerAadAdminName
sqlServerAadAdminId: sqlServerAadAdminId
databaseNames: []
}
}

resource sqlServerFailoverGroup 'Microsoft.Sql/servers/failoverGroups@2020-11-01-preview' = {
name: '${primarySqlServerName}/${failoverGroupName}'
dependsOn: [ primarySql, secondarySql ]
properties: {
databases: [for dataBaseName in databaseNames: resourceId('Microsoft.Sql/servers/databases', primarySqlServerName, dataBaseName)]
readWriteEndpoint: {
failoverPolicy: 'Automatic'
failoverWithDataLossGracePeriodMinutes: 60
}
readOnlyEndpoint: {
failoverPolicy: 'Enabled'
}
partnerServers: [
{
id: resourceId(resourceGroup().name, 'Microsoft.Sql/servers', secondarySqlServerName)
}
]
}
}

关于使用 Bicep 的 Azure SQL 故障转移组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72703163/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com