From 862855252b5f8ff9cdd6ef8a4b3e6afee6a3c326 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Subileau?= Date: Sat, 15 Sep 2018 14:07:22 +0200 Subject: [PATCH] win_nssm: restore support of string as dict form for app_parameters and remove support of literal YAML dict --- lib/ansible/modules/windows/win_nssm.ps1 | 40 +++++++++++++------ lib/ansible/modules/windows/win_nssm.py | 24 +++-------- .../targets/win_nssm/tasks/tests.yml | 22 +++------- 3 files changed, 39 insertions(+), 47 deletions(-) diff --git a/lib/ansible/modules/windows/win_nssm.ps1 b/lib/ansible/modules/windows/win_nssm.ps1 index afb12393ed1..0a9cb420a96 100644 --- a/lib/ansible/modules/windows/win_nssm.ps1 +++ b/lib/ansible/modules/windows/win_nssm.ps1 @@ -36,8 +36,8 @@ if (($appParameters -ne $null) -and ($appParametersFree -ne $null)) { Fail-Json $result "Use either app_parameters or app_parameteres_free_form, but not both" } -if (($appParameters -ne $null) -and ($appParameters -isnot [System.Collections.Hashtable])) { - Fail-Json -obj $result -message "The app_parameters parameter must be a dict" +if (($appParameters -ne $null) -and ($appParameters -isnot [string])) { + Fail-Json -obj $result -message "The app_parameters parameter must be a string representing a dictionary." } Function Nssm-Invoke @@ -171,6 +171,20 @@ Function Nssm-Install } } +Function ParseAppParameters() +{ + [CmdletBinding()] + param( + [Parameter(Mandatory=$true)] + [AllowEmptyString()] + [string]$appParameters + ) + + $escapedAppParameters = $appParameters.TrimStart("@").TrimStart("{").TrimEnd("}").Replace("; ","`n").Replace("\","\\") + + return ConvertFrom-StringData -StringData $escapedAppParameters +} + Function Nssm-Update-AppParameters { [CmdletBinding()] @@ -197,22 +211,22 @@ Function Nssm-Update-AppParameters if ($null -ne $appParameters) { + $appParametersHash = ParseAppParameters -appParameters $appParameters $appParamsArray = @() - $appParameters.GetEnumerator() | - % { - $key = $($_.Name) - $val = $($_.Value) + $appParametersHash.GetEnumerator() | foreach { + $key = $($_.Name) + $val = $($_.Value) - $appParamKeys += $key - $appParamVals += $val + $appParamKeys += $key + $appParamVals += $val - if ($key -ne "_") { - $appParamsArray += $key - } - - $appParamsArray += $val + if ($key -ne "_") { + $appParamsArray += $key } + $appParamsArray += $val + } + $result.nssm_app_parameters_keys = $appParamKeys $result.nssm_app_parameters_vals = $appParamVals diff --git a/lib/ansible/modules/windows/win_nssm.py b/lib/ansible/modules/windows/win_nssm.py index f896d1e3f95..9b16aff784c 100644 --- a/lib/ansible/modules/windows/win_nssm.py +++ b/lib/ansible/modules/windows/win_nssm.py @@ -49,7 +49,7 @@ options: - Path to receive error output. app_parameters: description: - - Parameters to be passed to the application when it starts. + - A string representing a dictionary of parameters to be passed to the application when it starts. - Use either this or C(app_parameters_free_form), not both. app_parameters_free_form: version_added: "2.3.0" @@ -87,37 +87,25 @@ EXAMPLES = r''' application: C:\windows\foo.exe # Install and start the foo service with a key-value pair argument -# This will yield the following command: C:\windows\foo.exe bar "true" +# This will yield the following command: C:\windows\foo.exe -bar true - win_nssm: name: foo application: C:\windows\foo.exe - app_parameters: - bar: 'true' - -# Install and start the foo service with a key-value pair argument, where the argument needs to start with a dash -# This will yield the following command: C:\windows\\foo.exe -bar "true" -- win_nssm: - name: foo - application: C:\windows\foo.exe - app_parameters: - "-bar": 'true' + app_parameters: -bar=true # Install and start the foo service with a single parameter # This will yield the following command: C:\windows\\foo.exe bar - win_nssm: name: foo application: C:\windows\foo.exe - app_parameters: - _: bar + app_parameters: _=bar # Install and start the foo service with a mix of single params, and key value pairs -# This will yield the following command: C:\windows\\foo.exe bar -file output.bat +# This will yield the following command: C:\windows\\foo.exe bar -file output.bat -foo false - win_nssm: name: foo application: C:\windows\foo.exe - app_parameters: - _: bar - "-file": "output.bat" + app_parameters: _=bar; -file=output.bat; -foo=false # Use the single line parameters option to specify an arbitrary string of parameters # for the service executable diff --git a/test/integration/targets/win_nssm/tasks/tests.yml b/test/integration/targets/win_nssm/tasks/tests.yml index 83364e7121f..e0e384febf1 100644 --- a/test/integration/targets/win_nssm/tasks/tests.yml +++ b/test/integration/targets/win_nssm/tasks/tests.yml @@ -187,12 +187,7 @@ win_nssm: name: '{{ test_service_name }}' application: C:\Windows\System32\cmd.exe - app_parameters: - foo: true - '-file.out': 'output.bat' - '-path': 'C:\with space\' - '-str': 'test"quotes' - _: bar + app_parameters: foo=true; -file.out=output.bat; -path=C:\with space\; -str=test"quotes; _=bar register: mixed_params - name: get result of install service with dict parameters @@ -205,19 +200,14 @@ - mixed_params.changed == true - (mixed_params_actual.stdout|from_json).Exists == true - (mixed_params_actual.stdout|from_json).Parameters.Application == "C:\Windows\System32\cmd.exe" - # Expected value: bar -str "test\"quotes" -file.out output.bat foo True -path "C:\with space\\" (backslashes doubled for jinja) - - (mixed_params_actual.stdout|from_json).Parameters.AppParameters == 'bar -str "test\\"quotes" -file.out output.bat foo True -path "C:\\with space\\\\"' + # Expected value: bar -file.out output.bat -str "test\"quotes" foo true -path "C:\with space\\" (backslashes doubled for jinja) + - (mixed_params_actual.stdout|from_json).Parameters.AppParameters == 'bar -file.out output.bat -str "test\\"quotes" foo true -path "C:\\with space\\\\"' - name: install service with dict parameters (idempotent) win_nssm: name: '{{ test_service_name }}' application: C:\Windows\System32\cmd.exe - app_parameters: - foo: true - '-file.out': 'output.bat' - '-path': 'C:\with space\' - '-str': 'test"quotes' - _: bar + app_parameters: foo=true; -file.out=output.bat; -path=C:\with space\; -str=test"quotes; _=bar register: mixed_params_again - name: get result of install service with dict parameters (idempotent) @@ -230,8 +220,8 @@ - mixed_params_again.changed == false - (mixed_params_again_actual.stdout|from_json).Exists == true - (mixed_params_again_actual.stdout|from_json).Parameters.Application == "C:\Windows\System32\cmd.exe" - # Expected value: bar -str "test\"quotes" -file.out output.bat foo True -path "C:\with space\\" (backslashes doubled for jinja) - - (mixed_params_again_actual.stdout|from_json).Parameters.AppParameters == 'bar -str "test\\"quotes" -file.out output.bat foo True -path "C:\\with space\\\\"' + # Expected value: bar -file.out output.bat -str "test\"quotes" foo true -path "C:\with space\\" (backslashes doubled for jinja) + - (mixed_params_again_actual.stdout|from_json).Parameters.AppParameters == 'bar -file.out output.bat -str "test\\"quotes" foo true -path "C:\\with space\\\\"' - name: remove service win_nssm: