Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Installing Ansible Python package on Windows

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.

like image 372
Evaldas Buinauskas Avatar asked Jul 04 '18 06:07

Evaldas Buinauskas


People also ask

Is Ansible a python package?

The ansible-core code runs Python 3 (for specific versions check Control Node Requirements Contributors to ansible-core and to Ansible Collections should be aware of the tips in this document so that they can write code that will run on the same versions of Python as the rest of Ansible.

How do I install Ansible packages?

To begin, choose the package manager on your local computer. For instance, if you're going to write your Ansible instructions (a "playbook," as it's called in Ansible) on a laptop running Fedora, start with the dnf module. If you're writing on Elementary OS, use the apt module, and so on.

Does Ansible install Python?

Creating Ansible playbook to install python become is uses to get the privileges of the root user. When we need to install python, this must be specified in pre-task s section of the playbook.


4 Answers

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.

like image 181
fgk Avatar answered Oct 23 '22 16:10

fgk


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

like image 45
Kasia Gauza Avatar answered Oct 23 '22 18:10

Kasia Gauza


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
like image 4
dseeley Avatar answered Oct 23 '22 17:10

dseeley


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.

like image 3
realtebo Avatar answered Oct 23 '22 18:10

realtebo