| 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 |
create_dns_delegation = @{ |
| 11 |
type = 'bool' |
| 12 |
} |
| 13 |
database_path = @{ |
| 14 |
type = 'path' |
| 15 |
} |
| 16 |
dns_domain_name = @{ |
| 17 |
required = $true |
| 18 |
type = 'str' |
| 19 |
} |
| 20 |
domain_mode = @{ |
| 21 |
type = 'str' |
| 22 |
} |
| 23 |
domain_netbios_name = @{ |
| 24 |
type = 'str' |
| 25 |
} |
| 26 |
forest_mode = @{ |
| 27 |
type = 'str' |
| 28 |
} |
| 29 |
install_dns = @{ |
| 30 |
default = $true |
| 31 |
type = 'bool' |
| 32 |
} |
| 33 |
log_path = @{ |
| 34 |
type = 'path' |
| 35 |
} |
| 36 |
reboot = @{ |
| 37 |
default = $false |
| 38 |
type = 'bool' |
| 39 |
} |
| 40 |
reboot_timeout = @{ |
| 41 |
default = 600 |
| 42 |
type = 'int' |
| 43 |
} |
| 44 |
safe_mode_password = @{ |
| 45 |
no_log = $true |
| 46 |
required = $true |
| 47 |
type = 'str' |
| 48 |
} |
| 49 |
sysvol_path = @{ |
| 50 |
type = 'path' |
| 51 |
} |
| 52 |
} |
| 53 |
supports_check_mode = $true |
| 54 |
} |
| 55 |
$module = [Ansible.Basic.AnsibleModule]::Create($args, $spec) |
| 56 |
|
| 57 |
$module.Result.reboot_required = $false |
| 58 |
$module.Result._do_action_reboot = $false # Used by action plugin |
| 59 |
|
| 60 |
$create_dns_delegation = $module.Params.create_dns_delegation |
| 61 |
$database_path = $module.Params.database_path |
| 62 |
$dns_domain_name = $module.Params.dns_domain_name |
| 63 |
$domain_mode = $module.Params.domain_mode |
| 64 |
$domain_netbios_name = $module.Params.domain_netbios_name |
| 65 |
$forest_mode = $module.Params.forest_mode |
| 66 |
$install_dns = $module.Params.install_dns |
| 67 |
$log_path = $module.Params.log_path |
| 68 |
$safe_mode_password = $module.Params.safe_mode_password |
| 69 |
$sysvol_path = $module.Params.sysvol_path |
| 70 |
|
| 71 |
if ([System.Environment]::OSVersion.Version -lt [Version]"6.2") { |
| 72 |
$module.FailJson("microsoft.ad.domain requires Windows Server 2012 or higher") |
| 73 |
} |
| 74 |
|
| 75 |
if ($domain_netbios_name -and $domain_netbios_name.Length -gt 15) { |
| 76 |
$module.FailJson("The parameter 'domain_netbios_name' should not exceed 15 characters in length") |
| 77 |
} |
| 78 |
|
| 79 |
$requiredFeatures = @("AD-Domain-Services", "RSAT-ADDS") |
| 80 |
$features = Get-WindowsFeature -Name $requiredFeatures |
| 81 |
$unavailableFeatures = Compare-Object -ReferenceObject $requiredFeatures -DifferenceObject $features.Name -PassThru |
| 82 |
|
| 83 |
if ($unavailableFeatures) { |
| 84 |
$module.FailJson("The following features required for a domain controller are unavailable: $($unavailableFeatures -join ',')") |
| 85 |
} |
| 86 |
|
| 87 |
$missingFeatures = $features | Where-Object InstallState -NE Installed |
| 88 |
if ($missingFeatures) { |
| 89 |
$res = Install-WindowsFeature -Name $missingFeatures -WhatIf:$module.CheckMode |
| 90 |
$module.Result.changed = $true |
| 91 |
$module.Result.reboot_required = [bool]$res.RestartNeeded |
| 92 |
|
| 93 |
# When in check mode and the prereq was "installed" we need to exit early as |
| 94 |
# the AD cmdlets weren't really installed |
| 95 |
if ($module.CheckMode) { |
| 96 |
$module.ExitJson() |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
# Check that we got a valid domain_mode |
| 101 |
$validDomainModes = [Enum]::GetNames((Get-Command -Name Install-ADDSForest).Parameters.DomainMode.ParameterType) |
| 102 |
if (($null -ne $domain_mode) -and -not ($domain_mode -in $validDomainModes)) { |
| 103 |
$validModes = $validDomainModes -join ", " |
| 104 |
$module.FailJson("The parameter 'domain_mode' does not accept '$domain_mode', please use one of: $validModes") |
| 105 |
} |
| 106 |
|
| 107 |
# Check that we got a valid forest_mode |
| 108 |
$validForestModes = [Enum]::GetNames((Get-Command -Name Install-ADDSForest).Parameters.ForestMode.ParameterType) |
| 109 |
if (($null -ne $forest_mode) -and -not ($forest_mode -in $validForestModes)) { |
| 110 |
$validModes = $validForestModes -join ", " |
| 111 |
$module.FailJson("The parameter 'forest_mode' does not accept '$forest_mode', please use one of: $validModes") |
| 112 |
} |
| 113 |
|
| 114 |
try { |
| 115 |
$forestContext = New-Object -TypeName System.DirectoryServices.ActiveDirectory.DirectoryContext -ArgumentList @( |
| 116 |
'Forest', $dns_domain_name |
| 117 |
) |
| 118 |
$forest = [System.DirectoryServices.ActiveDirectory.Forest]::GetForest($forestContext) |
| 119 |
} |
| 120 |
catch [System.DirectoryServices.ActiveDirectory.ActiveDirectoryObjectNotFoundException] { |
| 121 |
$forest = $null |
| 122 |
} |
| 123 |
catch [System.DirectoryServices.ActiveDirectory.ActiveDirectoryOperationException] { |
| 124 |
$forest = $null |
| 125 |
} |
| 126 |
|
| 127 |
if (-not $forest) { |
| 128 |
$installParams = @{ |
| 129 |
DomainName = $dns_domain_name |
| 130 |
SafeModeAdministratorPassword = (ConvertTo-SecureString $safe_mode_password -AsPlainText -Force) |
| 131 |
Confirm = $false |
| 132 |
SkipPreChecks = $true |
| 133 |
InstallDns = $install_dns |
| 134 |
NoRebootOnCompletion = $true |
| 135 |
WhatIf = $module.CheckMode |
| 136 |
} |
| 137 |
|
| 138 |
if ($database_path) { |
| 139 |
$installParams.DatabasePath = $database_path |
| 140 |
} |
| 141 |
|
| 142 |
if ($sysvol_path) { |
| 143 |
$installParams.SysvolPath = $sysvol_path |
| 144 |
} |
| 145 |
|
| 146 |
if ($log_path) { |
| 147 |
$installParams.LogPath = $log_path |
| 148 |
} |
| 149 |
|
| 150 |
if ($domain_netbios_name) { |
| 151 |
$installParams.DomainNetBiosName = $domain_netbios_name |
| 152 |
} |
| 153 |
|
| 154 |
if ($null -ne $create_dns_delegation) { |
| 155 |
$installParams.CreateDnsDelegation = $create_dns_delegation |
| 156 |
} |
| 157 |
|
| 158 |
if ($domain_mode) { |
| 159 |
$installParams.DomainMode = $domain_mode |
| 160 |
} |
| 161 |
|
| 162 |
if ($forest_mode) { |
| 163 |
$installParams.ForestMode = $forest_mode |
| 164 |
} |
| 165 |
|
| 166 |
$res = $null |
| 167 |
try { |
| 168 |
$res = Install-ADDSForest @installParams |
| 169 |
} |
| 170 |
catch [Microsoft.DirectoryServices.Deployment.DCPromoExecutionException] { |
| 171 |
# ExitCode 15 == 'Role change is in progress or this computer needs to be restarted.' |
| 172 |
# DCPromo exit codes details can be found at |
| 173 |
# https://docs.microsoft.com/en-us/windows-server/identity/ad-ds/deploy/troubleshooting-domain-controller-deployment |
| 174 |
if ($_.Exception.ExitCode -in @(15, 19)) { |
| 175 |
$module.Result.reboot_required = $true |
| 176 |
$module.Result._do_action_reboot = $true |
| 177 |
} |
| 178 |
|
| 179 |
$module.FailJson("Failed to install ADDSForest, DCPromo exited with $($_.Exception.ExitCode): $($_.Exception.Message)", $_) |
| 180 |
} |
| 181 |
finally { |
| 182 |
# The Netlogon service is set to auto start but is not started. This is |
| 183 |
# required for Ansible to connect back to the host and reboot in a |
| 184 |
# later task. Even if this fails Ansible can still connect but only |
| 185 |
# with ansible_winrm_transport=basic so we just display a warning if |
| 186 |
# this fails. |
| 187 |
if (-not $module.CheckMode) { |
| 188 |
try { |
| 189 |
Start-Service -Name Netlogon |
| 190 |
} |
| 191 |
catch { |
| 192 |
$msg = -join @( |
| 193 |
"Failed to start the Netlogon service after promoting the host, " |
| 194 |
"Ansible may be unable to connect until the host is manually rebooting: $($_.Exception.Message)" |
| 195 |
) |
| 196 |
$module.Warn($msg) |
| 197 |
} |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
$module.Result.changed = $true |
| 202 |
|
| 203 |
if ($module.CheckMode) { |
| 204 |
# the return value after -WhatIf does not have RebootRequired populated |
| 205 |
# manually set to True as the domain would have been installed |
| 206 |
$module.Result.reboot_required = $true |
| 207 |
} |
| 208 |
elseif ($res.RebootRequired) { |
| 209 |
$module.Result.reboot_required = $true |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
$module.ExitJson() |