The first thing I notice is that ansible requires that modules return JSON,
and in your case, you are just returning "test".

So maybe what you want to try is something like:

#!/usr/bin/powershell
# WANT_JSON
# POWERSHELL_COMMON
$result = New-Object psobject
Set-Attr $result "msg" "test"
Exit-Json $result


On Fri, Oct 21, 2016 at 9:50 AM, Rajendra Adhikari <[email protected]
> wrote:

> Has anybody successfully run powershell on Linux as an ansible module?
> Well, I have an ansible on one machine ServerAnsible. And, I have a
> powershell and powercli core installed on my linux machine ServerPS.  I
> would like to execute ansible module written in powershell from ansible
> machine to the powershell machine. I followed the ansible doc here,
> http://docs.ansible.com/ansible/intro_windows.html#
> developers-supported-modules-and-how-it-works But it looks like it is
> specific to Windows or works only when Powershell is on Windows machine.
> My end goal is to develop a vmware powercli modules that are delegated
> through a linux box running powershell and powercli instead of Windows
> machine.
> Here are what I did:
> Powershell machine: ServerPS
> Ansible machine: ServerAnsible
>
> File mymodule.ps1 at module directory:
> *#!/usr/bin/powershell*
> *# POWERSHELL_COMMON*
> *# WANT_JSON*
>
> *$resullt = Get-Date*
>
> *Exit-Json $result*
>
> ServerAnsible$ansible ServerPS -m mymodule.ps1
> ServerPS | FAILED! => {
>     "changed": false,
>     "failed": true,
>     "module_stderr": "",
>     "module_stdout": "\u001b[?1h\u001b=\u001b[39;
> 49m\u001b[31m\u001b[39;49m\u001b[31mThe variable '$result' cannot be
> retrieved because it has not been set.\u001b[39;49m\u001b[39;
> 49m\r\n\u001b[39;49m\u001b[31m\u001b[39;49m\u001b[31mAt
> /home/******/.ansible/tmp/ansible-tmp-1477059401.24-
> 235352349739057/mymod\u001b[39;49m\u001b[39;49m\r\n\u001b[
> 39;49m\u001b[31m\u001b[39;49m\u001b[31mules.ps1:233
> char:11\u001b[39;49m\u001b[39;49m\r\n\u001b[39;49m\u001b[31m\u001b[39;49m\u001b[31m+
> Exit-Json 
> $result\u001b[39;49m\u001b[39;49m\r\n\u001b[39;49m\u001b[31m\u001b[39;49m\u001b[31m+
>           
> ~~~~~~~\u001b[39;49m\u001b[39;49m\r\n\u001b[39;49m\u001b[31m\u001b[39;49m\u001b[31m
>    + CategoryInfo          : InvalidOperation: (result:String) [],
> RuntimeExc 
> \u001b[39;49m\u001b[39;49m\r\n\u001b[39;49m\u001b[31m\u001b[39;49m\u001b[31m
>   
> eption\u001b[39;49m\u001b[39;49m\r\n\u001b[39;49m\u001b[31m\u001b[39;49m\u001b[31m
>    + FullyQualifiedErrorId : VariableIsUndefined\u001b[39;
> 49m\u001b[39;49m\r\n\u001b[39;49m\u001b[31m\u001b[39;49m\u001b[31m
> \u001b[39;49m\u001b[39;49m\r\n",
>     "msg": "MODULE FAILURE",
>     "parsed": false
> }
>
> or, if I set the variable as:
> #!/usr/bin/powershell
> # POWERSHELL_COMMON
> # WANT_JSON
>
>
> $result="test"
> $resullt = Get-Date
>
> Exit-Json $result
>
> I am getting something like:
> ServerPS | FAILED! => {
>     "changed": false,
>     "failed": true,
>     "module_stderr": "",
>     "module_stdout": "\u001b[?1h\u001b=\"test\"\r\n",
>     "msg": "MODULE FAILURE",
>     "parsed": false
> }
>
> With verbose mode, looks like ansible is pushing the file to the remote
> server at least and trying to execute the powershell module.
> <ServerPS> ESTABLISH SSH CONNECTION FOR USER: *******
> <ServerPS> SSH: EXEC ssh -C -q -o ControlMaster=auto -o ControlPersist=60s
> -o StrictHostKeyChecking=no -o KbdInteractiveAuthentication=no -o
> PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey
> -o PasswordAuthentication=no -o User=******* -o ConnectTimeout=10 -o
> ControlPath=/home/*******/.ansible/cp/ansible-ssh-%h-%p-%r ServerPS
> '/bin/sh -c '"'"'( umask 77 && mkdir -p "` echo $HOME/.ansible/tmp/ansible-
> tmp-1477059699.61-150822866162574 `" && echo 
> ansible-tmp-1477059699.61-150822866162574="`
> echo $HOME/.ansible/tmp/ansible-tmp-1477059699.61-150822866162574 `" ) &&
> sleep 0'"'"''
> <ServerPS> PUT /tmp/tmpGt6Vr_ TO /home/*******/.ansible/tmp/
> ansible-tmp-1477059699.61-150822866162574/mymodules.ps1
> <ServerPS> SSH: EXEC sftp -b - -C -o ControlMaster=auto -o
> ControlPersist=60s -o StrictHostKeyChecking=no -o
> KbdInteractiveAuthentication=no -o PreferredAuthentications=
> gssapi-with-mic,gssapi-keyex,hostbased,publickey -o
> PasswordAuthentication=no -o User=******* -o ConnectTimeout=10 -o
> ControlPath=/home/*******/.ansible/cp/ansible-ssh-%h-%p-%r '[ServerPS]'
> <ServerPS> ESTABLISH SSH CONNECTION FOR USER: *******
> <ServerPS> SSH: EXEC ssh -C -q -o ControlMaster=auto -o ControlPersist=60s
> -o StrictHostKeyChecking=no -o KbdInteractiveAuthentication=no -o
> PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey
> -o PasswordAuthentication=no -o User=******* -o ConnectTimeout=10 -o
> ControlPath=/home/*******/.ansible/cp/ansible-ssh-%h-%p-%r -tt ServerPS
> '/bin/sh -c '"'"'LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8
> LC_MESSAGES=en_US.UTF-8 /usr/bin/powershell /home/*******/.ansible/tmp/
> ansible-tmp-1477059699.61-150822866162574/mymodules.ps1; rm -rf
> "/home/*******/.ansible/tmp/ansible-tmp-1477059699.61-150822866162574/" >
> /dev/null 2>&1 && sleep 0'"'"''
>
> Here is the powershell machine where I have a working powershell:
> [*******@ServerPS unixVMtool]$ powershell
> PowerShell
> Copyright (C) 2016 Microsoft Corporation. All rights reserved.
>
> PS /home/*******/unixVMtool> $PSVersionTable.PSVersion
> Major Minor Patch Label
> ----- ----- ----- -----
>     6     0     0 alpha
> PS /home/*******/unixVMtool> Get-Date | ConvertTo-Json
> {
>     "value":  "2016-10-21T07:32:26.261558-07:00",
>     "DisplayHint":  2,
>     "DateTime":  "Friday, October 21, 2016 7:32:26 AM"
> }
> PS /home/*******/unixVMtool>
>
>
> Wondering if powershell ansible module can even be run on powershell on
> Linux ?
>
> Any help on this is appreciated.
>
> Thanks.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/ansible-project/ac34b114-d5fe-4c0f-b9c7-60f8cfe211b4%40googlegroups.
> com
> <https://groups.google.com/d/msgid/ansible-project/ac34b114-d5fe-4c0f-b9c7-60f8cfe211b4%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>



-- 
Matt Martz
@sivel
sivel.net

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/CAD8N0v_gSWR_Jqix_TO6r3-zX0BciKQKop7N_ZbikSzHAY1W6g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to