Running VMware OS Optimization Tool with Ansible on Windows

Usually you optimize your Windows base images in a VMware vSphere environment by using VMware OS Optimization Tool. The VMware OS Optimization Tool helps in preparing and optimizing Windows and Windows Server systems for use with VMware Horizon.

In DevOps-World the process of building and optimizing Windows systems is fully automated. In lots of cases Ansible is a suitable tool for configuring Windows systems. Chaining the VMware OS Optimization Tool into your Ansible playbooks is a logical step as soon as Windows System should run on VMware.

VMware OS Optimization Tool provides commandline arguments for automation.

Unfortunately VMware OS Optimization Tool is an interactive tool, which needs an interactive Windows session. On the other hand when you run a win_shell or a win_command in Ansible, no interactive session is created on the target windows host. That means if you do something like this in your Ansible playbook.
This command will fail.

---
- hosts: windows
  tasks
    - name: Execute VMwareHorizonOSOptimizationTool
      win_command: 'C:\Temp\VMwareHorizonOSOptimizationTool.exe -o default'
 

There are some complicate hacks for getting an interactive shell using Ansible. The most easiest is running VMwareHorizonOSOptimizationTool.exe by using PsExec. PsExec is part of the Windows Sysinternals Tools.

PsExec is a light-weight telnet-replacement that lets you execute processes on other systems, complete with full interactivity for console applications, without having to manually install client software. PsExec’s most powerful uses include launching interactive command-prompts on remote systems and remote-enabling tools like IpConfig that otherwise do not have the ability to show information about remote systems.

Running VMwareHorizonOSOptimizationTool.exe using PsExec in an Ansible playbook looks like this. (Make sure to use the full path to VMwareHorizonOSOptimizationTool.exe)
This command will succeed.

---
- hosts: windows
  tasks
    - name: Execute VMwareHorizonOSOptimizationTool
      win_command: 'C:\Temp\psexec.exe -accepteula -nobanner -i 1 -s C:\Temp\VMwareHorizonOSOptimizationTool.exe -o default'