| 1 |
- set_fact: |
| 2 |
get_role_script: | |
| 3 |
$Ansible.Changed = $false |
| 4 |
Get-CimInstance -ClassName Win32_ComputerSystem -Property Domain, DomainRole, PartOfDomain | |
| 5 |
Select-Object -Property @{ |
| 6 |
N = 'Domain' |
| 7 |
E = { |
| 8 |
if ($_.PartOfDomain) { |
| 9 |
$_.Domain |
| 10 |
} |
| 11 |
else { |
| 12 |
$null |
| 13 |
} |
| 14 |
} |
| 15 |
}, @{ |
| 16 |
N = 'DomainRole' |
| 17 |
E = { |
| 18 |
switch ($_.DomainRole) { |
| 19 |
0 { "StandaloneWorkstation" } |
| 20 |
1 { "MemberWorkstation" } |
| 21 |
2 { "StandaloneServer" } |
| 22 |
3 { "MemberServer" } |
| 23 |
4 { "BackupDC" } |
| 24 |
5 { "PrimaryDC" } |
| 25 |
} |
| 26 |
} |
| 27 |
}, @{ |
| 28 |
N = 'HostName' |
| 29 |
E = { $env:COMPUTERNAME } |
| 30 |
} |
| 31 |
|
| 32 |
- name: test no change when not a DC |
| 33 |
domain_controller: |
| 34 |
dns_domain_name: '{{ domain_realm }}' |
| 35 |
domain_admin_user: '{{ domain_user_upn }}' |
| 36 |
domain_admin_password: '{{ domain_password }}' |
| 37 |
local_admin_password: '{{ domain_password }}' |
| 38 |
state: member_server |
| 39 |
reboot: true |
| 40 |
register: not_dc_no_change |
| 41 |
|
| 42 |
- name: assert test no change when not a DC |
| 43 |
assert: |
| 44 |
that: |
| 45 |
- not not_dc_no_change is changed |
| 46 |
- not_dc_no_change.reboot_required == False |
| 47 |
|
| 48 |
- name: promote to DC - check mode |
| 49 |
domain_controller: |
| 50 |
dns_domain_name: '{{ domain_realm }}' |
| 51 |
domain_admin_user: '{{ domain_user_upn }}' |
| 52 |
domain_admin_password: '{{ domain_password }}' |
| 53 |
safe_mode_password: '{{ domain_password }}' |
| 54 |
state: domain_controller |
| 55 |
reboot: true |
| 56 |
register: to_dc_check |
| 57 |
check_mode: true |
| 58 |
|
| 59 |
- name: get result of promote to DC - check mode |
| 60 |
ansible.windows.win_powershell: |
| 61 |
script: '{{ get_role_script }}' |
| 62 |
register: to_dc_check_actual |
| 63 |
|
| 64 |
- name: assert promote to DC - check mode |
| 65 |
assert: |
| 66 |
that: |
| 67 |
- to_dc_check is changed |
| 68 |
- to_dc_check_actual.output[0]["Domain"] == None |
| 69 |
- to_dc_check_actual.output[0]["DomainRole"] == "StandaloneServer" |
| 70 |
|
| 71 |
- name: change hostname to have a pending change before promotion |
| 72 |
ansible.windows.win_hostname: |
| 73 |
name: FOO |
| 74 |
|
| 75 |
- name: promote to DC with pending reboot |
| 76 |
domain_controller: |
| 77 |
dns_domain_name: '{{ domain_realm }}' |
| 78 |
domain_admin_user: '{{ domain_user_upn }}' |
| 79 |
domain_admin_password: '{{ domain_password }}' |
| 80 |
safe_mode_password: '{{ domain_password }}' |
| 81 |
state: domain_controller |
| 82 |
reboot: true |
| 83 |
register: to_dc |
| 84 |
|
| 85 |
- name: get result of promote to DC with pending reboot |
| 86 |
ansible.windows.win_powershell: |
| 87 |
script: '{{ get_role_script }}' |
| 88 |
register: to_dc_actual |
| 89 |
|
| 90 |
- name: assert promote to DC with pending reboot |
| 91 |
assert: |
| 92 |
that: |
| 93 |
- to_dc is changed |
| 94 |
- to_dc.reboot_required == False |
| 95 |
- to_dc_actual.output[0]["Domain"] == domain_realm |
| 96 |
- to_dc_actual.output[0]["DomainRole"] == "BackupDC" |
| 97 |
- to_dc_actual.output[0]["HostName"] == "FOO" |
| 98 |
|
| 99 |
- name: promote to DC - idempotent |
| 100 |
domain_controller: |
| 101 |
dns_domain_name: '{{ domain_realm }}' |
| 102 |
domain_admin_user: '{{ domain_user_upn }}' |
| 103 |
domain_admin_password: '{{ domain_password }}' |
| 104 |
safe_mode_password: '{{ domain_password }}' |
| 105 |
state: domain_controller |
| 106 |
reboot: true |
| 107 |
register: to_dc_again |
| 108 |
|
| 109 |
- name: assert promote to DC - idempotent |
| 110 |
assert: |
| 111 |
that: |
| 112 |
- not to_dc_again is changed |
| 113 |
- to_dc_again.reboot_required == False |
| 114 |
|
| 115 |
# The following operations will run with the domain admin account now that the |
| 116 |
# host is joined to the domain |
| 117 |
- name: change connection user and password to domain account |
| 118 |
set_fact: |
| 119 |
ansible_user: '{{ domain_user_upn }}' |
| 120 |
ansible_password: '{{ domain_password }}' |
| 121 |
|
| 122 |
- name: fail to change domain of DC |
| 123 |
domain_controller: |
| 124 |
dns_domain_name: bogus.local |
| 125 |
domain_admin_user: '{{ domain_user_upn }}' |
| 126 |
domain_admin_password: '{{ domain_password }}' |
| 127 |
safe_mode_password: '{{ domain_password }}' |
| 128 |
state: domain_controller |
| 129 |
reboot: true |
| 130 |
register: change_domain_fail |
| 131 |
failed_when: |
| 132 |
- change_domain_fail.msg != "The host FOO is a domain controller for the domain " ~ domain_realm ~ "; changing DC domains is not implemented" |
| 133 |
|
| 134 |
- name: fail with invalid username format |
| 135 |
domain_controller: |
| 136 |
dns_domain_name: '{{ domain_realm }}' |
| 137 |
domain_admin_user: '{{ domain_username }}' # Must be a UPN or Netbios Name |
| 138 |
domain_admin_password: '{{ domain_password }}' |
| 139 |
safe_mode_password: '{{ domain_password }}' |
| 140 |
state: domain_controller |
| 141 |
reboot: true |
| 142 |
register: invalid_user_fail |
| 143 |
failed_when: |
| 144 |
- invalid_user_fail.msg != "domain_admin_user must be in domain\\user or [email protected] format" |
| 145 |
|
| 146 |
- name: set DC as member server - check mode |
| 147 |
domain_controller: |
| 148 |
dns_domain_name: '{{ domain_realm }}' |
| 149 |
domain_admin_user: '{{ domain_user_upn }}' |
| 150 |
domain_admin_password: '{{ domain_password }}' |
| 151 |
local_admin_password: '{{ domain_password }}' |
| 152 |
state: member_server |
| 153 |
reboot: true |
| 154 |
register: member_server_check |
| 155 |
check_mode: true |
| 156 |
|
| 157 |
- name: get result of set DC as member server - check mode |
| 158 |
ansible.windows.win_powershell: |
| 159 |
script: '{{ get_role_script }}' |
| 160 |
register: member_server_check_actual |
| 161 |
|
| 162 |
- name: assert set DC as member server - check mode |
| 163 |
assert: |
| 164 |
that: |
| 165 |
- member_server_check is changed |
| 166 |
- member_server_check.reboot_required == False |
| 167 |
- member_server_check_actual.output[0]["Domain"] == domain_realm |
| 168 |
- member_server_check_actual.output[0]["DomainRole"] == "BackupDC" |
| 169 |
|
| 170 |
- name: set DC as member server |
| 171 |
domain_controller: |
| 172 |
dns_domain_name: '{{ domain_realm }}' |
| 173 |
domain_admin_user: '{{ domain_user_upn }}' |
| 174 |
domain_admin_password: '{{ domain_password }}' |
| 175 |
local_admin_password: '{{ domain_password }}' |
| 176 |
state: member_server |
| 177 |
reboot: true |
| 178 |
register: member_server |
| 179 |
|
| 180 |
- name: get result of set DC as member server |
| 181 |
ansible.windows.win_powershell: |
| 182 |
script: '{{ get_role_script }}' |
| 183 |
register: member_server_actual |
| 184 |
|
| 185 |
- name: assert set DC as member server |
| 186 |
assert: |
| 187 |
that: |
| 188 |
- member_server is changed |
| 189 |
- member_server.reboot_required == False |
| 190 |
- member_server_actual.output[0]["Domain"] == domain_realm |
| 191 |
- member_server_actual.output[0]["DomainRole"] == "MemberServer" |
| 192 |
|
| 193 |
- name: set DC as member server - idempotent |
| 194 |
domain_controller: |
| 195 |
dns_domain_name: '{{ domain_realm }}' |
| 196 |
domain_admin_user: '{{ domain_user_upn }}' |
| 197 |
domain_admin_password: '{{ domain_password }}' |
| 198 |
local_admin_password: '{{ domain_password }}' |
| 199 |
state: member_server |
| 200 |
reboot: true |
| 201 |
register: member_server_again |
| 202 |
|
| 203 |
- name: assert set DC as member server - idempotent |
| 204 |
assert: |
| 205 |
that: |
| 206 |
- not member_server_again is changed |
| 207 |
- member_server_again.reboot_required == False |
| 208 |
|
| 209 |
# Promote it once more to a DC to test an edge case where Ansible is unable to |
| 210 |
# connect back until a reboot has occurred. |
| 211 |
- name: promote to DC again |
| 212 |
domain_controller: |
| 213 |
dns_domain_name: '{{ domain_realm }}' |
| 214 |
domain_admin_user: '{{ domain_user_upn }}' |
| 215 |
domain_admin_password: '{{ domain_password }}' |
| 216 |
safe_mode_password: '{{ domain_password }}' |
| 217 |
state: domain_controller |
| 218 |
reboot: true |
| 219 |
register: to_dc_manual_reboot |
| 220 |
|
| 221 |
- name: get result of promote to DC with manual reboot |
| 222 |
ansible.windows.win_powershell: |
| 223 |
script: '{{ get_role_script }}' |
| 224 |
register: to_dc_manual_reboot_actual |
| 225 |
|
| 226 |
- name: assert promote to DC with manual reboot |
| 227 |
assert: |
| 228 |
that: |
| 229 |
- to_dc_manual_reboot is changed |
| 230 |
- to_dc_manual_reboot.reboot_required == False |
| 231 |
- to_dc_manual_reboot_actual.output[0]["Domain"] == domain_realm |
| 232 |
- to_dc_manual_reboot_actual.output[0]["DomainRole"] == "BackupDC" |