From 1cfaa5caab861464fe8df964ef6fcafb599d1da9 Mon Sep 17 00:00:00 2001 From: Dag Wieers Date: Fri, 24 Mar 2017 04:05:22 +0100 Subject: [PATCH] win_command/win_shell: Clean up and make coherent (#21371) * win_command/win_shell: Clean up and make coherent Changes include: - Make win_command and win_shell more coherent - Make condensed one-line more readable * Revert to original formatting --- lib/ansible/modules/windows/win_command.ps1 | 49 ++++++++++++--------- lib/ansible/modules/windows/win_shell.ps1 | 25 ++++++----- 2 files changed, 43 insertions(+), 31 deletions(-) diff --git a/lib/ansible/modules/windows/win_command.ps1 b/lib/ansible/modules/windows/win_command.ps1 index 4dd3df986e..0b8f01bb52 100644 --- a/lib/ansible/modules/windows/win_command.ps1 +++ b/lib/ansible/modules/windows/win_command.ps1 @@ -22,24 +22,7 @@ Set-StrictMode -Version 2 $ErrorActionPreference = "Stop" -$parsed_args = Parse-Args $args $false - -$raw_command_line = $(Get-AnsibleParam $parsed_args "_raw_params" -failifempty $true).Trim() -$chdir = Get-AnsibleParam $parsed_args "chdir" -type "path" -$creates = Get-AnsibleParam $parsed_args "creates" -type "path" -$removes = Get-AnsibleParam $parsed_args "removes" -type "path" - -$result = @{changed=$true; warnings=@(); cmd=$raw_command_line} - -If($creates -and $(Test-Path $creates)) { - Exit-Json @{cmd=$raw_command_line; msg="skipped, since $creates exists"; changed=$false; skipped=$true; rc=0} -} - -If($removes -and -not $(Test-Path $removes)) { - Exit-Json @{cmd=$raw_command_line; msg="skipped, since $removes does not exist"; changed=$false; skipped=$true; rc=0} -} - -$util_def = @' +$helper_def = @' using System; using System.ComponentModel; using System.Diagnostics; @@ -101,12 +84,34 @@ namespace Ansible.Command } '@ -$util_type = Add-Type -TypeDefinition $util_def +$params = Parse-Args $args -supports_check_mode $false -# FUTURE: extract this code to separate module_utils as Windows module API version of run_command +$raw_command_line = Get-AnsibleParam -obj $params -name "_raw_params" -type "str" -failifempty $true +$chdir = Get-AnsibleParam -obj $params -name "chdir" -type "path" +$creates = Get-AnsibleParam -obj $params -name "creates" -type "path" +$removes = Get-AnsibleParam -obj $params -name "removes" -type "path" + +$raw_command_line = $raw_command_line.Trim() + +$result = @{ + changed = $true + cmd = $raw_command_line +} + +If($creates -and $(Test-Path -Path $creates)) { + Exit-Json @{msg="skipped, since $creates exists";cmd=$raw_command_line;changed=$false;skipped=$true;rc=0} +} + +If($removes -and -not $(Test-Path -Path $removes)) { + Exit-Json @{msg="skipped, since $removes does not exist";cmd=$raw_command_line;changed=$false;skipped=$true;rc=0} +} + +Add-Type -TypeDefinition $helper_def $exec_args = $null +# FUTURE: extract this code to separate module_utils as Windows module API version of run_command + # Parse the command-line with the Win32 parser to get the application name to run. The Win32 parser # will deal with quoting/escaping for us... # FUTURE: no longer necessary once we switch to raw Win32 CreateProcess @@ -138,7 +143,7 @@ Catch [System.ComponentModel.Win32Exception] { # fail nicely for "normal" error conditions # FUTURE: this probably won't work on Nano Server $excep = $_ - Exit-Json @{failed=$true;changed=$false;cmd=$raw_command_line;rc=$excep.Exception.NativeErrorCode;msg=$excep.Exception.Message} + Exit-Json @{msg = $excep.Exception.Message; cmd = $raw_command_line; changed = $false; rc = $excep.Exception.NativeErrorCode} } $stdout = $stderr = [string] $null @@ -148,6 +153,8 @@ $stdout = $stderr = [string] $null $result.stdout = $stdout $result.stderr = $stderr +# TODO: decode CLIXML stderr output (and other streams?) + $proc.WaitForExit() | Out-Null $result.rc = $proc.ExitCode diff --git a/lib/ansible/modules/windows/win_shell.ps1 b/lib/ansible/modules/windows/win_shell.ps1 index 3e844d67c6..3da4f6adf8 100644 --- a/lib/ansible/modules/windows/win_shell.ps1 +++ b/lib/ansible/modules/windows/win_shell.ps1 @@ -60,22 +60,27 @@ namespace Ansible.Shell } "@ -$parsed_args = Parse-Args $args $false +$params = Parse-Args $args -supports_check_mode $false -$raw_command_line = $(Get-AnsibleParam $parsed_args "_raw_params" -failifempty $true).Trim() -$chdir = Get-AnsibleParam $parsed_args "chdir" -type "path" -$executable = Get-AnsibleParam $parsed_args "executable" -type "path" -$creates = Get-AnsibleParam $parsed_args "creates" -type "path" -$removes = Get-AnsibleParam $parsed_args "removes" -type "path" +$raw_command_line = Get-AnsibleParam -obj $params -name "_raw_params" -type "str" -failifempty $true +$chdir = Get-AnsibleParam -obj $params -name "chdir" -type "path" +$executable = Get-AnsibleParam -obj $params -name "executable" -type "path" +$creates = Get-AnsibleParam -obj $params -name "creates" -type "path" +$removes = Get-AnsibleParam -obj $params -name "removes" -type "path" -$result = @{changed=$true; warnings=@(); cmd=$raw_command_line} +$raw_command_line = $raw_command_line.Trim() + +$result = @{ + changed = $true + cmd = $raw_command_line +} If($creates -and $(Test-Path $creates)) { - Exit-Json @{cmd=$raw_command_line; msg="skipped, since $creates exists"; changed=$false; skipped=$true; rc=0} + Exit-Json @{msg="skipped, since $creates exists";cmd=$raw_command_line;changed=$false;skipped=$true;rc=0} } If($removes -and -not $(Test-Path $removes)) { - Exit-Json @{cmd=$raw_command_line; msg="skipped, since $removes does not exist"; changed=$false; skipped=$true; rc=0} + Exit-Json @{msg="skipped, since $removes does not exist";cmd=$raw_command_line;changed=$false;skipped=$true;rc=0} } Add-Type -TypeDefinition $helper_def @@ -117,7 +122,7 @@ Catch [System.ComponentModel.Win32Exception] { # fail nicely for "normal" error conditions # FUTURE: this probably won't work on Nano Server $excep = $_ - Exit-Json @{failed=$true;changed=$false;cmd=$raw_command_line;rc=$excep.Exception.NativeErrorCode;msg=$excep.Exception.Message} + Exit-Json @{msg = $excep.Exception.Message; cmd = $raw_command_line; changed = $false; rc = $excep.Exception.NativeErrorCode} } $stdout = $stderr = [string] $null