mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-22 12:50:22 -07:00
win_service: added support for paused services (#27216)
* win_service: added support for paused services * change pausable service for local computers * more fixes for older hosts * sigh * skip pause tests for Server 2008 as it relies on the service
This commit is contained in:
parent
ade593da52
commit
7a7a0cae94
5 changed files with 975 additions and 838 deletions
|
@ -34,7 +34,7 @@ $name = Get-AnsibleParam -obj $params -name 'name' -type 'str' -failifempty $tru
|
|||
$password = Get-AnsibleParam -obj $params -name 'password' -type 'str'
|
||||
$path = Get-AnsibleParam -obj $params -name 'path' -type 'path'
|
||||
$start_mode = Get-AnsibleParam -obj $params -name 'start_mode' -type 'str' -validateset 'auto','manual','disabled','delayed'
|
||||
$state = Get-AnsibleParam -obj $params -name 'state' -type 'str' -validateset 'started','stopped','restarted','absent'
|
||||
$state = Get-AnsibleParam -obj $params -name 'state' -type 'str' -validateset 'started','stopped','restarted','absent','paused'
|
||||
$username = Get-AnsibleParam -obj $params -name 'username' -type 'str'
|
||||
|
||||
$result = @{
|
||||
|
@ -92,6 +92,7 @@ Function Get-ServiceInfo($name) {
|
|||
$result.desktop_interact = (ConvertTo-Bool $wmi_svc.DesktopInteract)
|
||||
$result.dependencies = $existing_dependencies
|
||||
$result.depended_by = $existing_depended_by
|
||||
$result.can_pause_and_continue = $svc.CanPauseAndContinue
|
||||
}
|
||||
|
||||
Function Get-WmiErrorMessage($return_value) {
|
||||
|
@ -274,10 +275,18 @@ Function Set-ServiceDependencies($wmi_svc, $dependency_action, $dependencies) {
|
|||
|
||||
Function Set-ServiceState($svc, $wmi_svc, $state) {
|
||||
if ($state -eq "started" -and $result.state -ne "running") {
|
||||
try {
|
||||
Start-Service -Name $svc.Name -WhatIf:$check_mode
|
||||
} catch {
|
||||
Fail-Json $result $_.Exception.Message
|
||||
if ($result.state -eq "paused") {
|
||||
try {
|
||||
Resume-Service -Name $svc.Name -WhatIf:$check_mode
|
||||
} catch {
|
||||
Fail-Json $result "failed to start service from paused state $($svc.Name): $($_.Exception.Message)"
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
Start-Service -Name $svc.Name -WhatIf:$check_mode
|
||||
} catch {
|
||||
Fail-Json $result $_.Exception.Message
|
||||
}
|
||||
}
|
||||
|
||||
$result.changed = $true
|
||||
|
@ -303,6 +312,20 @@ Function Set-ServiceState($svc, $wmi_svc, $state) {
|
|||
$result.changed = $true
|
||||
}
|
||||
|
||||
if ($state -eq "paused" -and $result.state -ne "paused") {
|
||||
# check that we can actually pause the service
|
||||
if ($result.can_pause_and_continue -eq $false) {
|
||||
Fail-Json $result "failed to pause service $($svc.Name): The service does not support pausing"
|
||||
}
|
||||
|
||||
try {
|
||||
Suspend-Service -Name $svc.Name -WhatIf:$check_mode
|
||||
} catch {
|
||||
Fail-Json $result "failed to pause service $($svc.Name): $($_.Exception.Message)"
|
||||
}
|
||||
$result.changed = $true
|
||||
}
|
||||
|
||||
if ($state -eq "absent") {
|
||||
try {
|
||||
Stop-Service -Name $svc.Name -Force:$force_dependent_services -WhatIf:$check_mode
|
||||
|
@ -324,7 +347,7 @@ Function Set-ServiceConfiguration($svc) {
|
|||
$wmi_svc = Get-WmiObject Win32_Service | Where-Object { $_.Name -eq $svc.Name }
|
||||
Get-ServiceInfo -name $svc.Name
|
||||
if ($desktop_interact -eq $true -and (-not ($result.username -eq 'LocalSystem' -or $username -eq 'LocalSystem'))) {
|
||||
Fail-Json $result "Can only set desktop_interact to true when service is run with or 'username' equals 'LocalSystem'"
|
||||
Fail-Json $result "Can only set desktop_interact to true when service is run with/or 'username' equals 'LocalSystem'"
|
||||
}
|
||||
|
||||
if ($start_mode -ne $null) {
|
||||
|
|
|
@ -104,15 +104,20 @@ options:
|
|||
- delayed
|
||||
state:
|
||||
description:
|
||||
- C(started)/C(stopped)/C(absent) are idempotent actions that will not run
|
||||
- C(started)/C(stopped)/C(absent)/C(pause) are idempotent actions that will not run
|
||||
commands unless necessary.
|
||||
- C(restarted) will always bounce the service.
|
||||
- C(absent) added in Ansible 2.3
|
||||
- C(pause) was added in Ansible 2.4
|
||||
- Only services that support the paused state can be paused, you can
|
||||
check the return value C(can_pause_and_continue).
|
||||
- You can only pause a service that is already started.
|
||||
choices:
|
||||
- started
|
||||
- stopped
|
||||
- restarted
|
||||
- absent
|
||||
- paused
|
||||
username:
|
||||
description:
|
||||
- The username to set the service to start as.
|
||||
|
@ -135,6 +140,11 @@ EXAMPLES = r'''
|
|||
start_mode: auto
|
||||
state: started
|
||||
|
||||
- name: pause a service
|
||||
win_service:
|
||||
name: Netlogon
|
||||
state: paused
|
||||
|
||||
# a new service will also default to the following values:
|
||||
# - username: LocalSystem
|
||||
# - state: stopped
|
||||
|
@ -253,6 +263,11 @@ path:
|
|||
returned: success and service exists
|
||||
type: string
|
||||
sample: C:\Windows\system32\svchost.exe -k LocalServiceNoNetwork
|
||||
can_pause_and_continue:
|
||||
description: Whether the service can be paused and unpaused.
|
||||
returned: success and service exists
|
||||
type: bool
|
||||
sample: True
|
||||
description:
|
||||
description: The description of the service.
|
||||
returned: success and service exists
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue