Debian Codegraph @

ansible_collections/lowlydba/sqlserver/plugins/modules/availability_group.ps1

raw
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_secondary = @{type = "str"; required = $false }
17 sql_username_secondary = @{type = 'str'; required = $false }
18 sql_password_secondary = @{type = 'str'; required = $false; no_log = $true }
19 database = @{type = "str"; required = $false; aliases = @('database_name') }
20 ag_name = @{type = "str"; required = $true }
21 all_ags = @{type = "bool"; required = $false; }
22 shared_path = @{type = "str"; required = $false; default = $null }
23 dtc_support_enabled = @{type = "bool"; required = $false; }
24 basic_availability_group = @{type = "bool"; required = $false; }
25 contained_availability_group = @{type = "bool"; required = $false; }
26 database_health_trigger = @{type = "bool"; required = $false; }
27 is_distributed_ag = @{type = "bool"; required = $false; }
28 use_last_backup = @{type = "bool"; required = $false; }
29 healthcheck_timeout = @{type = "int"; required = $false; }
30 availability_mode = @{
31 type = "str"
32 required = $false
33 default = "SynchronousCommit"
34 choices = @("SynchronousCommit", "AsynchronousCommit")
35 }
36 failure_condition_level = @{
37 type = "str"
38 required = $false
39 choices = @(
40 "OnAnyQualifiedFailureCondition",
41 "OnCriticalServerErrors",
42 "OnModerateServerErrors",
43 "OnServerDown",
44 "OnServerUnresponsive"
45 )
46 }
47 failover_mode = @{
48 type = "str"
49 required = $false
50 default = "Automatic"
51 choices = @("Manual", "Automatic")
52 }
53 seeding_mode = @{
54 type = "str"
55 required = $false
56 default = "Manual"
57 choices = @("Manual", "Automatic")
58 }
59 automated_backup_preference = @{
60 type = "str"
61 required = $false
62 default = "Secondary"
63 choices = @("None", "Primary", "Secondary", "SecondaryOnly")
64 }
65 cluster_type = @{
66 type = "str"
67 required = $false
68 default = "Wsfc"
69 choices = @("Wsfc", "External", "None")
70 }
71 allow_null_backup = @{type = "bool"; required = $false }
72 force = @{type = "bool"; required = $false }
73 state = @{type = "str"; required = $false; default = "present"; choices = @("present", "absent") }
74 }
75 required_together = @(
76 , @('sql_username_secondary', 'sql_password_secondary')
77 )
78 }
79
80 # Setup var
81 $module = [Ansible.Basic.AnsibleModule]::Create($args, $spec, @(Get-LowlyDbaSqlServerAuthSpec))
82 $ProgressPreference = "SilentlyContinue"
83
84 # Var
85 $sqlInstance, $sqlCredential = Get-SqlCredential -Module $module
86 $secondary = $module.Params.sql_instance_secondary
87 if ($null -ne $module.Params.sql_username_secondary) {
88 [securestring]$secondarySecPassword = ConvertTo-SecureString $Module.Params.sql_password_secondary -AsPlainText -Force
89 [pscredential]$secondarySqlCredential = New-Object System.Management.Automation.PSCredential ($Module.Params.sql_username_secondary, $secondarySecPassword)
90 }
91 $agName = $module.Params.ag_name
92 $database = $module.Params.database
93 $seedingMode = $module.Params.seeding_mode
94 $sharedPath = $module.Params.shared_path
95 $healthCheckTimeout = $module.Params.healthcheck_timeout
96 $availabilityMode = $module.Params.availability_mode
97 $failureConditionLevel = $module.Params.failure_condition_level
98 $failoverMode = $module.Params.failover_mode
99 $automatedBackupPreference = $module.Params.automated_backup_preference
100 $clusterType = $module.Params.cluster_type
101 $state = $module.Params.state
102 [nullable[bool]]$all_ags = $module.Params.all_ags
103 [nullable[bool]]$useLastBackup = $module.Params.use_last_backup
104 [nullable[bool]]$dtcSupportEnabled = $module.Params.dtc_support_enabled
105 [nullable[bool]]$basicAvailabilityGroup = $module.Params.basic_availability_group
106 [nullable[bool]]$containedAvailabilityGroup = $module.Params.contained_availability_group
107 [nullable[bool]]$databaseHealthTrigger = $module.Params.database_health_trigger
108 [nullable[bool]]$isDistributedAg = $module.Params.is_distributed_ag
109 [nullable[bool]]$force = $module.Params.force
110 [nullable[bool]]$allowNullBackup = $module.Params.allow_null_backup
111 $checkMode = $module.CheckMode
112 $module.Result.changed = $false
113 $PSDefaultParameterValues = @{ "*:EnableException" = $true; "*:Confirm" = $false; "*:WhatIf" = $checkMode }
114
115 try {
116 $existingAG = Get-DbaAvailabilityGroup -SqlInstance $sqlInstance -SqlCredential $sqlCredential -AvailabilityGroup $agName
117
118 if ($state -eq "present") {
119 $agSplat = @{
120 Primary = $sqlInstance
121 PrimarySqlCredential = $sqlCredential
122 Name = $agName
123 SeedingMode = $seedingMode
124 FailoverMode = $failoverMode
125 AvailabilityMode = $availabilityMode
126 AutomatedBackupPreference = $automatedBackupPreference
127 ClusterType = $clusterType
128 }
129 if ($null -ne $sharedPath -and $seedingMode -eq "Manual") {
130 $agSplat.Add("SharedPath", $sharedPath)
131 }
132 if ($useLastBackup -eq $true) {
133 $agSplat.Add("UseLastBackup", $useLastBackup)
134 }
135 if ($dtcSupportEnabled -eq $true) {
136 $agSplat.Add("DtcSupport", $dtcSupportEnabled)
137 }
138 if ($basicAvailabilityGroup -eq $true) {
139 $agSplat.Add("Basic", $basicAvailabilityGroup)
140 }
141 if ($containedAvailabilityGroup -eq $true) {
142 $agSplat.Add("IsContained", $containedAvailabilityGroup)
143 }
144 if ($databaseHealthTrigger -eq $true) {
145 $agSplat.Add("DatabaseHealthTrigger", $databaseHealthTrigger)
146 }
147 if ($null -ne $healthCheckTimeout) {
148 $agSplat.Add("HealthCheckTimeout", $healthCheckTimeout)
149 }
150 if ($null -ne $failureConditionLevel) {
151 $agSplat.Add("FailureConditionLevel", $failureConditionLevel)
152 }
153 if ($null -ne $database) {
154 $agSplat.Add("Database", $database)
155 }
156 if ($null -ne $secondary) {
157 $agSplat.Add("Secondary", $secondary)
158 }
159 if ($null -ne $secondarySqlCredential) {
160 $agSplat.Add("SecondarySqlCredential", $secondarySqlCredential)
161 }
162 if ($force -eq $true) {
163 $agSplat.Add("Force", $force)
164 }
165
166 # Create the AG with initial replica(s)
167 if ($null -eq $existingAG) {
168 # Full backup requirement for new AG via automatic seeding
169 if ($seedingMode -eq "automatic" -and $null -ne $database) {
170 $dbBackup = Get-DbaLastBackup -SqlInstance $sqlInstance -SqlCredential $sqlCredential -Database $database
171 if ($null -eq $dbBackup.LastFullBackup -and $allowNullBackup -eq $true) {
172 $backupSplat = @{
173 SqlInstance = $sqlInstance
174 SqlCredential = $sqlCredential
175 Database = $database
176 FilePath = "NUL"
177 Type = "Full"
178 }
179 $null = Backup-DbaDatabase @backupSplat
180 }
181 }
182 $output = New-DbaAvailabilityGroup @agSplat
183 $module.Result.changed = $true
184 }
185 # Configure existing AG
186 else {
187 $setAgSplat = @{
188 AutomatedBackupPreference = $automatedBackupPreference
189 ClusterType = $clusterType
190 }
191 if ($all_ags -eq $true) {
192 $setAgSplat.Add("AllAvailabilityGroups", $all_ags)
193 }
194 if ($dtcSupportEnabled -eq $true) {
195 $setAgSplat.Add("DtcSupportEnabled", $dtcSupportEnabled)
196 }
197 if ($basicAvailabilityGroup -eq $true) {
198 $setAgSplat.Add("BasicAvailabilityGroup", $basicAvailabilityGroup)
199 }
200 if ($databaseHealthTrigger -eq $true) {
201 $setAgSplat.Add("DatabaseHealthTrigger", $databaseHealthTrigger)
202 }
203 if ($null -ne $failureConditionLevel) {
204 $setAgSplat.Add("FailureConditionLevel", $failureConditionLevel)
205 }
206 if ($null -ne $healthCheckTimeout) {
207 $setAgSplat.Add("HealthCheckTimeout", $healthCheckTimeout)
208 }
209 if ($isDistributedAg -eq $true) {
210 $setAgSplat.Add("IsDistributedAvailabilityGroup", $isDistributedAg)
211 }
212 $compareProperty = ($existingAG.Properties | Where-Object Name -in $setAgSplat.Keys).Name
213 $agDiff = Compare-Object -ReferenceObject $existingAG -DifferenceObject $setAgSplat -Property $compareProperty
214 if ($null -ne $agDiff) {
215 $output = $existingAG | Set-DbaAvailabilityGroup @setAgSplat
216 $module.Result.changed = $true
217 }
218 }
219 }
220 elseif ($state -eq "absent") {
221 if ($null -ne $existingAG) {
222 if ($all_ags -eq $true) {
223 $existingAG | Remove-DbaAvailabilityGroup -AllAvailabilityGroups
224 }
225 else {
226 $existingAG | Remove-DbaAvailabilityGroup
227 }
228 $module.Result.changed = $true
229 }
230 }
231
232 if ($output) {
233 $resultData = ConvertTo-SerializableObject -InputObject $output
234 $module.Result.data = $resultData
235 }
236 $module.ExitJson()
237 }
238 catch {
239 $module.FailJson("Configuring Availability Group failed: $($_.Exception.Message)", $_)
240 }