From 3ad86e383e5288acdbb6f033e2dfa3fb4d590eb6 Mon Sep 17 00:00:00 2001 From: Hans-Joachim Kliemeck Date: Wed, 21 Oct 2015 18:16:49 +0200 Subject: [PATCH] added start mode feature --- .../modules/extras/windows/win_nssm.ps1 | 42 +++++++++++++++++++ .../modules/extras/windows/win_nssm.py | 20 +++++++++ 2 files changed, 62 insertions(+) diff --git a/lib/ansible/modules/extras/windows/win_nssm.ps1 b/lib/ansible/modules/extras/windows/win_nssm.ps1 index 915a3889e1..99a61d12a9 100644 --- a/lib/ansible/modules/extras/windows/win_nssm.ps1 +++ b/lib/ansible/modules/extras/windows/win_nssm.ps1 @@ -33,6 +33,7 @@ $state = Get-Attr $params "state" "present" -validateSet "present", "absent", "s $application = Get-Attr $params "application" $null $appParameters = Get-Attr $params "app_parameters" $null +$startMode = Get-Attr $params "start_mode" "auto" -validateSet "auto", "manual", "disabled" -resultobj $result $stdoutFile = Get-Attr $params "stdout_file" $null $stderrFile = Get-Attr $params "stderr_file" $null @@ -403,6 +404,43 @@ Function Nssm-Update-Dependencies } } +Function Nssm-Update-StartMode +{ + [CmdletBinding()] + param( + [Parameter(Mandatory=$true)] + [string]$name, + [Parameter(Mandatory=$true)] + [string]$mode + ) + + $cmd = "nssm get ""$name"" Start" + $results = invoke-expression $cmd + + if ($LastExitCode -ne 0) + { + Set-Attr $result "nssm_error_cmd" $cmd + Set-Attr $result "nssm_error_log" "$results" + Throw "Error updating start mode for service ""$name""" + } + + $modes=@{"auto" = "SERVICE_AUTO_START"; "manual" = "SERVICE_DEMAND_START"; "disabled" = "SERVICE_DISABLED"} + $mappedMode = $modes.$mode + if ($mappedMode -ne $results) { + $cmd = "nssm set ""$name"" Start $mappedMode" + $results = invoke-expression $cmd + + if ($LastExitCode -ne 0) + { + Set-Attr $result "nssm_error_cmd" $cmd + Set-Attr $result "nssm_error_log" "$results" + Throw "Error updating start mode for service ""$name""" + } + + $result.changed = $true + } +} + Function Nssm-Get-Status { [CmdletBinding()] @@ -548,6 +586,7 @@ Try Nssm-Set-Ouput-Files -name $name -stdout $stdoutFile -stderr $stderrFile Nssm-Update-Dependencies -name $name -dependencies $dependencies Nssm-Update-Credentials -name $name -user $user -password $password + Nssm-Update-StartMode -name $name -mode $startMode } "started" { Nssm-Install -name $name -application $application @@ -555,6 +594,7 @@ Try Nssm-Set-Ouput-Files -name $name -stdout $stdoutFile -stderr $stderrFile Nssm-Update-Dependencies -name $name -dependencies $dependencies Nssm-Update-Credentials -name $name -user $user -password $password + Nssm-Update-StartMode -name $name -mode $startMode Nssm-Start -name $name } "stopped" { @@ -563,6 +603,7 @@ Try Nssm-Set-Ouput-Files -name $name -stdout $stdoutFile -stderr $stderrFile Nssm-Update-Dependencies -name $name -dependencies $dependencies Nssm-Update-Credentials -name $name -user $user -password $password + Nssm-Update-StartMode -name $name -mode $startMode Nssm-Stop -name $name } "restarted" { @@ -571,6 +612,7 @@ Try Nssm-Set-Ouput-Files -name $name -stdout $stdoutFile -stderr $stderrFile Nssm-Update-Dependencies -name $name -dependencies $dependencies Nssm-Update-Credentials -name $name -user $user -password $password + Nssm-Update-StartMode -name $name -mode $startMode Nssm-Restart -name $name } } diff --git a/lib/ansible/modules/extras/windows/win_nssm.py b/lib/ansible/modules/extras/windows/win_nssm.py index 94d4079e78..1f42f21f45 100644 --- a/lib/ansible/modules/extras/windows/win_nssm.py +++ b/lib/ansible/modules/extras/windows/win_nssm.py @@ -86,6 +86,19 @@ options: - Password to be used for service startup required: false default: null + password: + description: + - Password to be used for service startup + required: false + default: null + start_mode: + description: + - If C(auto) is selected, the service will start at bootup. C(manual) means that the service will start only when another service needs it. C(disabled) means that the service will stay off, regardless if it is needed or not. + required: false + choices: + - auto + - manual + - disabled author: - "Adam Keech (@smadam813)" - "George Frank (@georgefrank)" @@ -151,6 +164,13 @@ EXAMPLES = ''' user: foouser password: secret +# Install the foo service but do not start it automatically +- win_nssm: + name: foo + application: C:\windows\\foo.exe + state: present + start_mode: manual + # Remove the foo service - win_nssm: name: foo