To run commands on Windows with Ansible, you need WinRM enabled on the target, a Windows-capable connection plugin (usually winrm or psrp), and a playbook that uses the win_command or win_shell module. The shortest working path is: enable WinRM on the Windows host, install the pywinrm Python package on your control node, add the host to an inventory with ansible_connection=winrm, then run ansible -m win_command -a "whoami" as a test. This guide walks through that setup and the common pitfalls.

What You'll Need

  • A Linux or macOS control node with Ansible installed (Windows is not supported as a control node).
  • Python 3 and the pywinrm package on the control node.
  • A Windows target host (Windows 10/11 or Windows Server 2016 or later) with WinRM enabled and reachable over the network.
  • Administrator credentials on the Windows host, or a dedicated service account with the right privileges.
  • Network access from the control node to the Windows host on port 5985 (HTTP) or 5986 (HTTPS).

Step-by-Step

  1. Install the WinRM Python library on the control node. Run pip install pywinrm. If you use a virtual environment, activate it first. Ansible's Windows modules depend on this library; without it, connection attempts fail immediately.
  2. Enable WinRM on the Windows host. Open an elevated PowerShell window and run Enable-PSRemoting -Force. This configures the WinRM listener and firewall rules. For a quick test setup, you can also run winrm quickconfig. For production, prefer HTTPS with a certificate; the default HTTP listener is not encrypted.
  3. Check the WinRM service and listener. In PowerShell, run Get-Service WinRM to confirm the service is running, and winrm enumerate winrm/config/listener to see the listener. If the listener is missing, re-run Enable-PSRemoting.
  4. Create an inventory file on the control node. Make a file such as inventory.ini with a group and host entry:
    [windows]
    win1 ansible_host=192.0.2.10
    
    [windows:vars]
    ansible_user=Administrator
    ansible_password=YourPassword
    ansible_connection=winrm
    ansible_winrm_transport=ntlm
    ansible_winrm_server_cert_validation=ignore
    The ignore setting is only for lab setups with self-signed certificates; in production, use a trusted certificate and remove that line. Labels and variable names can differ slightly by Ansible version, but these are the standard ones.
  5. Test connectivity with a simple command. Run:
    ansible -i inventory.ini windows -m win_ping
    A successful response returns pong. If this fails, fix connectivity before writing playbooks.
  6. Run a one-off command. Use the win_command module for executables that are not shell built-ins:
    ansible -i inventory.ini windows -m win_command -a "whoami"
    For commands that need shell features (pipes, redirection, environment variables), use win_shell instead:
    ansible -i inventory.ini windows -m win_shell -a "Get-Process | Select-Object -First 5"
  7. Put it in a playbook. Create site.yml:
    - hosts: windows
      tasks:
        - name: Check current user
          win_command: whoami
          register: whoami_result
    
        - name: Show result
          debug:
            var: whoami_result.stdout
    Run it with ansible-playbook -i inventory.ini site.yml.
  8. Use win_command vs win_shell deliberately. win_command does not run through a shell, so it is safer and more predictable for calling .exe files. win_shell runs through PowerShell and supports pipelines and built-in cmdlets. Prefer the module that matches the command; do not wrap everything in win_shell by habit.
  9. Handle credentials securely. Avoid plaintext passwords in inventory. Use Ansible Vault to encrypt variables, or use a credential store. For domain accounts, set ansible_winrm_transport=kerberos and configure Kerberos on the control node.

Common Mistakes

  • Using command or shell modules on Windows hosts. Those are for Unix-like systems; Windows needs win_command and win_shell.
  • Forgetting to install pywinrm. The error usually mentions a missing WinRM library.
  • Leaving the default HTTP listener in place for production. Traffic is unencrypted; enable HTTPS.
  • Setting ansible_winrm_server_cert_validation=ignore permanently. It hides certificate problems instead of fixing them.
  • Assuming PowerShell is the default shell for win_command. It is not; use win_shell when you need PowerShell syntax.
  • Running Ansible from a Windows control node. Ansible control nodes are Linux/macOS only.

Tips

  • Use win_ping as the first test after any inventory change.
  • Prefer HTTPS with a proper certificate for any host outside a lab.
  • Use register and debug to inspect stdout and stderr from commands.
  • For repeatable tasks, use dedicated modules (e.g., win_service, win_file) instead of raw commands where one exists.
  • Keep credentials out of version control; use Vault or an external secret store.

Troubleshooting

If win_ping fails, check in this order: confirm the WinRM service is running on the target (Get-Service WinRM), confirm the listener exists (winrm enumerate winrm/config/listener), confirm the firewall allows port 5985 or 5986, and confirm the control node can reach the host (try Test-NetConnection from another Windows machine or nc -zv from Linux). If authentication fails, verify the username format (domain accounts often need DOMAIN\user), check that the account is allowed to log on remotely, and confirm the transport matches your setup (NTLM, Basic, or Kerberos). If commands run but return errors, check whether the command needs a full path or shell features; switch between win_command and win_shell accordingly. If certificate validation errors appear, either install a trusted certificate on the target or, for lab only, set ansible_winrm_server_cert_validation=ignore. If the error mentions pywinrm, install it with pip and re-run. As a last resort, restart the WinRM service on the target; this does not delete data but briefly interrupts remote management.

Frequently Asked Questions

Can I run Ansible from a Windows machine?

No. Ansible control nodes are supported on Linux and macOS only. You can run Ansible from WSL on Windows, but native Windows is not supported as a control node.

What is the difference between win_command and win_shell?

win_command runs an executable directly without a shell, so pipes and redirection do not work. win_shell runs the command through PowerShell, enabling shell features.

Do I need to open port 5985 or 5986?

Yes. WinRM listens on 5985 for HTTP and 5986 for HTTPS by default. The firewall must allow the port you use from the control node.

Can I use Ansible with domain accounts?

Yes. Use the Kerberos transport and configure Kerberos on the control node. NTLM also works for domain accounts but Kerberos is preferred in domain environments.

Is it safe to ignore certificate validation?

Only in a lab with self-signed certificates. In production, use a trusted certificate and leave validation enabled.

Why does my command work in PowerShell but fail in Ansible?

Likely because win_command does not use a shell. Use win_shell for PowerShell syntax, or call the executable with its full path.