From 97f4f56286fd7b5de27d1e5c01a15e4a1dcc8a6a Mon Sep 17 00:00:00 2001 From: Matt Martz Date: Wed, 18 Jun 2014 11:48:50 -0500 Subject: [PATCH] Add Exit-Json and Fail-Json powershell helper functions --- lib/ansible/module_utils/powershell.ps1 | 17 +++++++++++++++++ library/windows/setup.ps1 | 2 +- library/windows/slurp.ps1 | 2 +- library/windows/win_ping.ps1 | 2 +- library/windows/win_stat.ps1 | 2 +- 5 files changed, 21 insertions(+), 4 deletions(-) diff --git a/lib/ansible/module_utils/powershell.ps1 b/lib/ansible/module_utils/powershell.ps1 index 54d2eedc4f2..8c05706b604 100644 --- a/lib/ansible/module_utils/powershell.ps1 +++ b/lib/ansible/module_utils/powershell.ps1 @@ -47,3 +47,20 @@ Function Set-Attr($obj, $name, $value) $obj | Add-Member -Force -MemberType NoteProperty -Name $name -Value $value } +# Helper function to convert a powershell object to JSON to echo it, exiting +# the script +Function Exit-Json($obj) +{ + echo $obj | ConvertTo-Json + Exit +} + +# Helper function to add the "msg" property and "failed" property, convert the +# powershell object to JSON and echo it, exiting the script +Function Fail-Json($obj, $message) +{ + Set-Attr $obj "msg" $message + Set-Attr $obj "failed" $true + echo $obj | ConvertTo-Json + Exit +} diff --git a/library/windows/setup.ps1 b/library/windows/setup.ps1 index 883c2792059..07d32c56f51 100644 --- a/library/windows/setup.ps1 +++ b/library/windows/setup.ps1 @@ -34,4 +34,4 @@ Set-Attr $result.ansible_facts "ansible_os_family" "Windows" Set-Attr $result.ansible_facts "ansible_distribution" $osversion.VersionString Set-Attr $result.ansible_facts "ansible_distribution_version" $osversion.Version.ToString() -echo $result | ConvertTo-Json; +Exit-Json $result; diff --git a/library/windows/slurp.ps1 b/library/windows/slurp.ps1 index d224539ee63..40bb5eba033 100644 --- a/library/windows/slurp.ps1 +++ b/library/windows/slurp.ps1 @@ -44,4 +44,4 @@ $result = New-Object psobject @{ encoding = "base64" }; Set-Attr $result "content" $content; -echo $result | ConvertTo-Json; +Exit-Json $result; diff --git a/library/windows/win_ping.ps1 b/library/windows/win_ping.ps1 index 0771ea31259..ec6d530025c 100644 --- a/library/windows/win_ping.ps1 +++ b/library/windows/win_ping.ps1 @@ -29,4 +29,4 @@ $result = New-Object psobject @{ changed = $false ping = $data }; -echo $result | ConvertTo-Json; +Exit-Json $result; diff --git a/library/windows/win_stat.ps1 b/library/windows/win_stat.ps1 index d7b754a2834..fa5596ec1b7 100644 --- a/library/windows/win_stat.ps1 +++ b/library/windows/win_stat.ps1 @@ -61,4 +61,4 @@ If ($get_md5 -and $result.stat.exists -and -not $result.stat.isdir) Set-Attr $result.stat "md5" $path_md5; } -echo $result | ConvertTo-Json; +Exit-Json $result;