script module for powershell

I’m seeing some very inconsistent results when trying to run a Powershell script. through the script module. I can’t quite pinpoint it yet but I think it has to do when I run an external application, through Powershell. To be specific I am running an unattended dcpromo command. My playbook appears to be correct because if I run it 3 to 4 times it will eventually complete and everything will work. However during various runs it often gets hung up with either errors in powershell, which often don’t get reported back or what seems to be winrm not responding anymore. Possibly a loss connection from pywinrm. I really don’t know. I’ve been attempting to debug and troubleshoot it, however the results are not consistent enough for me to determine where to start looking.

The first question is, are there some specific rules to running a powershell script with the script module. Do I need to exit a certain way? Or is there a timeout feature for an ansible task where it will just continue with the playbook? Often times the powershell script completes correctly its just ansible gets hung and does not continue.

I do feel like this has something to do with pywinrm since many many times the script will complete it’s just ansible doesen’t know about it. How can I debug pywinrm while in an ansible run. I’ve tried -vvvvv(5x, I think that’s the max) and really it just stalls more often then not at the WinRM EXEC call. Is there anything I can do in the ansible playbook or the powershell script that will create a consistent result or are there some steps that I can take to try and debug pywinrm?

Nic

The only time I have seen inconsistent behaviour similar to what you describe I was trying to control machines which were many network hops away from the controller.
One thing to try would be to keep an eye on the windows event log on the remote machines.

I guess it might be worth checking what kind of network speeds you are getting just in case you are working over a busy network.

Hope that's some help.

Jon

Verify that you're running a recent version of pywinrm (either
http://github.com/diyan/pywinrm/archive/master.zip#egg=pywinrm or the
latest from PyPI will work). Earlier versions didn't correctly handle a
script taking longer than 60 seconds.

PowerShell scripts that encounter an error won't exit with a nonzero return
code by default (which is what normally indicates an error to Ansible). A
script will need to use a try/catch block or trap block (e.g.
https://github.com/ansible/ansible/blob/devel/test/integration/roles/test_win_script/files/test_script_with_errors.ps1)
to exit appropriately in case of an error. Otherwise, error messages will
be printed to stderr and ignored.

If you need to start another program and make sure the script waits for it
to finish, you may need to use Start-Process/Wait-Process instead of simply
running the command.

Jon,
I am one hop away from the network, so I’m not sure that this is a problem. The network is not terribly busy, it’s a home lab so I’m not saturating the infrastructure sub mili-second pings from the Ansible host.

Nic

Chris,
Traps was the answer. DCPromo by default exits with non-zero 1-4 if successful and 11+ on failure. Checking the return code and exiting zero solved the problem. throwing and exception and catching the trap produced consistent results and I was able to run my playbook successfully from start to finish a few times without error. I still get some hangups from time to time on the fact Gathering stage but this is few and far between, Thank you very much!

Nic