| 1 |
#!powershell |
| 2 |
|
| 3 |
# Copyright (c) 2022 Ansible Project |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
#AnsibleRequires -CSharpUtil Ansible.Basic |
| 7 |
|
| 8 |
$spec = @{ |
| 9 |
options = @{ |
| 10 |
database_path = @{ |
| 11 |
type = 'path' |
| 12 |
} |
| 13 |
dns_domain_name = @{ |
| 14 |
type = 'str' |
| 15 |
} |
| 16 |
domain_admin_password = @{ |
| 17 |
no_log = $true |
| 18 |
required = $true |
| 19 |
type = 'str' |
| 20 |
} |
| 21 |
domain_admin_user = @{ |
| 22 |
required = $true |
| 23 |
type = 'str' |
| 24 |
} |
| 25 |
domain_log_path = @{ |
| 26 |
# FUTURE: Add alias for log_path once some time has passed |
| 27 |
type = 'path' |
| 28 |
} |
| 29 |
install_dns = @{ |
| 30 |
type = 'bool' |
| 31 |
} |
| 32 |
install_media_path = @{ |
| 33 |
type = 'path' |
| 34 |
} |
| 35 |
local_admin_password = @{ |
| 36 |
no_log = $true |
| 37 |
type = 'str' |
| 38 |
} |
| 39 |
read_only = @{ |
| 40 |
default = $false |
| 41 |
type = 'bool' |
| 42 |
} |
| 43 |
reboot = @{ |
| 44 |
default = $false |
| 45 |
type = 'bool' |
| 46 |
} |
| 47 |
reboot_timeout = @{ |
| 48 |
default = 600 |
| 49 |
type = 'int' |
| 50 |
} |
| 51 |
replication_source_dc = @{ |
| 52 |
type = 'str' |
| 53 |
} |
| 54 |
safe_mode_password = @{ |
| 55 |
no_log = $true |
| 56 |
type = 'str' |
| 57 |
} |
| 58 |
site_name = @{ |
| 59 |
type = 'str' |
| 60 |
} |
| 61 |
state = @{ |
| 62 |
choices = 'domain_controller', 'member_server' |
| 63 |
required = $true |
| 64 |
type = 'str' |
| 65 |
} |
| 66 |
sysvol_path = @{ |
| 67 |
type = 'path' |
| 68 |
} |
| 69 |
} |
| 70 |
required_if = @( |
| 71 |
, @('state', 'domain_controller', @('dns_domain_name', 'safe_mode_password')) |
| 72 |
, @('state', 'member_server', @(, 'local_admin_password')) |
| 73 |
) |
| 74 |
supports_check_mode = $true |
| 75 |
} |
| 76 |
$module = [Ansible.Basic.AnsibleModule]::Create($args, $spec) |
| 77 |
|
| 78 |
$module.Result.reboot_required = $false |
| 79 |
$module.Result._do_action_reboot = $false # Used by action plugin |
| 80 |
|
| 81 |
$databasePath = $module.Params.database_path |
| 82 |
$dnsDomainName = $module.Params.dns_domain_name |
| 83 |
$installDns = $module.Params.install_dns |
| 84 |
$installMediaPath = $module.Params.install_media_path |
| 85 |
$logPath = $module.Params.domain_log_path |
| 86 |
$readOnly = $module.Params.read_only |
| 87 |
$replicationSourceDC = $module.Params.replication_source_dc |
| 88 |
$siteName = $module.Params.site_name |
| 89 |
$state = $module.Params.state |
| 90 |
$sysvolPath = $module.Params.sysvol_path |
| 91 |
|
| 92 |
$domainCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList @( |
| 93 |
$module.Params.domain_admin_user, |
| 94 |
(ConvertTo-SecureString -AsPlainText -Force -String $module.Params.domain_admin_password) |
| 95 |
) |
| 96 |
|
| 97 |
if ([System.Environment]::OSVersion.Version -lt [Version]"6.2") { |
| 98 |
$module.FailJson("microsoft.ad.domain_controller requires Windows Server 2012 or higher") |
| 99 |
} |
| 100 |
|
| 101 |
# short-circuit "member server" check, since we don't need feature checks for this... |
| 102 |
# role 4/5 - backup/primary DC |
| 103 |
$win32CS = Get-CimInstance -ClassName Win32_ComputerSystem -Property Domain, DomainRole |
| 104 |
$isKdc = $win32CS.DomainRole -in @(4, 5) |
| 105 |
If ($state -eq "member_server" -and -not $isKdc) { |
| 106 |
$module.ExitJson() |
| 107 |
} |
| 108 |
|
| 109 |
# all other operations will require the AD-DS and RSAT-ADDS features... |
| 110 |
$requiredFeatures = @("AD-Domain-Services", "RSAT-ADDS") |
| 111 |
$features = Get-WindowsFeature -Name $requiredFeatures |
| 112 |
$unavailableFeatures = Compare-Object -ReferenceObject $requiredFeatures -DifferenceObject $features.Name -PassThru |
| 113 |
|
| 114 |
if ($unavailableFeatures) { |
| 115 |
$module.FailJson("The following features required for a domain controller are unavailable: $($unavailableFeatures -join ',')") |
| 116 |
} |
| 117 |
|
| 118 |
$missingFeatures = $features | Where-Object InstallState -NE Installed |
| 119 |
if ($missingFeatures) { |
| 120 |
$res = Install-WindowsFeature -Name $missingFeatures -WhatIf:$module.CheckMode |
| 121 |
$module.Result.changed = $true |
| 122 |
$module.Result.reboot_required = [bool]$res.RestartNeeded |
| 123 |
|
| 124 |
# When in check mode and the prereq was "installed" we need to exit early as |
| 125 |
# the AD cmdlets weren't really installed |
| 126 |
if ($module.CheckMode) { |
| 127 |
$module.ExitJson() |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
$lastBootTime = (Get-CimInstance -ClassName Win32_OperatingSystem -Property LastBootUpTime).LastBootUpTime.ToFileTime() |
| 132 |
|
| 133 |
if ($state -eq 'domain_controller') { |
| 134 |
# ensure that domain admin user is in UPN or down-level domain format (prevent hang from https://support.microsoft.com/en-us/kb/2737935) |
| 135 |
If (-not $domainCredential.UserName.Contains("\") -and -not $domainCredential.UserName.Contains("@")) { |
| 136 |
$module.FailJson("domain_admin_user must be in domain\user or [email protected] format") |
| 137 |
} |
| 138 |
|
| 139 |
If ($isKdc) { |
| 140 |
# FUTURE: implement managed Remove/Add to change domains? |
| 141 |
If ($dnsDomainName -ne $win32CS.Domain) { |
| 142 |
$msg = -join @( |
| 143 |
"The host $env:COMPUTERNAME is a domain controller for the domain $($win32CS.Domain); " |
| 144 |
"changing DC domains is not implemented" |
| 145 |
) |
| 146 |
$module.FailJson($msg) |
| 147 |
} |
| 148 |
} |
| 149 |
else { |
| 150 |
$safeModePassword = $module.Params.safe_mode_password | ConvertTo-SecureString -AsPlainText -Force |
| 151 |
|
| 152 |
$installParams = @{ |
| 153 |
Confirm = $false |
| 154 |
Credential = $domainCredential |
| 155 |
DomainName = $dnsDomainName |
| 156 |
Force = $true |
| 157 |
NoRebootOnCompletion = $true |
| 158 |
SafeModeAdministratorPassword = $safeModePassword |
| 159 |
SkipPreChecks = $true |
| 160 |
WhatIf = $module.CheckMode |
| 161 |
} |
| 162 |
if ($databasePath) { |
| 163 |
$installParams.DatabasePath = $databasePath |
| 164 |
} |
| 165 |
if ($logPath) { |
| 166 |
$installParams.LogPath = $logPath |
| 167 |
} |
| 168 |
if ($sysvolPath) { |
| 169 |
$installParams.SysvolPath = $sysvolPath |
| 170 |
} |
| 171 |
if ($installMediaPath) { |
| 172 |
$installParams.InstallationMediaPath = $installMediaPath |
| 173 |
} |
| 174 |
if ($readOnly) { |
| 175 |
# while this is a switch value, if we set on $false site_name is required |
| 176 |
# https://github.com/ansible/ansible/issues/35858 |
| 177 |
$installParams.ReadOnlyReplica = $true |
| 178 |
} |
| 179 |
if ($replicationSourceDC) { |
| 180 |
$installParams.ReplicationSourceDC = $replicationSourceDC |
| 181 |
} |
| 182 |
if ($siteName) { |
| 183 |
$installParams.SiteName = $siteName |
| 184 |
} |
| 185 |
if ($null -ne $installDns) { |
| 186 |
$installParams.InstallDns = $installDns |
| 187 |
} |
| 188 |
|
| 189 |
try { |
| 190 |
$null = Install-ADDSDomainController @installParams |
| 191 |
} |
| 192 |
catch [Microsoft.DirectoryServices.Deployment.DCPromoExecutionException] { |
| 193 |
# ExitCode 15 == 'Role change is in progress or this computer needs to be restarted.' |
| 194 |
# DCPromo exit codes details can be found at |
| 195 |
# https://docs.microsoft.com/en-us/windows-server/identity/ad-ds/deploy/troubleshooting-domain-controller-deployment |
| 196 |
if ($_.Exception.ExitCode -in @(15, 19)) { |
| 197 |
$module.Result.reboot_required = $true |
| 198 |
$module.Result._do_action_reboot = $true |
| 199 |
} |
| 200 |
|
| 201 |
$module.FailJson("Failed to install ADDSDomainController, DCPromo exited with $($_.Exception.ExitCode): $($_.Exception.Message)", $_) |
| 202 |
} |
| 203 |
finally { |
| 204 |
# The Netlogon service is set to auto start but is not started. This is |
| 205 |
# required for Ansible to connect back to the host and reboot in a |
| 206 |
# later task. Even if this fails Ansible can still connect but only |
| 207 |
# with ansible_winrm_transport=basic so we just display a warning if |
| 208 |
# this fails. |
| 209 |
if (-not $module.CheckMode) { |
| 210 |
try { |
| 211 |
Start-Service -Name Netlogon |
| 212 |
} |
| 213 |
catch { |
| 214 |
$msg = -join @( |
| 215 |
"Failed to start the Netlogon service after promoting the host, " |
| 216 |
"Ansible may be unable to connect until the host is manually rebooting: $($_.Exception.Message)" |
| 217 |
) |
| 218 |
$module.Warn($msg) |
| 219 |
} |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
$module.Result.changed = $true |
| 224 |
$module.Result.reboot_required = $true |
| 225 |
} |
| 226 |
} |
| 227 |
else { |
| 228 |
# at this point we already know we're a DC and shouldn't be (due to short circuit check)... |
| 229 |
$assignedRoles = @((Get-ADDomainController -Server localhost).OperationMasterRoles) |
| 230 |
$localAdminPassword = $module.Params.local_admin_password | ConvertTo-SecureString -AsPlainText -Force |
| 231 |
|
| 232 |
# FUTURE: figure out a sane way to hand off roles automatically (designated recipient server, randomly look one up?) |
| 233 |
If ($assignedRoles.Count -gt 0) { |
| 234 |
$msg = -join @( |
| 235 |
"This domain controller has operation master role(s) ({0}) assigned; they must be moved to other " |
| 236 |
"DCs before demotion (see Move-ADDirectoryServerOperationMasterRole)" -f ($assignedRoles -join ", ") |
| 237 |
) |
| 238 |
$module.FailJson($msg) |
| 239 |
} |
| 240 |
|
| 241 |
# While the cmdlet has -WhatIf, it doesn't seem to work properly. Only run |
| 242 |
# when not in check mode. |
| 243 |
if (-not $module.CheckMode) { |
| 244 |
$uninstallParams = @{ |
| 245 |
Confirm = $false |
| 246 |
Credential = $domainCredential |
| 247 |
Force = $true |
| 248 |
LocalAdministratorPassword = $localAdminPassword |
| 249 |
NoRebootOnCompletion = $true |
| 250 |
} |
| 251 |
$null = Uninstall-ADDSDomainController @uninstallParams |
| 252 |
} |
| 253 |
|
| 254 |
$module.Result.changed = $true |
| 255 |
$module.Result.reboot_required = $true |
| 256 |
} |
| 257 |
|
| 258 |
if ($module.Result.reboot_required -and $module.Params.reboot -and -not $module.CheckMode) { |
| 259 |
# Promoting or depromoting puts the server in a very funky state and it may |
| 260 |
# not be possible for Ansible to connect back without a reboot is done. If |
| 261 |
# the user requested the action plugin to perform the reboot then start it |
| 262 |
# here and get the action plugin to continue where this left off. |
| 263 |
|
| 264 |
$module.Result._previous_boot_time = $lastBootTime |
| 265 |
|
| 266 |
$shutdownRegPath = 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\AutoLogonChecked' |
| 267 |
Remove-Item -LiteralPath $shutdownRegPath -Force -ErrorAction SilentlyContinue |
| 268 |
|
| 269 |
$comment = 'Reboot initiated by Ansible' |
| 270 |
$stdout = $null |
| 271 |
$stderr = . { shutdown.exe /r /t 10 /c $comment | Set-Variable stdout } 2>&1 | ForEach-Object ToString |
| 272 |
if ($LASTEXITCODE -eq 1190) { |
| 273 |
# A reboot was already scheduled, abort it and try again |
| 274 |
shutdown.exe /a |
| 275 |
$stdout = $null |
| 276 |
$stderr = . { shutdown.exe /r /t 10 /c $comment | Set-Variable stdout } 2>&1 | ForEach-Object ToString |
| 277 |
} |
| 278 |
|
| 279 |
if ($LASTEXITCODE) { |
| 280 |
$module.Result.rc = $LASTEXITCODE |
| 281 |
$module.Result.stdout = $stdout |
| 282 |
$module.Result.stderr = $stderr |
| 283 |
$module.FailJson("Failed to initiate reboot, see rc, stdout, stderr for more information") |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
$module.ExitJson() |