Cmdlets output handling 2.6 (#42721)

* fix win_unzip and win_share Powershell cmdlets output invalid JSONs (#42713)


(cherry picked from commit b2527c55c3)

* Added changelog fragment
pull/42147/merge
Jordan Borean 6 years ago committed by Matt Clay
parent 56caa66290
commit e3a0431c77

@ -0,0 +1,3 @@
bugfixes:
- win_share - discard any cmdlet output we don't use to ensure only the return json is received by Ansible
- win_unzip - discard any cmdlet output we don't use to ensure only the return json is received by Ansible

@ -57,7 +57,7 @@ Try {
If ($share) { If ($share) {
# See message around -WhatIf where $check_mode is defined # See message around -WhatIf where $check_mode is defined
if (-not $check_mode) { if (-not $check_mode) {
Remove-SmbShare -Force -Name $name Remove-SmbShare -Force -Name $name | Out-Null
} }
$result.actions += "Remove-SmbShare -Force -Name $name" $result.actions += "Remove-SmbShare -Force -Name $name"
$result.changed = $true $result.changed = $true
@ -87,7 +87,7 @@ Try {
# need to (re-)create share # need to (re-)create share
If (-not $share) { If (-not $share) {
if (-not $check_mode) { if (-not $check_mode) {
New-SmbShare -Name $name -Path $path New-SmbShare -Name $name -Path $path | Out-Null
} }
$share = Get-SmbShare -Name $name -ErrorAction SilentlyContinue $share = Get-SmbShare -Name $name -ErrorAction SilentlyContinue
@ -96,8 +96,8 @@ Try {
} }
If ($share.Path -ne $path) { If ($share.Path -ne $path) {
if (-not $check_mode) { if (-not $check_mode) {
Remove-SmbShare -Force -Name $name Remove-SmbShare -Force -Name $name | Out-Null
New-SmbShare -Name $name -Path $path New-SmbShare -Name $name -Path $path | Out-Null
} }
$share = Get-SmbShare -Name $name -ErrorAction SilentlyContinue $share = Get-SmbShare -Name $name -ErrorAction SilentlyContinue
$result.changed = $true $result.changed = $true
@ -108,28 +108,28 @@ Try {
# updates # updates
If ($share.Description -ne $description) { If ($share.Description -ne $description) {
if (-not $check_mode) { if (-not $check_mode) {
Set-SmbShare -Force -Name $name -Description $description Set-SmbShare -Force -Name $name -Description $description | Out-Null
} }
$result.changed = $true $result.changed = $true
$result.actions += "Set-SmbShare -Force -Name $name -Description $description" $result.actions += "Set-SmbShare -Force -Name $name -Description $description"
} }
If ($share.FolderEnumerationMode -ne $folderEnum) { If ($share.FolderEnumerationMode -ne $folderEnum) {
if (-not $check_mode) { if (-not $check_mode) {
Set-SmbShare -Force -Name $name -FolderEnumerationMode $folderEnum Set-SmbShare -Force -Name $name -FolderEnumerationMode $folderEnum | Out-Null
} }
$result.changed = $true $result.changed = $true
$result.actions += "Set-SmbShare -Force -Name $name -FolderEnumerationMode $folderEnum" $result.actions += "Set-SmbShare -Force -Name $name -FolderEnumerationMode $folderEnum"
} }
if ($share.CachingMode -ne $cachingMode) { if ($share.CachingMode -ne $cachingMode) {
if (-not $check_mode) { if (-not $check_mode) {
Set-SmbShare -Force -Name $name -CachingMode $cachingMode Set-SmbShare -Force -Name $name -CachingMode $cachingMode | Out-Null
} }
$result.changed = $true $result.changed = $true
$result.actions += "Set-SmbShare -Force -Name $name -CachingMode $cachingMode" $result.actions += "Set-SmbShare -Force -Name $name -CachingMode $cachingMode"
} }
if ($share.EncryptData -ne $encrypt) { if ($share.EncryptData -ne $encrypt) {
if (-not $check_mode) { if (-not $check_mode) {
Set-SmbShare -Force -Name $name -EncryptData $encrypt Set-SmbShare -Force -Name $name -EncryptData $encrypt | Out-Null
} }
$result.changed = $true $result.changed = $true
$result.actions += "Set-SmbShare -Force -Name $name -EncryptData $encrypt" $result.actions += "Set-SmbShare -Force -Name $name -EncryptData $encrypt"
@ -137,11 +137,11 @@ Try {
# clean permissions that imply others # clean permissions that imply others
ForEach ($user in $permissionFull) { ForEach ($user in $permissionFull) {
$permissionChange.remove($user) $permissionChange.remove($user) | Out-Null
$permissionRead.remove($user) $permissionRead.remove($user) | Out-Null
} }
ForEach ($user in $permissionChange) { ForEach ($user in $permissionChange) {
$permissionRead.remove($user) $permissionRead.remove($user) | Out-Null
} }
# remove permissions # remove permissions
@ -156,20 +156,20 @@ Try {
if (-not ($permission.AccountName -eq 'Everyone' -and $cim_count -eq 1)) { if (-not ($permission.AccountName -eq 'Everyone' -and $cim_count -eq 1)) {
If (-not ($permissionDeny.Contains($permission.AccountName))) { If (-not ($permissionDeny.Contains($permission.AccountName))) {
if (-not $check_mode) { if (-not $check_mode) {
Unblock-SmbShareAccess -Force -Name $name -AccountName $permission.AccountName Unblock-SmbShareAccess -Force -Name $name -AccountName $permission.AccountName | Out-Null
} }
$result.changed = $true $result.changed = $true
$result.actions += "Unblock-SmbShareAccess -Force -Name $name -AccountName $($permission.AccountName)" $result.actions += "Unblock-SmbShareAccess -Force -Name $name -AccountName $($permission.AccountName)"
} else { } else {
# Remove from the deny list as it already has the permissions # Remove from the deny list as it already has the permissions
$permissionDeny.remove($permission.AccountName) $permissionDeny.remove($permission.AccountName) | Out-Null
} }
} }
} ElseIf ($permission.AccessControlType -eq "Allow") { } ElseIf ($permission.AccessControlType -eq "Allow") {
If ($permission.AccessRight -eq "Full") { If ($permission.AccessRight -eq "Full") {
If (-not ($permissionFull.Contains($permission.AccountName))) { If (-not ($permissionFull.Contains($permission.AccountName))) {
if (-not $check_mode) { if (-not $check_mode) {
Revoke-SmbShareAccess -Force -Name $name -AccountName $permission.AccountName Revoke-SmbShareAccess -Force -Name $name -AccountName $permission.AccountName | Out-Null
} }
$result.changed = $true $result.changed = $true
$result.actions += "Revoke-SmbShareAccess -Force -Name $name -AccountName $($permission.AccountName)" $result.actions += "Revoke-SmbShareAccess -Force -Name $name -AccountName $($permission.AccountName)"
@ -178,11 +178,11 @@ Try {
} }
# user got requested permissions # user got requested permissions
$permissionFull.remove($permission.AccountName) $permissionFull.remove($permission.AccountName) | Out-Null
} ElseIf ($permission.AccessRight -eq "Change") { } ElseIf ($permission.AccessRight -eq "Change") {
If (-not ($permissionChange.Contains($permission.AccountName))) { If (-not ($permissionChange.Contains($permission.AccountName))) {
if (-not $check_mode) { if (-not $check_mode) {
Revoke-SmbShareAccess -Force -Name $name -AccountName $permission.AccountName Revoke-SmbShareAccess -Force -Name $name -AccountName $permission.AccountName | Out-Null
} }
$result.changed = $true $result.changed = $true
$result.actions += "Revoke-SmbShareAccess -Force -Name $name -AccountName $($permission.AccountName)" $result.actions += "Revoke-SmbShareAccess -Force -Name $name -AccountName $($permission.AccountName)"
@ -191,11 +191,11 @@ Try {
} }
# user got requested permissions # user got requested permissions
$permissionChange.remove($permission.AccountName) $permissionChange.remove($permission.AccountName) | Out-Null
} ElseIf ($permission.AccessRight -eq "Read") { } ElseIf ($permission.AccessRight -eq "Read") {
If (-not ($permissionRead.Contains($permission.AccountName))) { If (-not ($permissionRead.Contains($permission.AccountName))) {
if (-not $check_mode) { if (-not $check_mode) {
Revoke-SmbShareAccess -Force -Name $name -AccountName $permission.AccountName Revoke-SmbShareAccess -Force -Name $name -AccountName $permission.AccountName | Out-Null
} }
$result.changed = $true $result.changed = $true
$result.actions += "Revoke-SmbShareAccess -Force -Name $name -AccountName $($permission.AccountName)" $result.actions += "Revoke-SmbShareAccess -Force -Name $name -AccountName $($permission.AccountName)"
@ -204,7 +204,7 @@ Try {
} }
# user got requested permissions # user got requested permissions
$permissionRead.Remove($permission.AccountName) $permissionRead.Remove($permission.AccountName) | Out-Null
} }
} }
} }
@ -212,28 +212,28 @@ Try {
# add missing permissions # add missing permissions
ForEach ($user in $permissionRead) { ForEach ($user in $permissionRead) {
if (-not $check_mode) { if (-not $check_mode) {
Grant-SmbShareAccess -Force -Name $name -AccountName $user -AccessRight "Read" Grant-SmbShareAccess -Force -Name $name -AccountName $user -AccessRight "Read" | Out-Null
} }
$result.changed = $true $result.changed = $true
$result.actions += "Grant-SmbShareAccess -Force -Name $name -AccountName $user -AccessRight Read" $result.actions += "Grant-SmbShareAccess -Force -Name $name -AccountName $user -AccessRight Read"
} }
ForEach ($user in $permissionChange) { ForEach ($user in $permissionChange) {
if (-not $check_mode) { if (-not $check_mode) {
Grant-SmbShareAccess -Force -Name $name -AccountName $user -AccessRight "Change" Grant-SmbShareAccess -Force -Name $name -AccountName $user -AccessRight "Change" | Out-Null
} }
$result.changed = $true $result.changed = $true
$result.actions += "Grant-SmbShareAccess -Force -Name $name -AccountName $user -AccessRight Change" $result.actions += "Grant-SmbShareAccess -Force -Name $name -AccountName $user -AccessRight Change"
} }
ForEach ($user in $permissionFull) { ForEach ($user in $permissionFull) {
if (-not $check_mode) { if (-not $check_mode) {
Grant-SmbShareAccess -Force -Name $name -AccountName $user -AccessRight "Full" Grant-SmbShareAccess -Force -Name $name -AccountName $user -AccessRight "Full" | Out-Null
} }
$result.changed = $true $result.changed = $true
$result.actions += "Grant-SmbShareAccess -Force -Name $name -AccountName $user -AccessRight Full" $result.actions += "Grant-SmbShareAccess -Force -Name $name -AccountName $user -AccessRight Full"
} }
ForEach ($user in $permissionDeny) { ForEach ($user in $permissionDeny) {
if (-not $check_mode) { if (-not $check_mode) {
Block-SmbShareAccess -Force -Name $name -AccountName $user Block-SmbShareAccess -Force -Name $name -AccountName $user | Out-Null
} }
$result.changed = $true $result.changed = $true
$result.actions += "Block-SmbShareAccess -Force -Name $name -AccountName $user" $result.actions += "Block-SmbShareAccess -Force -Name $name -AccountName $user"

@ -101,7 +101,7 @@ $ext = [System.IO.Path]::GetExtension($src)
If (-Not (Test-Path -LiteralPath $dest -PathType Container)){ If (-Not (Test-Path -LiteralPath $dest -PathType Container)){
Try{ Try{
New-Item -ItemType "directory" -path $dest -WhatIf:$check_mode New-Item -ItemType "directory" -path $dest -WhatIf:$check_mode | out-null
} Catch { } Catch {
Fail-Json -obj $result -message "Error creating '$dest' directory! Msg: $($_.Exception.Message)" Fail-Json -obj $result -message "Error creating '$dest' directory! Msg: $($_.Exception.Message)"
} }
@ -180,5 +180,4 @@ If ($delete_archive){
} }
$result.removed = $true $result.removed = $true
} }
Exit-Json $result Exit-Json $result

Loading…
Cancel
Save