| 1 |
#!powershell |
| 2 |
|
| 3 |
# Copyright (c) 2024 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 |
create_dns_delegation = @{ |
| 11 |
type = 'bool' |
| 12 |
} |
| 13 |
database_path = @{ |
| 14 |
type = 'path' |
| 15 |
} |
| 16 |
dns_domain_name = @{ |
| 17 |
type = 'str' |
| 18 |
} |
| 19 |
domain_admin_password = @{ |
| 20 |
type = 'str' |
| 21 |
required = $true |
| 22 |
no_log = $true |
| 23 |
} |
| 24 |
domain_admin_user = @{ |
| 25 |
type = 'str' |
| 26 |
required = $true |
| 27 |
} |
| 28 |
domain_mode = @{ |
| 29 |
type = 'str' |
| 30 |
} |
| 31 |
domain_type = @{ |
| 32 |
choices = 'child', 'tree' |
| 33 |
default = 'child' |
| 34 |
type = 'str' |
| 35 |
} |
| 36 |
install_dns = @{ |
| 37 |
type = 'bool' |
| 38 |
} |
| 39 |
log_path = @{ |
| 40 |
type = 'path' |
| 41 |
} |
| 42 |
parent_domain_name = @{ |
| 43 |
type = 'str' |
| 44 |
} |
| 45 |
reboot = @{ |
| 46 |
default = $false |
| 47 |
type = 'bool' |
| 48 |
} |
| 49 |
reboot_timeout = @{ |
| 50 |
default = 600 |
| 51 |
type = 'int' |
| 52 |
} |
| 53 |
replication_source_dc = @{ |
| 54 |
type = 'str' |
| 55 |
} |
| 56 |
safe_mode_password = @{ |
| 57 |
type = 'str' |
| 58 |
required = $true |
| 59 |
no_log = $true |
| 60 |
} |
| 61 |
site_name = @{ |
| 62 |
type = 'str' |
| 63 |
} |
| 64 |
sysvol_path = @{ |
| 65 |
type = 'path' |
| 66 |
} |
| 67 |
} |
| 68 |
required_if = @( |
| 69 |
, @('domain_type', 'tree', @('parent_domain_name')) |
| 70 |
) |
| 71 |
required_together = @( |
| 72 |
, @("domain_admin_user", "domain_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 |
$createDnsDelegation = $module.Params.create_dns_delegation |
| 82 |
$databasePath = $module.Params.database_path |
| 83 |
$dnsDomainName = $module.Params.dns_domain_name |
| 84 |
$domainMode = $module.Params.domain_mode |
| 85 |
$domainType = $module.Params.domain_type |
| 86 |
$installDns = $module.Params.install_dns |
| 87 |
$logPath = $module.Params.log_path |
| 88 |
$parentDomainName = $module.Params.parent_domain_name |
| 89 |
$replicationSourceDC = $module.Params.replication_source_dc |
| 90 |
$safeModePassword = $module.Params.safe_mode_password |
| 91 |
$siteName = $module.Params.site_name |
| 92 |
$sysvolPath = $module.Params.sysvol_path |
| 93 |
|
| 94 |
$domainCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList @( |
| 95 |
$module.Params.domain_admin_user, |
| 96 |
(ConvertTo-SecureString -AsPlainText -Force -String $module.Params.domain_admin_password) |
| 97 |
) |
| 98 |
|
| 99 |
if ($domainType -eq 'child' -and $parentDomainName) { |
| 100 |
$module.FailJson("parent_domain_name must not be set when domain_type=child") |
| 101 |
} |
| 102 |
|
| 103 |
$requiredFeatures = @("AD-Domain-Services", "RSAT-ADDS") |
| 104 |
$features = Get-WindowsFeature -Name $requiredFeatures |
| 105 |
$unavailableFeatures = Compare-Object -ReferenceObject $requiredFeatures -DifferenceObject $features.Name -PassThru |
| 106 |
|
| 107 |
if ($unavailableFeatures) { |
| 108 |
$module.FailJson("The following features required for a domain child are unavailable: $($unavailableFeatures -join ',')") |
| 109 |
} |
| 110 |
|
| 111 |
$missingFeatures = $features | Where-Object InstallState -NE Installed |
| 112 |
if ($missingFeatures) { |
| 113 |
$res = Install-WindowsFeature -Name $missingFeatures -WhatIf:$module.CheckMode |
| 114 |
$module.Result.changed = $true |
| 115 |
$module.Result.reboot_required = [bool]$res.RestartNeeded |
| 116 |
|
| 117 |
# When in check mode and the prereq was "installed" we need to exit early as |
| 118 |
# the AD cmdlets weren't really installed |
| 119 |
if ($module.CheckMode) { |
| 120 |
$module.ExitJson() |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
# Check that we got a valid domain_mode |
| 125 |
$validDomainModes = [Enum]::GetNames((Get-Command -Name Install-ADDSDomain).Parameters.DomainMode.ParameterType) |
| 126 |
if (($null -ne $domainMode) -and -not ($domainMode -in $validDomainModes)) { |
| 127 |
$validModes = $validDomainModes -join ", " |
| 128 |
$module.FailJson("The parameter 'domain_mode' does not accept '$domainMode', please use one of: $validModes") |
| 129 |
} |
| 130 |
|
| 131 |
$systemRole = Get-CimInstance -ClassName Win32_ComputerSystem -Property Domain, DomainRole |
| 132 |
if ($systemRole.DomainRole -in @(4, 5)) { |
| 133 |
if ($systemRole.Domain -ne $dnsDomainName) { |
| 134 |
$module.FailJson("Host is already a domain controller in another domain $($systemRole.Domain)") |
| 135 |
} |
| 136 |
$module.ExitJson() |
| 137 |
} |
| 138 |
|
| 139 |
$installParams = @{ |
| 140 |
Confirm = $false |
| 141 |
Credential = $domainCredential |
| 142 |
Force = $true |
| 143 |
NoRebootOnCompletion = $true |
| 144 |
SafeModeAdministratorPassword = (ConvertTo-SecureString $safeModePassword -AsPlainText -Force) |
| 145 |
SkipPreChecks = $true |
| 146 |
WhatIf = $module.CheckMode |
| 147 |
} |
| 148 |
|
| 149 |
if ($domainType -eq 'child') { |
| 150 |
$newDomainName, $parentDomainName = $dnsDomainName.Split([char[]]".", 2) |
| 151 |
$installParams.DomainType = 'ChildDomain' |
| 152 |
$installParams.NewDomainName = $newDomainName |
| 153 |
$installParams.ParentDomainName = $parentDomainName |
| 154 |
} |
| 155 |
else { |
| 156 |
$installParams.DomainType = 'TreeDomain' |
| 157 |
$installParams.NewDomainName = $dnsDomainName |
| 158 |
$installParams.ParentDomainName = $parentDomainName |
| 159 |
} |
| 160 |
|
| 161 |
if ($null -ne $createDnsDelegation) { |
| 162 |
$installParams.CreateDnsDelegation = $createDnsDelegation |
| 163 |
} |
| 164 |
if ($databasePath) { |
| 165 |
$installParams.DatabasePath = $databasePath |
| 166 |
} |
| 167 |
if ($domainMode) { |
| 168 |
$installParams.DomainMode = $domainMode |
| 169 |
} |
| 170 |
if ($null -ne $installDns) { |
| 171 |
$installParams.InstallDns = $installDns |
| 172 |
} |
| 173 |
if ($logPath) { |
| 174 |
$installParams.LogPath = $logPath |
| 175 |
} |
| 176 |
if ($replicationSourceDC) { |
| 177 |
$installParams.ReplicationSourceDC = $replicationSourceDC |
| 178 |
} |
| 179 |
if ($siteName) { |
| 180 |
$installParams.SiteName = $siteName |
| 181 |
} |
| 182 |
if ($sysvolPath) { |
| 183 |
$installParams.SysvolPath = $sysvolPath |
| 184 |
} |
| 185 |
|
| 186 |
try { |
| 187 |
$null = Install-ADDSDomain @installParams |
| 188 |
} |
| 189 |
catch [Microsoft.DirectoryServices.Deployment.DCPromoExecutionException] { |
| 190 |
# ExitCode 15 == 'Role change is in progress or this computer needs to be restarted.' |
| 191 |
# DCPromo exit codes details can be found at |
| 192 |
# https://docs.microsoft.com/en-us/windows-server/identity/ad-ds/deploy/troubleshooting-domain-controller-deployment |
| 193 |
if ($_.Exception.ExitCode -in @(15, 19)) { |
| 194 |
$module.Result.reboot_required = $true |
| 195 |
$module.Result._do_action_reboot = $true |
| 196 |
} |
| 197 |
|
| 198 |
$module.FailJson("Failed to install ADDSDomain, DCPromo exited with $($_.Exception.ExitCode)", $_) |
| 199 |
} |
| 200 |
finally { |
| 201 |
# The Netlogon service is set to auto start but is not started. This is |
| 202 |
# required for Ansible to connect back to the host and reboot in a |
| 203 |
# later task. Even if this fails Ansible can still connect but only |
| 204 |
# with ansible_winrm_transport=basic so we just display a warning if |
| 205 |
# this fails. |
| 206 |
if (-not $module.CheckMode) { |
| 207 |
try { |
| 208 |
Start-Service -Name Netlogon |
| 209 |
} |
| 210 |
catch { |
| 211 |
$msg = -join @( |
| 212 |
"Failed to start the Netlogon service after promoting the host, " |
| 213 |
"Ansible may be unable to connect until the host is manually rebooted: $($_.Exception.Message)" |
| 214 |
) |
| 215 |
$module.Warn($msg) |
| 216 |
} |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
$module.Result.changed = $true |
| 221 |
$module.Result.reboot_required = $true |
| 222 |
|
| 223 |
if ($module.Result.reboot_required -and $module.Params.reboot -and -not $module.CheckMode) { |
| 224 |
# Promoting or depromoting puts the server in a very funky state and it may |
| 225 |
# not be possible for Ansible to connect back without a reboot is done. If |
| 226 |
# the user requested the action plugin to perform the reboot then start it |
| 227 |
# here and get the action plugin to continue where this left off. |
| 228 |
|
| 229 |
$lastBootTime = (Get-CimInstance -ClassName Win32_OperatingSystem -Property LastBootUpTime).LastBootUpTime.ToFileTime() |
| 230 |
$module.Result._previous_boot_time = $lastBootTime |
| 231 |
|
| 232 |
$shutdownRegPath = 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\AutoLogonChecked' |
| 233 |
Remove-Item -LiteralPath $shutdownRegPath -Force -ErrorAction SilentlyContinue |
| 234 |
|
| 235 |
$comment = 'Reboot initiated by Ansible' |
| 236 |
$stdout = $null |
| 237 |
$stderr = . { shutdown.exe /r /t 10 /c $comment | Set-Variable stdout } 2>&1 | ForEach-Object ToString |
| 238 |
if ($LASTEXITCODE -eq 1190) { |
| 239 |
# A reboot was already scheduled, abort it and try again |
| 240 |
shutdown.exe /a |
| 241 |
$stdout = $null |
| 242 |
$stderr = . { shutdown.exe /r /t 10 /c $comment | Set-Variable stdout } 2>&1 | ForEach-Object ToString |
| 243 |
} |
| 244 |
|
| 245 |
if ($LASTEXITCODE) { |
| 246 |
$module.Result.rc = $LASTEXITCODE |
| 247 |
$module.Result.stdout = $stdout |
| 248 |
$module.Result.stderr = $stderr |
| 249 |
$module.FailJson("Failed to initiate reboot, see rc, stdout, stderr for more information") |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
$module.ExitJson() |