Debian Codegraph @

ansible_collections/community/windows/tests/integration/targets/win_nssm/tasks/tests.yml

raw
1 ---
2 - name: get register cmd that will get service info
3 set_fact:
4 test_service_cmd: |
5 $res = @{}
6 $srvobj = Get-WmiObject Win32_Service -Filter "Name=""$service""" | Select Name,DisplayName,Description,PathName,StartMode,StartName,State
7 if ($srvobj) {
8 $srvobj | Get-Member -MemberType *Property | % { $res.($_.name) = $srvobj.($_.name) }
9
10 $startName = $res.StartName
11 $candidates = @(if ($startName -eq "LocalSystem") {
12 "NT AUTHORITY\SYSTEM"
13 }
14 elseif ($startName.Contains('\')) {
15 $nameSplit = $startName.Split('\', 2)
16
17 if ($nameSplit[0] -eq '.') {
18 ,@($env:COMPUTERNAME, $nameSplit[1])
19 $nameSplit[1]
20 } else {
21 ,$nameSplit
22 }
23 }
24 else {
25 $startName
26 })
27
28 $sid = for ($i = 0; $i -lt $candidates.Length; $i++) {
29 $candidate = $candidates[$i]
30 $ntAccount = New-Object -TypeName System.Security.Principal.NTAccount -ArgumentList $candidate
31 try {
32 $ntAccount.Translate([System.Security.Principal.SecurityIdentifier])
33 break
34 }
35 catch [System.Security.Principal.IdentityNotMappedException] {
36 if ($i -eq ($candidates.Length - 1)) {
37 throw
38 }
39 continue
40 }
41 }
42
43 $res.StartName = $sid.Translate([System.Security.Principal.NTAccount]).Value
44
45 $res.Exists = $true
46 $res.Dependencies = @(Get-WmiObject -Query "Associators of {Win32_Service.Name=""$service""} Where AssocClass=Win32_DependentService" | select -ExpandProperty Name)
47 $res.Parameters = @{}
48 $srvkey = "HKLM:\SYSTEM\CurrentControlSet\Services\$service\Parameters"
49 Get-Item "$srvkey" | Select-Object -ExpandProperty property | % { $res.Parameters.$_ = (Get-ItemProperty -Path "$srvkey" -Name $_).$_}
50 } else {
51 $res.Exists = $false
52 }
53 ConvertTo-Json -InputObject $res -Compress
54
55 - name: install service (check mode)
56 win_nssm:
57 name: '{{ test_service_name }}'
58 application: C:\Windows\System32\cmd.exe
59 state: present
60 register: install_service_check
61 check_mode: yes
62
63 - name: get result of install service (check mode)
64 ansible.windows.win_shell: '$service = ''{{ test_service_name }}''; {{ test_service_cmd }}'
65 register: install_service_check_actual
66
67 - name: assert results of install service (check mode)
68 assert:
69 that:
70 - install_service_check.changed == true
71 - (install_service_check_actual.stdout|from_json).Exists == false
72
73 - name: install service
74 win_nssm:
75 name: '{{ test_service_name }}'
76 application: C:\Windows\System32\cmd.exe
77 state: present
78 register: install_service
79
80 - name: get result of install service
81 ansible.windows.win_shell: '$service = ''{{ test_service_name }}''; {{ test_service_cmd }}'
82 register: install_service_actual
83
84 - name: assert results of install service
85 assert:
86 that:
87 - install_service.changed == true
88 - (install_service_actual.stdout|from_json).Exists == true
89 - (install_service_actual.stdout|from_json).State == 'Stopped'
90 - (install_service_actual.stdout|from_json).StartMode == 'Auto'
91 - (install_service_actual.stdout|from_json).Parameters.Application == "C:\Windows\System32\cmd.exe"
92 - (install_service_actual.stdout|from_json).Parameters.AppDirectory == "C:\Windows\System32"
93
94 - name: test install service (idempotent)
95 win_nssm:
96 name: '{{ test_service_name }}'
97 application: C:\Windows\System32\cmd.exe
98 state: present
99 register: install_service_again
100
101 - name: get result of install service (idempotent)
102 ansible.windows.win_shell: '$service = ''{{ test_service_name }}''; {{ test_service_cmd }}'
103 register: install_service_again_actual
104
105 - name: assert results of install service (idempotent)
106 assert:
107 that:
108 - install_service_again.changed == false
109 - (install_service_again_actual.stdout|from_json).Exists == true
110 - (install_service_again_actual.stdout|from_json).State == 'Stopped'
111 - (install_service_again_actual.stdout|from_json).StartMode == 'Auto'
112 - (install_service_again_actual.stdout|from_json).Parameters.Application == "C:\Windows\System32\cmd.exe"
113 - (install_service_again_actual.stdout|from_json).Parameters.AppDirectory == "C:\Windows\System32"
114
115 - name: install and start service
116 win_nssm:
117 name: '{{ test_service_name }}'
118 application: C:\Windows\System32\cmd.exe
119 state: started
120 register: install_start_service
121
122 - name: get result of install and start service
123 ansible.windows.win_shell: '$service = ''{{ test_service_name }}''; {{ test_service_cmd }}'
124 register: install_start_service_actual
125
126 - name: assert results of install and start service
127 assert:
128 that:
129 - install_start_service.changed == true
130 - (install_start_service_actual.stdout|from_json).Exists == true
131 - (install_start_service_actual.stdout|from_json).State == 'Running'
132 - (install_start_service_actual.stdout|from_json).StartMode == 'Auto'
133 - (install_start_service_actual.stdout|from_json).Parameters.Application == "C:\Windows\System32\cmd.exe"
134 - (install_start_service_actual.stdout|from_json).Parameters.AppDirectory == "C:\Windows\System32"
135
136 - name: install and start service with more parameters (check mode)
137 win_nssm:
138 name: '{{ test_service_name }}'
139 display_name: Ansible testing
140 description: win_nssm test service
141 application: C:\Windows\System32\cmd.exe
142 start_mode: manual
143 working_directory: '{{ test_win_nssm_path }}'
144 dependencies: 'tcpip,dnscache'
145 username: '{{ test_win_nssm_username }}'
146 password: '{{ test_win_nssm_password }}'
147 stdout_file: '{{ test_win_nssm_path }}\log.txt'
148 stderr_file: '{{ test_win_nssm_path }}\error.txt'
149 state: started
150 register: install_service_complex_check
151 check_mode: yes
152
153 - name: get result of install and start service with more parameters (check mode)
154 ansible.windows.win_shell: '$service = ''{{ test_service_name }}''; {{ test_service_cmd }}'
155 register: install_service_complex_check_actual
156
157 - name: assert results of install and start service with more parameters (check mode)
158 assert:
159 that:
160 - install_service_complex_check.changed == true
161 - (install_service_complex_check_actual.stdout|from_json).Exists == true
162 - (install_service_complex_check_actual.stdout|from_json).DisplayName == '{{ test_service_name }}'
163 - (install_service_complex_check_actual.stdout|from_json).Description is none
164 - (install_service_complex_check_actual.stdout|from_json).StartMode != 'Manual'
165 - (install_service_complex_check_actual.stdout|from_json).StartName != test_win_nssm_normalised_username
166 - (install_service_complex_check_actual.stdout|from_json).Parameters.Application == "C:\Windows\System32\cmd.exe"
167 - (install_service_complex_check_actual.stdout|from_json).Parameters.AppDirectory == "C:\Windows\System32"
168 - '"AppStdout" not in (install_service_complex_check_actual.stdout|from_json).Parameters'
169 - '"AppStderr" not in (install_service_complex_check_actual.stdout|from_json).Parameters'
170 - (install_service_complex_check_actual.stdout|from_json).Dependencies|length == 0
171
172 - name: install and start service with more parameters
173 win_nssm:
174 name: '{{ test_service_name }}'
175 display_name: Ansible testing
176 description: win_nssm test service
177 application: C:\Windows\System32\cmd.exe
178 start_mode: manual
179 working_directory: '{{ test_win_nssm_path }}'
180 dependencies: 'tcpip,dnscache'
181 username: '{{ test_win_nssm_username }}'
182 password: '{{ test_win_nssm_password }}'
183 stdout_file: '{{ test_win_nssm_path }}\log.txt'
184 stderr_file: '{{ test_win_nssm_path }}\error.txt'
185 state: started
186 register: install_service_complex
187
188 - name: get result of install and start service with more parameters
189 ansible.windows.win_shell: '$service = ''{{ test_service_name }}''; {{ test_service_cmd }}'
190 register: install_service_complex_actual
191
192 - name: assert results of install and start service with more parameters
193 assert:
194 that:
195 - install_service_complex.changed == true
196 - (install_service_complex_actual.stdout|from_json).Exists == true
197 - (install_service_complex_actual.stdout|from_json).DisplayName == 'Ansible testing'
198 - (install_service_complex_actual.stdout|from_json).Description == 'win_nssm test service'
199 - (install_service_complex_actual.stdout|from_json).State == 'Running'
200 - (install_service_complex_actual.stdout|from_json).StartMode == 'Manual'
201 - (install_service_complex_actual.stdout|from_json).StartName == test_win_nssm_normalised_username
202 - (install_service_complex_actual.stdout|from_json).Parameters.Application == "C:\Windows\System32\cmd.exe"
203 - (install_service_complex_actual.stdout|from_json).Parameters.AppDirectory == test_win_nssm_path
204 - (install_service_complex_actual.stdout|from_json).Parameters.AppStdout == test_win_nssm_path + '\\log.txt'
205 - (install_service_complex_actual.stdout|from_json).Parameters.AppStderr == test_win_nssm_path + '\\error.txt'
206 - (install_service_complex_actual.stdout|from_json).Dependencies|length == 2
207 - '"Tcpip" in (install_service_complex_actual.stdout|from_json).Dependencies'
208 - '"Dnscache" in (install_service_complex_actual.stdout|from_json).Dependencies'
209
210 - name: install and start service with more parameters (idempotent)
211 win_nssm:
212 name: '{{ test_service_name }}'
213 display_name: Ansible testing
214 description: win_nssm test service
215 application: C:\Windows\System32\cmd.exe
216 start_mode: manual
217 working_directory: '{{ test_win_nssm_path }}'
218 # Dependencies order should not trigger a change
219 dependencies: 'dnscache,tcpip'
220 username: '{{ test_win_nssm_username }}'
221 password: '{{ test_win_nssm_password }}'
222 stdout_file: '{{ test_win_nssm_path }}\log.txt'
223 stderr_file: '{{ test_win_nssm_path }}\error.txt'
224 state: started
225 register: install_service_complex_again
226
227 - name: get result of install and start service with more parameters (idempotent)
228 ansible.windows.win_shell: '$service = ''{{ test_service_name }}''; {{ test_service_cmd }}'
229 register: install_service_complex_again_actual
230
231 - name: assert results of install and start service with more parameters (idempotent)
232 assert:
233 that:
234 - install_service_complex_again.changed == false
235 - (install_service_complex_again_actual.stdout|from_json).Exists == true
236 - (install_service_complex_again_actual.stdout|from_json).DisplayName == 'Ansible testing'
237 - (install_service_complex_again_actual.stdout|from_json).Description == 'win_nssm test service'
238 - (install_service_complex_again_actual.stdout|from_json).State == 'Running'
239 - (install_service_complex_again_actual.stdout|from_json).StartMode == 'Manual'
240 - (install_service_complex_again_actual.stdout|from_json).StartName == test_win_nssm_normalised_username
241 - (install_service_complex_again_actual.stdout|from_json).Parameters.Application == "C:\Windows\System32\cmd.exe"
242 - (install_service_complex_again_actual.stdout|from_json).Parameters.AppDirectory == test_win_nssm_path
243 - (install_service_complex_again_actual.stdout|from_json).Parameters.AppStdout == test_win_nssm_path + '\\log.txt'
244 - (install_service_complex_again_actual.stdout|from_json).Parameters.AppStderr == test_win_nssm_path + '\\error.txt'
245 - (install_service_complex_again_actual.stdout|from_json).Dependencies|length == 2
246 - '"Tcpip" in (install_service_complex_again_actual.stdout|from_json).Dependencies'
247 - '"Dnscache" in (install_service_complex_again_actual.stdout|from_json).Dependencies'
248
249 - name: install service with string form parameters
250 win_nssm:
251 name: '{{ test_service_name }}'
252 application: C:\Windows\System32\cmd.exe
253 arguments: '-v -Dtest.str=value "C:\with space\\"'
254 state: present
255 register: str_params
256
257 - name: get result of install service with string form parameters
258 ansible.windows.win_shell: '$service = ''{{ test_service_name }}''; {{ test_service_cmd }}'
259 register: str_params_actual
260
261 - name: assert results of install service with string form parameters
262 assert:
263 that:
264 - str_params.changed == true
265 - (str_params_actual.stdout|from_json).Exists == true
266 - (str_params_actual.stdout|from_json).Parameters.Application == "C:\Windows\System32\cmd.exe"
267 # Expected value: -v -Dtest.str=value "C:\with space\\" (backslashes doubled for jinja)
268 - (str_params_actual.stdout|from_json).Parameters.AppParameters == '-v -Dtest.str=value "C:\\with space\\\\"'
269
270 - name: install service with string form parameters (idempotent)
271 win_nssm:
272 name: '{{ test_service_name }}'
273 application: C:\Windows\System32\cmd.exe
274 arguments: '-v -Dtest.str=value "C:\with space\\"'
275 state: present
276 register: str_params_again
277
278 - name: get result of install service with string form parameters (idempotent)
279 ansible.windows.win_shell: '$service = ''{{ test_service_name }}''; {{ test_service_cmd }}'
280 register: str_params_again_actual
281
282 - name: assert results of install service with string form parameters (idempotent)
283 assert:
284 that:
285 - str_params_again.changed == false
286 - (str_params_again_actual.stdout|from_json).Exists == true
287 - (str_params_again_actual.stdout|from_json).Parameters.Application == "C:\Windows\System32\cmd.exe"
288 # Expected value: -v -Dtest.str=value "C:\with space\\" (backslashes doubled for jinja)
289 - (str_params_again_actual.stdout|from_json).Parameters.AppParameters == '-v -Dtest.str=value "C:\\with space\\\\"'
290
291 - name: install service with extra environment vars (check mode)
292 win_nssm:
293 name: '{{ test_service_name }}'
294 application: C:\Windows\System32\cmd.exe
295 start_mode: manual
296 state: present
297 app_environment:
298 foo: bar
299 baz: 2
300 register: install_service_appenv_check
301 check_mode: yes
302
303 - name: get result of install service with extra environment vars (check mode)
304 ansible.windows.win_shell: nssm.exe get '{{ test_service_name }}' AppEnvironmentExtra
305 register: install_service_appenv_check_actual
306
307 ## note: this could fail (in theory) when the service is not yet
308 ## installed (diff mode), but because of side effects of earlier
309 ## tests this will actually not fail in practice, however, it is
310 ## not a real issue in any case
311 failed_when: false
312
313 - name: assert results of install service with extra environment vars (check mode)
314 assert:
315 that:
316 - install_service_appenv_check.changed == true
317 - install_service_appenv_check_actual.stdout == '\r\n' or install_service_appenv_check_actual.stdout == ''
318
319 - name: install service with extra environment vars
320 win_nssm:
321 name: '{{ test_service_name }}'
322 application: C:\Windows\System32\cmd.exe
323 start_mode: manual
324 state: present
325 app_environment:
326 foo: bar
327 baz: 2
328 register: install_service_appenv
329
330 - name: get result of install service with extra environment vars
331 ansible.windows.win_shell: nssm.exe get '{{ test_service_name }}' AppEnvironmentExtra
332 register: install_service_appenv_actual
333
334 - name: assert results of install service with extra environment vars
335 assert:
336 that:
337 - install_service_appenv.changed == true
338 - (install_service_appenv_actual.stdout_lines|length) == 3
339 - (install_service_appenv_actual.stdout_lines[0]) == 'baz=2'
340 - (install_service_appenv_actual.stdout_lines[2]) == 'foo=bar'
341
342 - name: install service with extra environment vars (idempotent)
343 win_nssm:
344 name: '{{ test_service_name }}'
345 application: C:\Windows\System32\cmd.exe
346 start_mode: manual
347 state: present
348 app_environment:
349 foo: bar
350 baz: 2
351 register: install_service_appenv_idem
352
353 - name: get result of install service with extra environment vars (idempotent)
354 ansible.windows.win_shell: nssm.exe get '{{ test_service_name }}' AppEnvironmentExtra
355 register: install_service_appenv_idem_actual
356
357 - name: assert results of install service with extra environment vars (idempotent)
358 assert:
359 that:
360 - install_service_appenv_idem.changed == false
361 - (install_service_appenv_idem_actual.stdout_lines|length) == 3
362 - (install_service_appenv_idem_actual.stdout_lines[0]) == 'baz=2'
363 - (install_service_appenv_idem_actual.stdout_lines[2]) == 'foo=bar'
364
365 - name: install service dont change app_env if not explicitly requested
366 win_nssm:
367 name: '{{ test_service_name }}'
368 application: C:\Windows\System32\cmd.exe
369 start_mode: manual
370 state: present
371 register: install_service_appenv_implicit
372
373 - name: get result of install service dont change app_env if not explicitly requested
374 ansible.windows.win_shell: nssm.exe get '{{ test_service_name }}' AppEnvironmentExtra
375 register: install_service_appenv_implicit_actual
376
377 - name: assert results of install service dont change app_env if not explicitly requested
378 assert:
379 that:
380 - install_service_appenv_implicit.changed == false
381 - (install_service_appenv_implicit_actual.stdout_lines|length) == 3
382 - (install_service_appenv_implicit_actual.stdout_lines[0]) == 'baz=2'
383 - (install_service_appenv_implicit_actual.stdout_lines[2]) == 'foo=bar'
384
385 - name: install service resetting env vars
386 win_nssm:
387 name: '{{ test_service_name }}'
388 application: C:\Windows\System32\cmd.exe
389 start_mode: manual
390 state: present
391 app_environment: {}
392 register: install_service_reset_appenv
393
394 - name: get result of install service resetting env vars
395 ansible.windows.win_shell: nssm.exe get '{{ test_service_name }}' AppEnvironmentExtra
396 register: install_service_reset_appenv_actual
397
398 - name: assert results of install service resetting env vars
399 assert:
400 that:
401 - install_service_reset_appenv.changed == true
402 - install_service_reset_appenv_actual.stdout == '\r\n'
403
404 - name: install service with list of parameters
405 win_nssm:
406 name: '{{ test_service_name }}'
407 application: C:\Windows\System32\cmd.exe
408 arguments:
409 - -foo=bar
410 - -day
411 # Test non-string value
412 - 14
413 # Test if dot is not interpreted as separator (see #44079)
414 - -file.out
415 # Test if spaces are escaped
416 - C:\with space\output.bat
417 - -str
418 # Test if quotes and backslashes are escaped
419 - test"quotes\
420 register: list_params
421
422 - name: get result of install service with list of parameters
423 ansible.windows.win_shell: '$service = ''{{ test_service_name }}''; {{ test_service_cmd }}'
424 register: list_params_actual
425
426 - name: assert results of install service with list of parameters
427 assert:
428 that:
429 - list_params.changed == true
430 - (list_params_actual.stdout|from_json).Exists == true
431 - (list_params_actual.stdout|from_json).Parameters.Application == "C:\Windows\System32\cmd.exe"
432 # Expected value: -foo=bar -day 14 -file.out "C:\with space\output.bat" -str "test\"quotes\\" (backslashes doubled for jinja)
433 - (list_params_actual.stdout|from_json).Parameters.AppParameters == '-foo=bar -day 14 -file.out "C:\\with space\\output.bat" -str "test\\"quotes\\\\"'
434
435 - name: install service with list of parameters (idempotent)
436 win_nssm:
437 name: '{{ test_service_name }}'
438 application: C:\Windows\System32\cmd.exe
439 arguments:
440 - -foo=bar
441 - -day
442 - 14
443 - -file.out
444 - C:\with space\output.bat
445 - -str
446 - test"quotes\
447 register: list_params_again
448
449 - name: get result of install service with list of parameters (idempotent)
450 ansible.windows.win_shell: '$service = ''{{ test_service_name }}''; {{ test_service_cmd }}'
451 register: list_params_again_actual
452
453 - name: assert results of install service with list of parameters (idempotent)
454 assert:
455 that:
456 - list_params_again.changed == false
457 - (list_params_again_actual.stdout|from_json).Exists == true
458 - (list_params_again_actual.stdout|from_json).Parameters.Application == "C:\Windows\System32\cmd.exe"
459 # Expected value: -foo=bar -day 14 -file.out "C:\with space\output.bat" -str "test\"quotes\\" (backslashes doubled for jinja)
460 - (list_params_again_actual.stdout|from_json).Parameters.AppParameters == '-foo=bar -day 14 -file.out "C:\\with space\\output.bat" -str "test\\"quotes\\\\"'
461
462 - name: set service username to SYSTEM
463 win_nssm:
464 name: '{{ test_service_name }}'
465 username: LocalSystem
466 application: C:\Windows\System32\cmd.exe
467 register: service_system
468
469 - name: get service account for SYSTEM
470 ansible.windows.win_service_info:
471 name: '{{ test_service_name }}'
472 register: service_system_actual
473
474 - name: assert set service username to SYSTEM
475 assert:
476 that:
477 - service_system is changed
478 - service_system_actual.services[0].username == 'NT AUTHORITY\\SYSTEM'
479
480 - name: set service username to SYSTEM (idempotent)
481 win_nssm:
482 name: '{{ test_service_name }}'
483 username: SYSTEM
484 application: C:\Windows\System32\cmd.exe
485 register: service_system_again
486
487 - name: assert set service username to SYSTEM (idempotent)
488 assert:
489 that:
490 - not service_system_again is changed
491
492 - name: set service username to NETWORK SERVICE
493 win_nssm:
494 name: '{{ test_service_name }}'
495 username: NETWORK SERVICE
496 application: C:\Windows\System32\cmd.exe
497 register: service_network
498
499 - name: get service account for NETWORK SERVICE
500 ansible.windows.win_service_info:
501 name: '{{ test_service_name }}'
502 register: service_network_actual
503
504 - name: assert set service username to NETWORK SERVICE
505 assert:
506 that:
507 - service_network is changed
508 - service_network_actual.services[0].username == 'NT Authority\\NetworkService'
509
510 - name: set service username to NETWORK SERVICE (idempotent)
511 win_nssm:
512 name: '{{ test_service_name }}'
513 username: NT AUTHORITY\NETWORK SERVICE
514 application: C:\Windows\System32\cmd.exe
515 register: service_network_again
516
517 - name: assert set service username to NETWORK SERVICE (idempotent)
518 assert:
519 that:
520 - not service_network_again is changed
521
522 - name: remove service (check mode)
523 win_nssm:
524 name: '{{ test_service_name }}'
525 state: absent
526 register: remove_service_check
527 check_mode: yes
528
529 - name: get result of remove service (check mode)
530 ansible.windows.win_shell: '$service = ''{{ test_service_name }}''; {{ test_service_cmd }}'
531 register: remove_service_check_actual
532
533 - name: assert results of remove service (check mode)
534 assert:
535 that:
536 - remove_service_check.changed == true
537 - (remove_service_check_actual.stdout|from_json).Exists == true
538
539 - name: remove service
540 win_nssm:
541 name: '{{ test_service_name }}'
542 state: absent
543 register: remove_service
544
545 - name: get result of remove service
546 ansible.windows.win_shell: '$service = ''{{ test_service_name }}''; {{ test_service_cmd }}'
547 register: remove_service_actual
548
549 - name: assert results of remove service
550 assert:
551 that:
552 - remove_service.changed == true
553 - (remove_service_actual.stdout|from_json).Exists == false
554
555 - name: remove service (idempotent)
556 win_nssm:
557 name: '{{ test_service_name }}'
558 state: absent
559 register: remove_service_again
560
561 - name: get result of remove service (idempotent)
562 ansible.windows.win_shell: '$service = ''{{ test_service_name }}''; {{ test_service_cmd }}'
563 register: remove_service_again_actual
564
565 - name: assert results of remove service (idempotent)
566 assert:
567 that:
568 - remove_service_again.changed == false
569 - (remove_service_again_actual.stdout|from_json).Exists == false