|
1 | 1 | # Copyright 2022-2024 VMware, Inc. |
2 | 2 | # SPDX-License-Identifier: BSD-2-Clause |
3 | 3 | --- |
4 | | -# Retry to get serivce status in Windows guest OS until it's running |
| 4 | +# Retry to get serivce status in Windows guest OS until it reaches the expected state. |
5 | 5 | # Parameters: |
6 | | -# win_service_name: the service name |
| 6 | +# win_service_name: the service name to getting status. |
| 7 | +# win_wait_service_state (optional): the state of the service, e.g., 'Stopped', 'Running'. |
| 8 | +# Default value is 'Running'. |
| 9 | +# win_wait_service_timeout (optional): the timeout in seconds to wait for service state. |
| 10 | +# Default value is 60s. |
7 | 11 | # |
8 | 12 | - name: "Check required parameter" |
9 | | - ansible.builtin.fail: |
10 | | - msg: "win_service_name must be defined before get service status" |
11 | | - when: win_service_name is undefined or not win_service_name |
| 13 | + ansible.builtin.assert: |
| 14 | + that: |
| 15 | + - win_service_name is defined |
| 16 | + - win_service_name | length > 0 |
| 17 | + msg: "Parameter 'win_service_name' must be defined before get service status." |
12 | 18 |
|
13 | 19 | - name: "Check specified service '{{ win_service_name }}' status in Windows" |
14 | | - ansible.windows.win_shell: 'get-service -Name {{ win_service_name }} | foreach {$_.Status}' |
15 | | - delay: 5 |
16 | | - retries: 10 |
| 20 | + ansible.windows.win_shell: 'Get-Service -Name {{ win_service_name }} | foreach {$_.Status}' |
17 | 21 | delegate_to: "{{ vm_guest_ip }}" |
18 | 22 | register: get_service_status |
| 23 | + delay: 5 |
| 24 | + retries: "{{ (win_wait_service_timeout | default(60) / 5) | round | int }}" |
19 | 25 | until: |
20 | 26 | - get_service_status is defined |
21 | 27 | - get_service_status.stdout_lines is defined |
22 | | - - get_service_status.stdout_lines | length != 0 |
23 | | - - get_service_status.stdout_lines[0] == 'Running' |
| 28 | + - get_service_status.stdout_lines | length == 1 |
| 29 | + - get_service_status.stdout_lines[0] == win_wait_service_state | default('Running') |
24 | 30 | ignore_errors: true |
| 31 | + ignore_unreachable: true |
25 | 32 |
|
26 | 33 | - name: "Check service '{{ win_service_name }}' status in Windows" |
27 | 34 | ansible.builtin.assert: |
28 | 35 | that: |
29 | | - - get_service_status is defined |
30 | | - - get_service_status.stdout_lines is defined |
31 | | - - get_service_status.stdout_lines | length != 0 |
32 | | - - get_service_status.stdout_lines[0] == 'Running' |
| 36 | + - get_service_status.failed is defined |
| 37 | + - not get_service_status.failed |
33 | 38 | fail_msg: >- |
34 | | - Windows service '{{ win_service_name }}' status is not running after 50 seconds. |
35 | | - Current service status is '{{ get_service_status.stdout_lines[0] | default("") }}'. |
36 | | -
|
37 | | -- name: "Display the PowerShell command result" |
38 | | - ansible.builtin.debug: var=get_service_status |
39 | | - when: enable_debug is defined and enable_debug |
| 39 | + Windows service '{{ win_service_name }}' status is not {{ win_wait_service_state | default('Running') }} |
| 40 | + after {{ win_wait_service_timeout | default(60) }} seconds. |
| 41 | + Get service status in guest OS '{{ get_service_status.stdout_lines | default("") }}'. |
0 commit comments