Over the last few weeks I have been lifting, shifting and reshaping some of the home lab and within that process we needed some more templates for both Windows and Linux.

I found an amazing project GitHub Repo – vmware-samples/packer-examples-for-vsphere

And Documentation can be found here

This will give you the ability to quickly get some Linux and Windows templates up and running quickly in your vSphere environment.

My advice from the start is do not use WSL (Windows Subsystem for Linux) but that could be my own user error.

I am using an Ubuntu server in my home lab to perform these tasks and I hit a snag not with the configuration but with some of the dependancies you need to run.

Fixing the “No Module Named ‘winrm'” Error in Packer + Ansible for Windows VM Provisioning

When using Packer with Ansible to provision Windows virtual machines on vSphere, I recently encountered the following error during the Ansible playbook execution:


fatal: [default]: FAILED! => {"msg": "winrm or requests is not installed: No module named 'winrm'"}

This stopped my automated build in its tracks. After some debugging, I found that Ansible’s execution environment was missing the pywinrm module, which is required for managing Windows hosts via WinRM. Here’s how I diagnosed and fixed the issue.

Understanding the Problem

Ansible relies on the pywinrm Python module to communicate with Windows hosts using the WinRM (Windows Remote Management) protocol. If this module isn’t installed in the correct environment, Ansible cannot establish a connection, resulting in the “No module named ‘winrm'” error.

Even though pywinrm might be installed in the system’s Python, Packer’s execution context (often running inside a virtual environment) might not have access to it.

Step-by-Step Solution

  1. Check if pywinrm is Installed in the Correct Python Environment
    Since Ansible was running inside a pipx-managed virtual environment, I first verified whether the winrm module was available:

/home/veeam/.local/pipx/venvs/ansible/bin/python -c "import winrm; print(winrm)"

This returned:


ModuleNotFoundError: No module named 'winrm'

That confirmed the issue—pywinrm was missing from the environment Ansible was using.

  1. Install pywinrm in the Correct Environment
    Since Ansible was installed via pipx, I needed to install pywinrm inside the same environment rather than globally.

Option 1: Using pipx to Inject pywinrm


pipx inject ansible pywinrm

This ensures that pywinrm is available within Ansible’s execution context.

Option 2: Installing Directly in the Virtual Environment
If you prefer, you can manually install pywinrm inside the virtual environment:


/home/veeam/.local/pipx/venvs/ansible/bin/pip install pywinrm

If pip is missing, install it first:


apt install python3-pip -y
  1. Verify the Fix
    To confirm that pywinrm is now correctly installed, run:

/home/veeam/.local/pipx/venvs/ansible/bin/python -c "import winrm; print(winrm)"

If no errors appear, the installation was successful!

  1. Re-run Packer and Ansible
    With pywinrm installed, I restarted the Packer build:

packer build -var-file=variables.pkrvars.hcl windows-server.pkr.hcl

This time, Ansible successfully connected to the Windows VM over WinRM, and provisioning completed without issues. 🎉

Final Thoughts

This issue highlighted an important lesson about managing dependencies within virtual environments. When working with Packer and Ansible, always ensure that required Python modules are installed inside the environment that Ansible is running in.

By using pipx inject, I was able to keep my environment clean while ensuring Ansible had access to the necessary modules. If you run into similar issues, check:

✅ Where Ansible is installed
✅ Which Python environment it’s using
✅ That required modules like pywinrm are installed in the same environment

Hope this helps anyone facing the same issue! 🚀

Leave a Reply

Your email address will not be published. Required fields are marked *