| 1 |
#!powershell |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# (c) 2022, John McCall (@lowlydba) |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
#AnsibleRequires -CSharpUtil Ansible.Basic |
| 8 |
#AnsibleRequires -PowerShell ansible_collections.lowlydba.sqlserver.plugins.module_utils._SqlServerUtils |
| 9 |
#Requires -Modules @{ ModuleName="dbatools"; ModuleVersion="2.0.0" } |
| 10 |
|
| 11 |
$ErrorActionPreference = "Stop" |
| 12 |
|
| 13 |
$spec = @{ |
| 14 |
supports_check_mode = $true |
| 15 |
options = @{ |
| 16 |
sql_instance_replica = @{type = 'str'; required = $true } |
| 17 |
sql_username_replica = @{type = 'str'; required = $false } |
| 18 |
sql_password_replica = @{type = 'str'; required = $false; no_log = $true } |
| 19 |
ag_name = @{type = 'str'; required = $true } |
| 20 |
endpoint = @{type = 'str'; required = $false; default = 'hadr_endpoint' } |
| 21 |
endpoint_url = @{type = 'str'; required = $false } |
| 22 |
backup_priority = @{type = 'int'; required = $false; default = 50 } |
| 23 |
failover_mode = @{ |
| 24 |
type = 'str' |
| 25 |
required = $false |
| 26 |
default = 'Manual' |
| 27 |
choices = @('Manual', 'Automatic') |
| 28 |
} |
| 29 |
availability_mode = @{ |
| 30 |
type = 'str' |
| 31 |
required = $false; default = 'AsynchronousCommit' |
| 32 |
choices = @('SynchronousCommit', 'AsynchronousCommit') |
| 33 |
} |
| 34 |
seeding_mode = @{ |
| 35 |
type = 'str' |
| 36 |
required = $false |
| 37 |
default = 'Automatic' |
| 38 |
choices = @('Manual', 'Automatic') |
| 39 |
} |
| 40 |
connection_mode_in_primary_role = @{ |
| 41 |
type = 'str' |
| 42 |
required = $false |
| 43 |
default = 'AllowAllConnections' |
| 44 |
choices = @('AllowReadIntentConnectionsOnly', 'AllowAllConnections') |
| 45 |
} |
| 46 |
connection_mode_in_secondary_role = @{ |
| 47 |
type = 'str' |
| 48 |
required = $false |
| 49 |
default = 'AllowNoConnections' |
| 50 |
choices = @('AllowNoConnections', 'AllowReadIntentConnectionsOnly', 'AllowAllConnections') |
| 51 |
} |
| 52 |
read_only_routing_connection_url = @{ |
| 53 |
type = 'str' |
| 54 |
required = $false |
| 55 |
} |
| 56 |
read_only_routing_list = @{ |
| 57 |
type = 'str' |
| 58 |
required = $false |
| 59 |
} |
| 60 |
cluster_type = @{ |
| 61 |
type = 'str' |
| 62 |
required = $false |
| 63 |
default = 'Wsfc' |
| 64 |
choices = @('Wsfc', 'External', 'None') |
| 65 |
} |
| 66 |
configure_xe_session = @{ type = 'bool'; required = $false; default = $false } |
| 67 |
session_timeout = @{ type = 'int'; required = $false } |
| 68 |
state = @{type = 'str'; required = $false; default = 'present'; choices = @('present', 'absent') } |
| 69 |
} |
| 70 |
required_together = @( |
| 71 |
, @('sql_username_replica', 'sql_password_replica') |
| 72 |
) |
| 73 |
} |
| 74 |
$module = [Ansible.Basic.AnsibleModule]::Create($args, $spec, @(Get-LowlyDbaSqlServerAuthSpec)) |
| 75 |
$sqlInstance, $sqlCredential = Get-SqlCredential -Module $module |
| 76 |
$readOnlyRoutingConnectionUrl = $module.params.read_only_routing_connection_url |
| 77 |
$readOnlyRoutingList = $module.Params.read_only_routing_list |
| 78 |
$failoverMode = $module.Params.failover_mode |
| 79 |
$seedingMode = $module.Params.seeding_mode |
| 80 |
$agName = $module.Params.ag_name |
| 81 |
$clusterType = $module.Params.cluster_type |
| 82 |
$availabilityMode = $module.Params.availability_mode |
| 83 |
$replicaSqlInstance = $module.Params.sql_instance_replica |
| 84 |
$connectionModeInPrimaryRole = $module.Params.connection_mode_in_primary_role |
| 85 |
$connectionModeInSecondaryRole = $module.Params.connection_mode_in_secondary_role |
| 86 |
$configureXESession = $module.Params.configure_xe_session |
| 87 |
$state = $module.Params.state |
| 88 |
[nullable[int]]$sessionTimeout = $module.Params.session_timeout |
| 89 |
$endpoint = $module.Params.endpoint |
| 90 |
$endpointUrl = $module.Params.endpoint_url |
| 91 |
$backupPriority = $module.Params.backup_priority |
| 92 |
|
| 93 |
if ($null -ne $module.Params.sql_username_replica) { |
| 94 |
[securestring]$replicaSecPassword = ConvertTo-SecureString $Module.Params.sql_password_replica -AsPlainText -Force |
| 95 |
[pscredential]$replicaSqlCredential = New-Object System.Management.Automation.PSCredential ($Module.Params.sql_username_replica, $replicaSecPassword) |
| 96 |
} |
| 97 |
if ($null -eq $replicaSqlCredential) { |
| 98 |
$replicaSqlCredential = $sqlCredential |
| 99 |
} |
| 100 |
$module.Result.changed = $false |
| 101 |
$checkMode = $module.CheckMode |
| 102 |
$PSDefaultParameterValues = @{ "*:EnableException" = $true; "*:Confirm" = $false; "*:WhatIf" = $checkMode } |
| 103 |
|
| 104 |
try { |
| 105 |
$replicaInstance = Connect-DbaInstance -SqlInstance $replicaSqlInstance -SqlCredential $replicaSqlCredential |
| 106 |
$allReplicas = Get-DbaAgReplica -SqlInstance $replicaSqlInstance -SqlCredential $replicaSqlCredential -AvailabilityGroup $agName |
| 107 |
$existingReplica = $allReplicas | Where-Object Name -eq $replicaInstance.DomainInstanceName |
| 108 |
|
| 109 |
if ($state -eq "present") { |
| 110 |
$addReplicaSplat = @{ |
| 111 |
SqlInstance = $replicaSqlInstance |
| 112 |
SqlCredential = $replicaSqlCredential |
| 113 |
Endpoint = $endpoint |
| 114 |
AvailabilityMode = $availabilityMode |
| 115 |
FailoverMode = $failoverMode |
| 116 |
BackupPriority = $backupPriority |
| 117 |
ConnectionModeInPrimaryRole = $connectionModeInPrimaryRole |
| 118 |
ConnectionModeInSecondaryRole = $connectionModeInSecondaryRole |
| 119 |
SeedingMode = $seedingMode |
| 120 |
ClusterType = $clusterType |
| 121 |
} |
| 122 |
if ($null -ne $readOnlyRoutingList) { |
| 123 |
$addReplicaSplat.Add("ReadOnlyRoutingList", $readOnlyRoutingList) |
| 124 |
} |
| 125 |
if ($null -ne $readOnlyRoutingConnectionUrl) { |
| 126 |
$addReplicaSplat.Add("ReadOnlyRoutingConnectionUrl", $readOnlyRoutingConnectionUrl) |
| 127 |
} |
| 128 |
if ($null -ne $endpointUrl) { |
| 129 |
$addReplicaSplat.Add("EndpointUrl", $endpointUrl) |
| 130 |
} |
| 131 |
if ($configureXESession -eq $true) { |
| 132 |
$addReplicaSplat.Add("ConfigureXESession", $true) |
| 133 |
} |
| 134 |
if ($null -ne $sessionTimeout) { |
| 135 |
$addReplicaSplat.Add("SessionTimeout", $sessionTimeout) |
| 136 |
} |
| 137 |
|
| 138 |
if ($null -eq $existingReplica) { |
| 139 |
$availabilityGroup = Get-DbaAvailabilityGroup -SqlInstance $sqlInstance -SqlCredential $sqlCredential -AvailabilityGroup $agName |
| 140 |
$output = $availabilityGroup | Add-DbaAgReplica @addReplicaSplat |
| 141 |
$module.Result.changed = $true |
| 142 |
} |
| 143 |
else { |
| 144 |
$compareReplicaProperty = @( |
| 145 |
'AvailabilityMode' |
| 146 |
'FailoverMode' |
| 147 |
'BackupPriority' |
| 148 |
'ConnectionModeInPrimaryRole' |
| 149 |
'ConnectionModeInSecondaryRole' |
| 150 |
'SeedingMode' |
| 151 |
'SessionTimeout' |
| 152 |
'EndpointUrl' |
| 153 |
) |
| 154 |
$setReplicaSplat = @{} |
| 155 |
$addReplicaSplat.GetEnumerator() | Where-Object Key -in $compareReplicaProperty | ForEach-Object { $setReplicaSplat.Add($_.Key, $_.Value) } |
| 156 |
[string[]]$compareProperty = $setReplicaSplat.Keys |
| 157 |
$replicaDiff = Compare-Object -ReferenceObject $setReplicaSplat -DifferenceObject $existingReplica -Property $compareProperty |
| 158 |
$setReplicaSplat.Add("SqlInstance", $sqlInstance) |
| 159 |
$setReplicaSplat.Add("SqlCredential", $sqlCredential) |
| 160 |
$setReplicaSplat.Add("Replica", $existingReplica.Name) |
| 161 |
$setReplicaSplat.Add("AvailabilityGroup", $agName) |
| 162 |
if ($replicaDiff) { |
| 163 |
$output = Set-DbaAgReplica @setReplicaSplat |
| 164 |
$module.Result.changed = $true |
| 165 |
} |
| 166 |
} |
| 167 |
} |
| 168 |
} |
| 169 |
catch { |
| 170 |
$module.FailJson("Configuring Availability Group replica failed: $($_.Exception.Message)") |
| 171 |
} |
| 172 |
try { |
| 173 |
if ($state -eq "absent") { |
| 174 |
if ($null -ne $existingReplica) { |
| 175 |
$output = $existingReplica | Remove-DbaAgReplica |
| 176 |
$module.Result.changed = $true |
| 177 |
} |
| 178 |
} |
| 179 |
} |
| 180 |
catch { |
| 181 |
$module.FailJson("Removing Availability Group replica failed: $($_.Exception.Message)") |
| 182 |
} |
| 183 |
|
| 184 |
try { |
| 185 |
if ($output) { |
| 186 |
$resultData = ConvertTo-SerializableObject -InputObject $output |
| 187 |
$module.Result.data = $resultData |
| 188 |
} |
| 189 |
$module.ExitJson() |
| 190 |
} |
| 191 |
catch { |
| 192 |
$module.FailJson("Error parsing results - operation likely completed: $($_.Exception.Message)") |
| 193 |
} |