| 1 |
- name: test out invalid os version |
| 2 |
test_min_os_version: |
| 3 |
register: invalid_os_version |
| 4 |
ignore_errors: yes |
| 5 |
|
| 6 |
- name: assert test out invalid os version |
| 7 |
assert: |
| 8 |
that: |
| 9 |
- invalid_os_version is failed |
| 10 |
- '"This module cannot run on this OS as it requires a minimum version of 20.0, actual was " in invalid_os_version.msg' |
| 11 |
|
| 12 |
- name: test out become requires without become_user set |
| 13 |
test_all_options: |
| 14 |
register: become_system |
| 15 |
|
| 16 |
- name: assert become requires without become_user set |
| 17 |
assert: |
| 18 |
that: |
| 19 |
- become_system is successful |
| 20 |
- become_system.output == "S-1-5-18" |
| 21 |
|
| 22 |
- set_fact: |
| 23 |
become_test_username: ansible_become_test |
| 24 |
gen_pw: "{{ 'password123!' + lookup('password', '/dev/null chars=ascii_letters,digits length=8') }}" |
| 25 |
|
| 26 |
- name: execute tests and ensure that test user is deleted regardless of success/failure |
| 27 |
block: |
| 28 |
- name: create unprivileged user |
| 29 |
win_user: |
| 30 |
name: "{{ become_test_username }}" |
| 31 |
password: "{{ gen_pw }}" |
| 32 |
update_password: always |
| 33 |
groups: Users |
| 34 |
register: become_test_user_result |
| 35 |
|
| 36 |
- name: create tempdir for test user |
| 37 |
win_file: |
| 38 |
path: C:\Windows\TEMP\test-dir |
| 39 |
state: directory |
| 40 |
|
| 41 |
- name: deny delete permissions on new temp dir for test user |
| 42 |
win_acl: |
| 43 |
path: C:\Windows\TEMP\test-dir |
| 44 |
user: '{{ become_test_user_result.sid }}' |
| 45 |
type: '{{ item.type }}' |
| 46 |
rights: '{{ item.rights }}' |
| 47 |
loop: |
| 48 |
- type: allow |
| 49 |
rights: ListDirectory, CreateFiles, CreateDirectories, ReadAttributes, ReadExtendedAttributes, WriteData, WriteAttributes, WriteExtendedAttributes, Synchronize |
| 50 |
- type: deny |
| 51 |
rights: DeleteSubdirectoriesAndFiles, Delete |
| 52 |
|
| 53 |
- name: ensure current user is not the become user |
| 54 |
win_shell: whoami |
| 55 |
register: whoami_out |
| 56 |
|
| 57 |
- name: verify output |
| 58 |
assert: |
| 59 |
that: |
| 60 |
- not whoami_out.stdout_lines[0].endswith(become_test_username) |
| 61 |
|
| 62 |
- name: get become user profile dir so we can clean it up later |
| 63 |
vars: &become_vars |
| 64 |
ansible_become_user: "{{ become_test_username }}" |
| 65 |
ansible_become_password: "{{ gen_pw }}" |
| 66 |
ansible_become_method: runas |
| 67 |
ansible_become: yes |
| 68 |
win_shell: $env:USERPROFILE |
| 69 |
register: profile_dir_out |
| 70 |
|
| 71 |
- name: ensure profile dir contains test username (eg, if become fails silently, prevent deletion of real user profile) |
| 72 |
assert: |
| 73 |
that: |
| 74 |
- become_test_username in profile_dir_out.stdout_lines[0] |
| 75 |
|
| 76 |
- name: test out become requires when become_user set |
| 77 |
test_all_options: |
| 78 |
vars: *become_vars |
| 79 |
register: become_system |
| 80 |
|
| 81 |
- name: assert become requires when become_user set |
| 82 |
assert: |
| 83 |
that: |
| 84 |
- become_system is successful |
| 85 |
- become_system.output == become_test_user_result.sid |
| 86 |
|
| 87 |
- name: run module with tempdir with no delete access |
| 88 |
win_ping: |
| 89 |
register: temp_deletion_warning |
| 90 |
vars: |
| 91 |
<<: *become_vars |
| 92 |
ansible_remote_tmp: C:\Windows\TEMP\test-dir |
| 93 |
|
| 94 |
always: |
| 95 |
- name: ensure test user is deleted |
| 96 |
win_user: |
| 97 |
name: "{{ become_test_username }}" |
| 98 |
state: absent |
| 99 |
|
| 100 |
- name: ensure test user profile is deleted |
| 101 |
# NB: have to work around powershell limitation of long filenames until win_file fixes it |
| 102 |
win_shell: rmdir /S /Q {{ profile_dir_out.stdout_lines[0] }} |
| 103 |
args: |
| 104 |
executable: cmd.exe |
| 105 |
when: become_test_username in profile_dir_out.stdout_lines[0] | default("") |
| 106 |
|
| 107 |
- name: remove test tempdir |
| 108 |
win_file: |
| 109 |
path: C:\Windows\TEMP\test-dir |
| 110 |
state: absent |
| 111 |
|
| 112 |
- name: get PS events containing module args or envvars created since test start |
| 113 |
raw: | |
| 114 |
$dt=[datetime]::FromFileTime({{ test_host_info.current_time }}) |
| 115 |
(Get-WinEvent -LogName Microsoft-Windows-Powershell/Operational | |
| 116 |
? { $_.TimeCreated -ge $dt -and $_.Message -match "fail_module|hyphen-var" }).Count |
| 117 |
register: ps_log_count |
| 118 |
|
| 119 |
- name: assert no PS events contain module args or envvars |
| 120 |
assert: |
| 121 |
that: |
| 122 |
- ps_log_count.stdout | int == 0 |
| 123 |
|
| 124 |
- name: pass through ISO 8601 date strings correctly |
| 125 |
test_datetime_string: |
| 126 |
value: '2040-10-27T14:30:00' |
| 127 |
register: iso8601_date |
| 128 |
failed_when: >- |
| 129 |
iso8601_date.value != '2040-10-27T14:30:00' or |
| 130 |
iso8601_date.value_type != 'System.String' |
| 131 |
|
| 132 |
- name: test builtin modules are imported correctly with ETS definitions |
| 133 |
test_builtin_module_ets: |
| 134 |
register: builtin_module_ets |
| 135 |
|
| 136 |
- name: assert builtin modules are imported correctly with ETS definitions |
| 137 |
assert: |
| 138 |
that: |
| 139 |
- builtin_module_ets is successful |
| 140 |
- builtin_module_ets.count > 0 |
| 141 |
|
| 142 |
- name: pass through ISO 8601 date strings correctly with become |
| 143 |
test_datetime_string: |
| 144 |
value: '2040-10-27T14:30:00' |
| 145 |
become: true |
| 146 |
become_method: runas |
| 147 |
become_user: SYSTEM |
| 148 |
register: iso8601_date |
| 149 |
failed_when: >- |
| 150 |
iso8601_date.value != '2040-10-27T14:30:00' or |
| 151 |
iso8601_date.value_type != 'System.String' |
| 152 |
|
| 153 |
- name: pass through ISO 8601 date strings correctly with async |
| 154 |
test_datetime_string: |
| 155 |
value: '2040-10-27T14:30:00' |
| 156 |
register: iso8601_date |
| 157 |
async: 60 |
| 158 |
poll: 3 |
| 159 |
failed_when: >- |
| 160 |
iso8601_date.value != '2040-10-27T14:30:00' or |
| 161 |
iso8601_date.value_type != 'System.String' |
| 162 |
|
| 163 |
# https://github.com/ansible/ansible/issues/87228 |
| 164 |
- name: Prepend 32-bit PATH for interpreter check |
| 165 |
ansible.windows.win_powershell: |
| 166 |
script: | |
| 167 |
$ErrorActionPreference = 'Stop' |
| 168 |
|
| 169 |
$keyPath = 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment' |
| 170 |
$sysWowPowerShell = 'C:\Windows\SysWow64\WindowsPowerShell\v1.0' |
| 171 |
|
| 172 |
$key = Get-Item -LiteralPath $keyPath |
| 173 |
$rawPath = $key.GetValue('PATH', '', 'DoNotExpandEnvironmentNames') |
| 174 |
$key.Dispose() |
| 175 |
|
| 176 |
[System.Management.Automation.Language.CodeGeneration]::EscapeSingleQuotedStringContent($rawPath) |
| 177 |
|
| 178 |
$keyParams = @{ |
| 179 |
Path = $keyPath |
| 180 |
Name = 'PATH' |
| 181 |
Value = "$sysWowPowerShell;$rawPath" |
| 182 |
PropertyType = 'ExpandString' |
| 183 |
Force = $true |
| 184 |
Confirm = $false |
| 185 |
} |
| 186 |
New-ItemProperty @keyParams | Out-Null |
| 187 |
register: path_info |
| 188 |
|
| 189 |
- name: Set reset PowerShell script to restore PATH to original value |
| 190 |
set_fact: |
| 191 |
reset_path_command: | |
| 192 |
$ErrorActionPreference = 'Stop' |
| 193 |
|
| 194 |
$keyPath = 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment' |
| 195 |
$rawPath = '{{ path_info.output[0] }}' |
| 196 |
|
| 197 |
$keyParams = @{ |
| 198 |
Path = $keyPath |
| 199 |
Name = 'PATH' |
| 200 |
Value = $rawPath |
| 201 |
PropertyType = 'ExpandString' |
| 202 |
Force = $true |
| 203 |
Confirm = $false |
| 204 |
} |
| 205 |
New-ItemProperty @keyParams | Out-Null |
| 206 |
|
| 207 |
- block: |
| 208 |
- name: Reset connection to ensure refreshed env vars |
| 209 |
meta: reset_connection |
| 210 |
|
| 211 |
- name: Test that multiple PATH entries continue to work |
| 212 |
get_info: |
| 213 |
|
| 214 |
always: |
| 215 |
# We use raw as we won't know if setting the PATH broke everything or not. |
| 216 |
- name: Reset PATH back to the original value |
| 217 |
raw: >- |
| 218 |
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe |
| 219 |
-EncodedCommand {{ reset_path_command | b64encode(encoding='utf-16-le')}} |
| 220 |
|
| 221 |
- name: Reset connection to after resetting env vars |
| 222 |
meta: reset_connection |