Installing Ansible Python package on Windows

I had a similar requirement - install Ansible as a legitimate Python library so I could reference it and browse the source in my Windows dev environment (not to run Ansible on Windows). I made it partially install (some failures, but not enough to halt the install) by doing the following:

  1. Download the latest zip release version from github (e.g. https://github.com/ansible/ansible/archive/v2.9.2.zip). Note, must be zip version, because the tar.gz has symbolic links in it).
  2. Unzip to (e.g.) C:\Temp\ansible-2.9.2
  3. Remove the symlink dependency by changing setup.py to return immediately from _maintain_symlinks:
    def _maintain_symlinks(symlink_type, base_path):
        return
    
  4. cd C:\Temp\ansible-2.9.2
  5. c:\Python38\python.exe setup.py install

Another approach is to install Ubuntu 18.04 from the store. Or even newer when available. Then perform all changes regarding Ansible in the Linux environment.

Of course, this will force you to do some tricks if you need to use Ansible as a controller.


I've managed to install ansible on Windows 10 with following steps (ran in powershell):

  • Clone ansible repository, e.g. to ansible folder
  • pip3 install -e .\ansible\

You may also need to make a symbolic link, however, shouldn't be neccessary:

New-Item -ItemType SymbolicLink -Name ansible_release.py -Target .\lib\ansible\release.py

Ansible will be somewhat unusable for development, because it's using some Unix-only modules like grp or pwd. For example, you won't be able to run unit tests (e.g. module_utils/basic.py imports grp and pwd). Downloading grp.py to site-packages folder won't help.

To have a smoother experience, I recommend installing WSL (Windows Subsystem for Linux) plus install python with pip and just run pip install ansible. Here's how you can use WSL for development in Visual Studio Code


Installing Ansible on Windows is cumbersome. My advice is not a direct solution on how to install Ansible on Windows, but rather a workaround.

I use a Docker container with Ansible for developing playbooks on my Windows machine. You'd need Docker for Windows on your machine.

Here's the Dockerfile:

FROM alpine:3.7

ENV ANSIBLE_VERSION=2.5.4

ENV BUILD_PACKAGES \
        bash \
        curl \
        tar \
        nano \
        openssh-client \
        sshpass \
        git \
        python \
        py-boto \
        py-dateutil \
        py-httplib2 \
        py-jinja2 \
        py-paramiko \
        py-pip \
        py-setuptools \
        py-yaml \
        ca-certificates

RUN apk --update add --virtual build-dependencies \
        gcc \
        musl-dev \
        libffi-dev \
        openssl-dev \
        python-dev && \
    set -x && \
    apk update && apk upgrade && \
    apk add --no-cache ${BUILD_PACKAGES} && \
    pip install --upgrade pip && \
    pip install python-keyczar docker-py boto3 botocore && \
    apk del build-dependencies && \
    rm -rf /var/cache/apk/* && \
    mkdir -p /etc/ansible/ /ansible && \
    echo "[local]" >> /etc/ansible/hosts && \
    echo "localhost" >> /etc/ansible/hosts && \
    curl -fsSL https://releases.ansible.com/ansible/ansible-${ANSIBLE_VERSION}.tar.gz -o ansible.tar.gz && \
    tar -xzf ansible.tar.gz -C /ansible --strip-components 1 && \
    rm -fr ansible.tar.gz /ansible/docs /ansible/examples /ansible/packaging

ENV ANSIBLE_GATHERING=smart \
    ANSIBLE_HOST_KEY_CHECKING=false \
    ANSIBLE_RETRY_FILES_ENABLED=false \
    ANSIBLE_ROLES_PATH=/ansible/playbooks/roles \
    ANSIBLE_SSH_PIPELINING=True \
    PYTHONPATH=/ansible/lib \
    PATH=/ansible/bin:$PATH \
    ANSIBLE_LIBRARY=/ansible/library \
    EDITOR=nano

WORKDIR /ansible/playbooks

ENTRYPOINT ["ansible-playbook"]

Build the docker container with the docker build command. Afterwards you can create a small bash script that executes the docker run command and mounts your current directory into the container. You may call it ansible-playbook.sh:

winpty docker run --rm -it -v /$(pwd):/ansible/playbooks <name of your container> $@

Now you will be able to launch Ansible playbook with ./ansible-playbook.sh <your playbook> in GIT BASH. If you'd like to run this in PowerShell you would probably need to remove the winpty command, but I did not test this in PS yet.

It is not the finest solution but it gets the work done. Hope it helps you, too.