Python – Installing Ansible Python package on Windows

ansiblepippython

I'm struggling to install Ansible Python package on my Windows 10 machine.

I don't need Ansible to run on my machine, this is purely for development purpose on my Windows host. All commands will later be issued on a Linux machine.

After running:

pip install ansible

I get the following exception:

Command "c:\users\evaldas.buinauskas\appdata\local\programs\python\python37-32\python.exe -u -c "import setuptools, tokenize;__file__='C:\Users\evaldas.buinauskas\AppData\Local\Temp\pip-install-hpay_le9\ansible\setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install –record C:\Users\evaldas.buinauskas\AppData\Local\Temp\pip-record-dvfgngpp\install-record.txt –single-version-externally-managed –compile" failed with error code 1 in C:\Users\evaldas.buinauskas\AppData\Local\Temp\pip-install-hpay_le9\ansible\

Also there's a repetetive exception that I think is the root cause:

error: can't copy 'lib\ansible\module_utils\ansible_release.py': doesn't exist or not a regular file

This GitHub issue says that installing should be possible, not running it. That's basically all I really need.

I tried running CMD/PowerShell/Cygwin as Administrator, didn't help.

Also, there's an answer that tells how to install it on Windows: How to overcome – pip install ansible on windows failing with filename or extension too long on windows

But I don't really understand how to get a wheel file for Ansible package.

Best Answer

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.