I found some time to play around with this a bit more. It turns out the 
initial installer spawns multiple processes and doesn't actually wait until 
it is finished. This causes the parent WinRM process to close it's shell 
which has the side affect of killing any children it spawns.

It's weird behaviour from the installer as it should be waiting until all 
the installed processes have finish before finishing but this isn't the 
case. The reason why it works when you run locally or through 
'Enter-PSSession' is because those sessions are persistent, whereas Ansible 
and 'Invoke-Command' will close that session as soon as it's command is 
finished.

Luckily using async will allow you to escape this behaviour, using the 
following playbook you can install VS and wait until the installer has 
actually completed. It uses async on the first task so that the spawned 
installer is not killed when WinRM exits and then waits until it is 
complete in the 2nd task. Become isn't needed for this task, you just need 
to escape the WinRM job boundary which is what async is for.

---
- hosts: vagrant-windows
  gather_facts: no
  tasks:
  - name: download installer
    win_get_url:
      url: https:
//download.visualstudio.microsoft.com/download/pr/324e8588-c90a-4229-947f-d60efec43018/0baa0fb3754413e8048d3625ddf5d585/vs_enterprise.exe
      dest: C:\temp\vs_enterprise.exe
      force: no

  # Async is required because the installer spawns a detached process and 
doesn't wait until it is complete. Because
  # WinRM exits it kills any child processes including the spawned 
vs_installer. By using async, we can ensure the
  # child process continues to run in the background and is not reaped when 
WinRM exits.
  - name: install VS Enterprise
    win_command: c:\temp\vs_enterprise.exe --add Microsoft.VisualStudio.
Component.CoreEditor --quiet
    async: 60 # May need a higher timeout if the initial installer download 
takes too long

  # Because the installer is running in the background, we need to wait 
until it is complete before continuing. We also
  # set the exit code of this process to whatever the exit code is of the 
vs_installer process if found. This gives us
  # at least some error reporting in case something went bad
  - name: wait until process is complete
    win_shell: |
      $process = Get-Process -Name vs_installer -ErrorAction 
SilentlyContinue
      if ($process) {
          $process | Wait-Process
          $host.SetShouldExit($process.ExitCode)
      } else {
          $host.SetShouldExit(0)  # Clears out any error with Get-Process 
not finding a process
      }
    changed_when: False

Ultimately this seems like poor behaviour on the installers part but this 
is all out of Ansible's control.

Thanks

Jordan

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to ansible-devel+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to