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.
ESTABLISH SSH CONNECTION FOR USER: *******
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’“'”‘’
PUT /tmp/tmpGt6Vr_ TO /home//.ansible/tmp/ansible-tmp-1477059699.61-150822866162574/mymodules.ps1
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]’
ESTABLISH SSH CONNECTION FOR USER: *******
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.