diff --git a/changelogs/fragments/win_domain-exceptions-2.yaml b/changelogs/fragments/win_domain-exceptions-2.yaml new file mode 100644 index 00000000000..6c08b7d1051 --- /dev/null +++ b/changelogs/fragments/win_domain-exceptions-2.yaml @@ -0,0 +1,2 @@ +bugfixes: +- win_domain_controller - Do not fail the play without the user being able to catch dcpromo failing because of a pending reboot within a playbook using ignore_error or retry logic. diff --git a/lib/ansible/modules/windows/win_domain_controller.ps1 b/lib/ansible/modules/windows/win_domain_controller.ps1 index 4fa8c48b6b0..1f195aeec47 100644 --- a/lib/ansible/modules/windows/win_domain_controller.ps1 +++ b/lib/ansible/modules/windows/win_domain_controller.ps1 @@ -211,7 +211,20 @@ Try { if ($site_name) { $install_params.SiteName = $site_name } - $install_result = Install-ADDSDomainController -NoRebootOnCompletion -Force @install_params + try + { + $null = Install-ADDSDomainController -NoRebootOnCompletion -Force @install_params + } 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 -eq 15) { + $result.reboot_required = $true + } else { + Fail-Json -obj $result -message "Failed to install ADDSDomainController with DCPromo: $($_.Exception.Message)" + } + } + # If $_.FullyQualifiedErrorId -eq 'Test.VerifyUserCredentialPermissions.DCPromo.General.25,Microsoft.DirectoryServices.Deployment.PowerShell.Commands.InstallADDSDomainControllerCommand' + # the module failed to resolve the given dns domain name Write-DebugLog "Installation complete, trying to start the Netlogon service" # The Netlogon service is set to auto start but is not started. This is