2017-03-02 01:49:15 +00:00
#!powershell
2018-07-17 21:29:05 +00:00
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
# Requires -Module Ansible.ModuleUtils.Legacy
2017-03-02 01:49:15 +00:00
Set-StrictMode -Version 2
$ErrorActionPreference = " Stop "
# FUTURE: consider action wrapper to manage reboots and credential changes
Function Ensure-Prereqs {
$gwf = Get-WindowsFeature AD-Domain -Services
If ( $gwf . InstallState -ne " Installed " ) {
$result . changed = $true
If ( $check_mode ) {
Exit-Json $result
}
$awf = Add-WindowsFeature AD-Domain -Services
# FUTURE: check if reboot necessary
}
}
2018-07-17 21:29:05 +00:00
$params = Parse-Args $args -supports_check_mode $true
$check_mode = Get-AnsibleParam -obj $params -name " _ansible_check_mode " -default $false
$dns_domain_name = Get-AnsibleParam -obj $params -name " dns_domain_name " -failifempty $true
$domain_netbios_name = Get-AnsibleParam -obj $params -name " domain_netbios_name "
$safe_mode_admin_password = Get-AnsibleParam -obj $params -name " safe_mode_password " -failifempty $true
$database_path = Get-AnsibleParam -obj $params -name " database_path " -type " path "
$sysvol_path = Get-AnsibleParam -obj $params -name " sysvol_path " -type " path "
2017-03-02 01:49:15 +00:00
$forest = $null
# FUTURE: support down to Server 2012?
If ( [ System.Environment ] :: OSVersion . Version -lt [ Version ] " 6.3.9600.0 " ) {
Fail-Json -message " win_domain requires Windows Server 2012R2 or higher "
}
$result = @ { changed = $false ; reboot_required = $false }
# FUTURE: any sane way to do the detection under check-mode *without* installing the feature?
Ensure-Prereqs
Try {
2017-03-29 18:34:45 +00:00
$forest = Get-ADForest $dns_domain_name -ErrorAction SilentlyContinue
2017-03-02 01:49:15 +00:00
}
Catch { }
If ( -not $forest ) {
$result . changed = $true
If ( -not $check_mode ) {
$sm_cred = ConvertTo-SecureString $safe_mode_admin_password -AsPlainText -Force
$install_forest_args = @ {
2017-03-29 18:34:45 +00:00
DomainName = $dns_domain_name ;
2017-03-02 01:49:15 +00:00
SafeModeAdministratorPassword = $sm_cred ;
Confirm = $false ;
SkipPreChecks = $true ;
2018-06-27 01:29:45 +00:00
InstallDns = $true ;
2017-03-02 01:49:15 +00:00
NoRebootOnCompletion = $true ;
}
2018-01-03 20:32:01 +00:00
if ( $database_path ) {
$install_forest_args . DatabasePath = $database_path
}
if ( $sysvol_path ) {
$install_forest_args . SysvolPath = $sysvol_path
}
2018-04-24 05:35:38 +00:00
if ( $domain_netbios_name ) {
$install_forest_args . DomainNetBiosName = $domain_netbios_name
}
2017-03-02 01:49:15 +00:00
$iaf = Install-ADDSForest @install_forest_args
$result . reboot_required = $iaf . RebootRequired
2018-08-10 06:17:45 +00:00
# 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.
try {
Start-Service -Name Netlogon
} catch {
Add-Warning -obj $result -message " Failed to start the Netlogon service after promoting the host, Ansible may be unable to connect until the host is manually rebooting: $( $_ . Exception . Message ) "
}
2017-03-02 01:49:15 +00:00
}
}
Exit-Json $result