mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-22 21:00: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) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue