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