Installing Ansible Python package on Windows

23,714

Solution 1

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.

Solution 2

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

Solution 3

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

Solution 4

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.

Solution 5

Thanks @Kevin C for the tip, trying out now ...

As you wrote, the "pip under windows" solution is working BUT ... useless to some degree, because ansible really needs to be executed in a linux environment to fully work as designed. To try out install python 3.8 via MS Store e.g. and pip install the cloned ansible git repo from here

As you suggested using the Windows Subsystem for Linux (WSL) will work.

See below my

  • Summary and
  • Walkthrough

Summary

It is possible to install ansible on Win10 + Windows Subsystem for linux rather than in a docker container, you have to decide youself if it fits your purpose better or worse. The setup time is also very reasonable, and e.g. integration into Visual Studio code works in order to test ansible development locally (ansible-lint or ansible-playbook --syntax-check e.g.). Also, other commands, like ansible-galaxy and ansible-inventory commands work as expected, basic tests done...

Walkthrough

You might want to make sure you are running the most recent Windows 10 release; At the time of writing I am using Windows 10 pro, Version 2004. WSL2 is available for this Windows Version.

WSL2 installation

via Admin powershell as per MS instructions:

dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart

dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

wsl --set-default-version 2

you might still have to update the wsl kernel image if the last command tells you to, so follow the wsl2kernel update guide that is shown there if you see this message:

WSL 2 requires an update to its kernel component. For information please visit https://aka.ms/wsl2kernel

... after installing that, the above message should not be shown any more.

At the time of writing installing any linux "distribution" from the Microsoft store directly via powershell is not possible .

[EDIT] @2021-02: previously, https://aka.ms/wslstore worked... not any more. BUT you can now still either use the store and search or follow the instructions for manual installation e.g.:

Invoke-WebRequest -Uri https://aka.ms/wsl-debian-gnulinux -OutFile Debian.appx -UseBasicParsing

Add-AppxPackage .\Debian.appx

...as microsoft documents here.

Finally: install ansible

So after initial configuration which takes maybe 5 minutes (download times may broadly differ), there you already have a Linux shell under Windows available. As with every usual Ubuntu or Debian, now you can install ansible easily with

sudo apt install ansible

which should install a recent version of ansible (ansible version is 2.9.12 @2020-08).

This should be it.

Optionally if you also want to use Visual Studio Code:

Visual Studio code configuration

To use this setup with VS Code, download here and install (... in Windows 10, not in Debian / WSL) (... you could also use chocolatey for that installation, but VS code usually updates itself whenever possible per default).

As per the originally linked article VS code & WSL "Configure VS Code to use Bash"

2 refinements there:

  • the settings are nowadays (Win 10 2004, 64 bit, Date 2020-08) found under:

File -> Preferences -> Settings or reachable directly via the keys [CTRL] + [,]

  • The complete path to start bash has also changed also a little bit:

"terminal.integrated.shell.windows": "C:\Windows\System32\bash.exe"

Still, you have to make yourself familiar as per the original VS code & WSL article where what is mounted where in WSL - you need to find your ansible project location in order to proceed...

Also you may want to install git as mentioned there. Additionally the VS Code Extensions that the ansible project recommends might come in handy. For a feature presentation of the basic "ansible" extension see here.

Happy ansible coding & testing (with VS Code)!

Share:
23,714

Related videos on Youtube

Evaldas Buinauskas
Author by

Evaldas Buinauskas

Updated on August 01, 2022

Comments

  • Evaldas Buinauskas
    Evaldas Buinauskas almost 2 years

    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.

  • Evaldas Buinauskas
    Evaldas Buinauskas almost 6 years
    Yep, I also thought about using Docker and mounting my current directory there. Thanks!
  • aggregate1166877
    aggregate1166877 about 5 years
    This works in cygwin too, so long as your python install path does not have spaces.
  • guz4217
    guz4217 over 3 years
    Admittedly, this solution after rereading & refining does actually N O T use python / pip to get you to a working ansible to use in Windows (Teminal / VS Code) . So, might be considered a wrong answer because it only fullfills the "ansible under windows" but not "ansible under windows via python / pip" - also the docker solution uses a "side tracked answer". that solution basically puts together a virtual environment based on the git repository in the container then to be used. "Pure python + pip" based solutions however seem not to be sufficient to use ansible "natively" under windows.
  • FiftiN
    FiftiN over 3 years
    How to use ansible after that?
  • dseeley
    dseeley over 3 years
    This is for source browsing/ indexing only: "(not to run Ansible on Windows)". Intellij (for example) will index it as a python library, so you can jump to definitions etc.