salt icon indicating copy to clipboard operation
salt copied to clipboard

masterless windows: unable to run powershell on standalone minion

Open emmellee opened this issue 13 years ago • 15 comments

I am trying to pull the IP from one of my interfaces.

I didn't have any luck with network.interfaces, so I'm trying powershell. However the syntax inside pillars has me stumped.

I created a script and tried referencing the powershell file within pillars using cmd.script, but no luck on that either.

Which leads me to finally ask does powershell work within pillars?

emmellee avatar Nov 15 '13 23:11 emmellee

Have you tried salt['cmd.run']('hostname', shell='powershell')?

basepi avatar Nov 18 '13 18:11 basepi

I must not have. Thank you. But, because my Get-WmiObject command above has embedded single and double quotes, I'm not able to successfully wrap the command for use with cmd.run.

I've tried using cmd.script like so:

OIP: {{ salt['cmd.script']('salt://scripts/get-OOBIP.ps1', shell='powershell') }}

But unfortunately, that is deleting my script and errors with:

C:\salt>salt-call state.sls jenkins
[INFO    ] Executing command 'Powershell -File D:\\\\srv\\salt\\file_roots\\scri
pts/get-OOBIP.ps1' in directory 'C:\\Users\\edited'
[ERROR   ] An un-handled exception was caught by salt's global exception handler
:
ExtraData: unpack(b) recieved extra data.
Traceback (most recent call last):
  File "<string>", line 6, in <module>
  File "__main__.py", line 726, in <module>
  File "__main__.py", line 332, in bootstrap
  File "__main__.py", line 359, in chainload
  File "__main__.py", line 715, in _chainload
  File "__main__.py", line 128, in <module>
  File "__main__salt-call__.py", line 14, in <module>
  File "salt/scripts.py", line 77, in salt_call
  File "salt/cli/__init__.py", line 303, in run
  File "salt/cli/caller.py", line 135, in run
  File "salt/cli/caller.py", line 78, in call
  File "salt/modules/state.py", line 305, in sls
  File "salt/modules/state.py", line 91, in running
  File "salt/modules/saltutil.py", line 365, in is_running
  File "salt/modules/saltutil.py", line 392, in running
  File "salt/payload.py", line 95, in loads
  File "_unpacker.pyx", line 114, in msgpack._unpacker.unpackb (msgpack/_unpacke
r.cpp:114)
ExtraData: unpack(b) recieved extra data.
Traceback (most recent call last):
  File "<string>", line 6, in <module>
  File "__main__.py", line 726, in <module>
  File "__main__.py", line 332, in bootstrap
  File "__main__.py", line 359, in chainload
  File "__main__.py", line 715, in _chainload
  File "__main__.py", line 128, in <module>
  File "__main__salt-call__.py", line 14, in <module>
  File "salt/scripts.py", line 77, in salt_call
  File "salt/cli/__init__.py", line 303, in run
  File "salt/cli/caller.py", line 135, in run
  File "salt/cli/caller.py", line 78, in call
  File "salt/modules/state.py", line 305, in sls
  File "salt/modules/state.py", line 91, in running
  File "salt/modules/saltutil.py", line 365, in is_running
  File "salt/modules/saltutil.py", line 392, in running
  File "salt/payload.py", line 95, in loads
  File "_unpacker.pyx", line 114, in msgpack._unpacker.unpackb (msgpack/_unpacke
r.cpp:114)
msgpack.exceptions.ExtraData: unpack(b) recieved extra data.

I suppose I'm sending the wrong arguments or my script is returning an array which can't be handled. It's probably the latter and I'll look at that. Don't know why salt is wiping out my script, though. Unfortunately, I'm new to both salt and powershell.

Thanks for your help.

emmellee avatar Nov 18 '13 20:11 emmellee

My script has two lines and successfully returns an IP when run from the command line:

$twa = Get-WmiObject -Class Win32_NetworkAdapter -Filter "NetConnectionID='TWA'" | %{$_.GetRelated('Win32_NetworkAdapterConfiguration') } 
Write-Output $twa.IPAddress

But salt deletes my script when called from pillars:

OIP: {{ salt['cmd.script']('salt://scripts/get-OOBIP.ps1', shell='powershell') }}

Wrong syntax, number of arguments?

emmellee avatar Nov 18 '13 21:11 emmellee

So, why did network.interfaces or network.ip_addrs not work for you?

On Fri, Nov 15, 2013 at 4:49 PM, emmellee [email protected] wrote:

I am trying to pull the IP from one of my interfaces.

I didn't have any luck with network.interfaces, so I'm trying powershell. However the syntax inside pillars has me stumped.

Here is my powershell command:

Get-WmiObject -Class Win32_NetworkAdapter -Filter "NetConnectionID='TWA'" | %{$_.GetRelated('Win32_NetworkAdapterConfiguration') } |format-table IPAddress -hidetableheaders

Starting small, this works from the command line:

salt-call cmd.run hostname shell=powershell

But I cannot determine the proper syntax to run hostname with the alternate shell powershell within pillars and I've found no clues in the documentation or forum.

The default command.exe shell works inside pillars like so:

myhost: {{ salt'cmd.run' http://'hostname' }}

but I need to invoke powershell for what I'm after. But if I can't get hostname to work, no need to try further.

I created a script and tried referencing the powershell file within pillars using cmd.script, but no luck on that either.

Which leads me to finally ask does powershell work within pillars?

— Reply to this email directly or view it on GitHubhttps://github.com/saltstack/salt/issues/8579 .

Dave Boucha | Sr. Engineer

Join us at SaltConf, Jan. 28-30, 2014 in Salt Lake City. www.saltconf.com

5272 South College Drive, Suite 301 | Murray, UT 84123 office 801-305-3563 [email protected] | www.saltstack.com http://saltstack.com/

UtahDave avatar Nov 18 '13 21:11 UtahDave

Because I would need to know ahead of time which physical interface I would need to query. I cannot be assured that the particular network/IP I'm looking for will always be on the same physical interface of each server. I had to find a way to look at the tag the administrators used. I didn't know how salt might do that for me, but I found that powershell could. If I could just figure out how salt should call powershell...

But, after stopping the minion service, deleting the cache, ensuring the file exists, restarting the minion service, and then running salt-call, my script is always deleted and I receive something like this from pillars:

C:\salt>salt-call state.sls jenkins
[INFO    ] Executing command 'Powershell -File D:\\\\srv\\salt\\file_roots\\scri
pts/get-OOBIP.ps1' in directory 'C:\\Users\\edited'
[ERROR   ] Unable to cache file "salt://scripts/get-OOBIP.ps1" from env "base".
[ERROR   ] cmd.script: Unable to clean tempfile '': [Error 3] The system cannot
find the path specified: ''
[INFO    ] Loading fresh modules for state activity
blah-blah

emmellee avatar Nov 18 '13 22:11 emmellee

OK, have you looked at the docs here: http://docs.saltstack.com/ref/modules/all/salt.modules.cmdmod.html#salt.modules.cmdmod.run ?

There's an example of using powershell there.

On Mon, Nov 18, 2013 at 3:05 PM, emmellee [email protected] wrote:

Because I would need to know ahead of time which physical interface I would need to query. I cannot be assured that the particular network/IP I'm looking for will always be on the same physical interface of each server. I had to find a way to look at the tag the administrators used. I didn't know how salt might do that for me, but I found that powershell could. If I could just figure out how salt should call powershell...

But, after stopping the minion service, deleting the cache, ensuring the file exists, and then running salt-call, my script is always deleted and I receive something like this from pillars:

C:\salt>salt-call state.sls jenkins [INFO ] Executing command 'Powershell -File D:\srv\salt\file_roots
scri pts/get-OOBIP.ps1' in directory 'C:\Users\edited' [ERROR ] Unable to cache file "salt://scripts/get-OOBIP.ps1" from env "base". [ERROR ] cmd.script: Unable to clean tempfile '': [Error 3] The system cannot find the path specified: '' [INFO ] Loading fresh modules for state activity blah-blah

— Reply to this email directly or view it on GitHubhttps://github.com/saltstack/salt/issues/8579#issuecomment-28742844 .

Dave Boucha | Sr. Engineer

Join us at SaltConf, Jan. 28-30, 2014 in Salt Lake City. www.saltconf.com

5272 South College Drive, Suite 301 | Murray, UT 84123 office 801-305-3563 [email protected] | www.saltstack.com http://saltstack.com/

UtahDave avatar Nov 18 '13 22:11 UtahDave

Yes. I did. From what I could see, it should have worked as I have given it the source and the shell as arguments. Cannot figure out why it would delete my file unless it is overwriting it - and I don't know why it would do that, unless I've misunderstood its use entirely.

emmellee avatar Nov 18 '13 22:11 emmellee

And we can see from my output that it does seem to want to do the right thing:

Executing command 'Powershell -File D:\srv\salt\file_roots\scripts/get-OOBIP.ps1' in directory 'C:\Users\edited'

To error is one thing, but seems rather uncivilized for it to remove my script...?

I'm running 17.2 masterless minion on windows.

C:\salt>salt-call --version salt-call.exe 0.17.2

emmellee avatar Nov 18 '13 22:11 emmellee

OK, I was reading through the previous posts here. You can't run powershell commands from pillars because pillars are evaluated on the Salt Master; which won't be a windows machine.

UtahDave avatar Nov 18 '13 22:11 UtahDave

Gah, OK, sorry. You're running masterless on a Windows machine.

Let me test something here

UtahDave avatar Nov 18 '13 22:11 UtahDave

Thanks.

I've also tried a workaround using cmd.run as suggested in the salt-users forum:

OIP: {{ salt['cmd.run']('powershell -FILE d:/srv/salt/scripts/get-OOBIP.ps1') }}

But that incorrectly sets my OIP variable to this string:

"Processing -File 'D:\srv\salt"

emmellee avatar Nov 21 '13 23:11 emmellee

Any luck reproducing? Might this be a regression in 17.2 for windows or is this unique to my running in a windows masterless minion environment? If unique to me, I've kinda put my hopes in being able to invoke powershell in my saltstack configuration management setup. Crossing my fingers...

emmellee avatar Nov 22 '13 19:11 emmellee

Could you please redesignate this as a bug rather than a question? The problem being that I cannot run a powershell script via cmd.script in my masterless windows minion environment.

emmellee avatar Nov 25 '13 16:11 emmellee

Done.

basepi avatar Nov 25 '13 23:11 basepi

I've been successful in running powershell using cmd.run in a pillar environment using the following.

OIP: {{ salt['cmd.run']('D://srv/salt/file_roots/scripts/get-OOBIP.ps1', shell='powershell') }}

You may close ticket. Thank you.

emmellee avatar Dec 12 '13 23:12 emmellee