How to fire a custom (python) service and continue in powershell using ansible?
<p>I am trying to start a python service in a windows host using ansible. I have tried using both Start-Job and Start-Process as follows. But I am not able to get the exact results.</p>

<p>Using Start-Job</p>

<pre><code>Start-Job -ScriptBlock {Start-Process C:\Users\voiceqa\ansitest\Scripts\python.exe C:\Users\voiceqa\ansitest\Scripts\run_wireshark_service.py -PassThru -RedirectStandardError C:\Users\voiceqa\error.txt -RedirectStandardOutput C:\Users\voiceqa\output.txt -NoNewWindow 
</code></pre>

<p>The problem with this is as soon as ansible comes out of the session, Start-Job stops which inturn kills the process that it is running.</p>

<p>Using Start-Process</p>

<pre><code>Start-Process powershell -ArgumentList "C:\Users\voiceqa\ansitest\Scripts\python.exe C:\Users\voiceqa\ansitest\Scripts\run_wireshark_service.py" -WindowStyle Hidden -RedirectStandardError C:\Users\voiceqa\error.txt -RedirectStandardOutput C:\Users\voiceqa\output.txt -PassThru 

Start-Process C:\Users\voiceqa\ansitest\Scripts\python.exe -ArgumentList "C:\Users\voiceqa\ansitest\Scripts\run_wireshark_service.py" -WindowStyle Hidden -RedirectStandardError C:\Users\voiceqa\error.txt -RedirectStandardOutput C:\Users\voiceqa\output.txt -PassThru -UseNewEnvironment| Export-Clixml -Path C:\Users\voiceqa\wiresharkservice.xml

Start-Process C:\Users\voiceqa\ansitest\Scripts\python.exe C:\Users\voiceqa\ansitest\Scripts\run_wireshark_service.py -PassThru -NoNewWindow
</code></pre>

<p>I have tried all these. All of these have same issues. Ansible is waiting for these commands to finish ( which it won't) as shown</p>

<p><a href="http://i.stack.imgur.com/EHwU3.png" rel="nofollow"><img src="http://i.stack.imgur.com/EHwU3.png" alt="Here"></a>
All I need is fire this python service and continue with rest of the work. How can I achieve this functionality? Any help would be welcome.</p>

<p>It's tricky to do in the currently-released versions of Ansible. Even if you managed to background the tasks via <code>raw:</code> (which is possible with some hoop-jumping), WinRM won't let the command complete until all the processes exit (WinRM runs everything under a Windows job object). You have to escape the job.</p>

<p>Ansible 2.2 will have async, win_shell and win_command, but async isn't currently the right thing, since it leaves a watchdog process running that will kill the child process after the async timeout has elapsed. I've been testing a "breakaway" option for command/shell that will allow you to do what you're wanting (not sure if it'll be ready for primetime by 2.2 module freeze though). </p>

<p>If you're really running a service, I'd suggest setting it up as a Windows service (either directly, via sc, or using something like NSSM).</p>

<p>If that's a no-go, you can run background processes in the current version of Ansible (so long as you don't need access to stdin/stdout/stderr of the launched process) via <code>raw:</code> and WMI like this: </p>

<p><code>raw: ([wmiclass]"Win32_Process").Create("myprocess.exe /option1 /option2")</code></p>

python powershell ansible ansible-playbook