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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
|
#!powershell
# (c) 2017, Red Hat, Inc.
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
# WANT_JSON
# POWERSHELL_COMMON
Set-StrictMode -Version 2
$ErrorActionPreference = "Stop"
$ConfirmPreference = "None"
$log_path = $null
Function Write-DebugLog {
Param(
[string]$msg
)
$DebugPreference = "Continue"
$ErrorActionPreference = "Continue"
$date_str = Get-Date -Format u
$msg = "$date_str $msg"
Write-Debug $msg
if($log_path) {
Add-Content $log_path $msg
}
}
$required_features = @("AD-Domain-Services","RSAT-ADDS")
Function Get-MissingFeatures {
Write-DebugLog "Checking for missing Windows features..."
$features = @(Get-WindowsFeature $required_features)
If($features.Count -ne $required_features.Count) {
Throw "One or more Windows features required for a domain controller are unavailable"
}
$missing_features = @($features | Where-Object InstallState -ne Installed)
return ,$missing_features # no, the comma's not a typo- allows us to return an empty array
}
Function Ensure-FeatureInstallation {
# ensure RSAT-ADDS and AD-Domain-Services features are installed
Write-DebugLog "Ensuring required Windows features are installed..."
$feature_result = Install-WindowsFeature $required_features
If(-not $feature_result.Success) {
Exit-Json -message ("Error installing AD-Domain-Services and RSAT-ADDS features: {0}" -f ($feature_result | Out-String))
}
}
# return the domain we're a DC for, or null if not a DC
Function Get-DomainControllerDomain {
Write-DebugLog "Checking for domain controller role and domain name"
$sys_cim = Get-WmiObject Win32_ComputerSystem
$is_dc = $sys_cim.DomainRole -in (4,5) # backup/primary DC
# this will be our workgroup or joined-domain if we're not a DC
$domain = $sys_cim.Domain
Switch($is_dc) {
$true { return $domain }
Default { return $null }
}
}
Function Create-Credential {
Param(
[string] $cred_user,
[string] $cred_password
)
$cred = New-Object System.Management.Automation.PSCredential($cred_user, $($cred_password | ConvertTo-SecureString -AsPlainText -Force))
Return $cred
}
Function Get-OperationMasterRoles {
$assigned_roles = @((Get-ADDomainController -Server localhost).OperationMasterRoles)
Return ,$assigned_roles # no, the comma's not a typo- allows us to return an empty array
}
$result = @{
changed = $false
reboot_required = $false
}
$param = Parse-Args -arguments $args -supports_check_mode $true
$dns_domain_name = Get-AnsibleParam $param "dns_domain_name"
$safe_mode_password= Get-AnsibleParam $param "safe_mode_password"
$domain_admin_user = Get-AnsibleParam $param "domain_admin_user" -failifempty $result
$domain_admin_password= Get-AnsibleParam $param "domain_admin_password" -failifempty $result
$local_admin_password= Get-AnsibleParam $param "local_admin_password"
$state = Get-AnsibleParam $param "state" -validateset ("domain_controller", "member_server") -failifempty $result
$log_path = Get-AnsibleParam $param "log_path"
$_ansible_check_mode = Get-AnsibleParam $param "_ansible_check_mode" -default $false
$global:log_path = $log_path
Try {
# ensure target OS support; < 2012 doesn't have cmdlet support for DC promotion
If(-not (Get-Command Install-WindowsFeature -ErrorAction SilentlyContinue)) {
Fail-Json -message "win_domain_controller requires at least Windows Server 2012"
}
# validate args
If($state -eq "domain_controller") {
If(-not $dns_domain_name) {
Fail-Json -message "dns_domain_name is required when desired state is 'domain_controller'"
}
If(-not $safe_mode_password) {
Fail-Json -message "safe_mode_password is required when desired state is 'domain_controller'"
}
# ensure that domain admin user is in UPN or down-level domain format (prevent hang from https://support.microsoft.com/en-us/kb/2737935)
If(-not $domain_admin_user.Contains("\") -and -not $domain_admin_user.Contains("@")) {
Fail-Json -message "domain_admin_user must be in domain\user or user@domain.com format"
}
}
Else { # member_server
If(-not $local_admin_password) {
Fail-Json -message "local_admin_password is required when desired state is 'member_server'"
}
}
# short-circuit "member server" check, since we don't need feature checks for this...
$current_dc_domain = Get-DomainControllerDomain
If($state -eq "member_server" -and -not $current_dc_domain) {
Exit-Json $result
}
# all other operations will require the AD-DS and RSAT-ADDS features...
$missing_features = Get-MissingFeatures
If($missing_features.Count -gt 0) {
Write-DebugLog ("Missing Windows features ({0}), need to install" -f ($missing_features -join ", "))
$result.changed = $true # we need to install features
If($_ansible_check_mode) {
# bail out here- we can't proceed without knowing the features are installed
Write-DebugLog "check-mode, exiting early"
Exit-Json $result
}
Ensure-FeatureInstallation | Out-Null
}
$domain_admin_cred = Create-Credential -cred_user $domain_admin_user -cred_password $domain_admin_password
switch($state) {
domain_controller {
If(-not $safe_mode_password) {
Fail-Json -message "safe_mode_password is required for state=domain_controller"
}
If($current_dc_domain) {
# FUTURE: implement managed Remove/Add to change domains?
If($current_dc_domain -ne $dns_domain_name) {
Fail-Json "$(hostname) is a domain controller for domain $current_dc_domain; changing DC domains is not implemented"
}
}
# need to promote to DC
If(-not $current_dc_domain) {
Write-DebugLog "Not currently a domain controller; needs promotion"
$result.changed = $true
If($_ansible_check_mode) {
Write-DebugLog "check-mode, exiting early"
Fail-Json -message $result
}
$result.reboot_required = $true
$safe_mode_secure = $safe_mode_password | ConvertTo-SecureString -AsPlainText -Force
Write-DebugLog "Installing domain controller..."
$install_result = Install-ADDSDomainController -NoRebootOnCompletion -DomainName $dns_domain_name -Credential $domain_admin_cred -SafeModeAdministratorPassword $safe_mode_secure -Force
Write-DebugLog "Installation completed, needs reboot..."
}
}
member_server {
If(-not $local_admin_password) {
Fail-Json -message "local_admin_password is required for state=domain_controller"
}
# at this point we already know we're a DC and shouldn't be...
Write-DebugLog "Need to uninstall domain controller..."
$result.changed = $true
Write-DebugLog "Checking for operation master roles assigned to this DC..."
$assigned_roles = Get-OperationMasterRoles
# FUTURE: figure out a sane way to hand off roles automatically (designated recipient server, randomly look one up?)
If($assigned_roles.Count -gt 0) {
Fail-Json -message ("This domain controller has operation master role(s) ({0}) assigned; they must be moved to other DCs before demotion (see Move-ADDirectoryServerOperationMasterRole)" -f ($assigned_roles -join ", "))
}
If($_ansible_check_mode) {
Write-DebugLog "check-mode, exiting early"
Exit-Json $result
}
$result.reboot_required = $true
$local_admin_secure = $local_admin_password | ConvertTo-SecureString -AsPlainText -Force
Write-DebugLog "Uninstalling domain controller..."
$uninstall_result = Uninstall-ADDSDomainController -NoRebootOnCompletion -LocalAdministratorPassword $local_admin_secure -Credential $domain_admin_cred
Write-DebugLog "Uninstallation complete, needs reboot..."
}
default { throw ("invalid state {0}" -f $state) }
}
Exit-Json $result
}
Catch {
$excep = $_
Write-DebugLog "Exception: $($excep | out-string)"
Throw
}
|