简体   繁体   English

在 Windows 上安装 Ansible Python 包

[英]Installing Ansible Python package on Windows

I'm struggling to install Ansible Python package on my Windows 10 machine.我正在努力在我的 Windows 10 机器上安装 Ansible Python 包。

I don't need Ansible to run on my machine, this is purely for development purpose on my Windows host.我不需要 Ansible 在我的机器上运行,这纯粹是为了在我的 Windows 主机上进行开发。 All commands will later be issued on a Linux machine.所有命令稍后将在 Linux 机器上发出。

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\\命令 "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" 在 C:\\Users\\evaldas.buinauskas\\AppData\\Local\\Temp\\pip-install-hpay_le9\\ansible\\ 中失败,错误代码为 1

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错误:无法复制“lib\\ansible\\module_utils\\ansible_release.py”:不存在或不是常规文件

This GitHub issue says that installing should be possible, not running it.这个 GitHub 问题说安装应该是可能的,而不是运行它。 That's basically all I really need.这基本上就是我真正需要的。

I tried running CMD/PowerShell/Cygwin as Administrator, didn't help.我尝试以管理员身份运行 CMD/PowerShell/Cygwin,没有帮助。

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此外,还有一个答案告诉您如何在 Windows 上安装它: 如何克服 - pip install ansible on windows failed with filename or extension too long on windows

But I don't really understand how to get a wheel file for Ansible package.但我真的不明白如何为 Ansible 包获取轮文件。

Installing Ansible on Windows is cumbersome.在 Windows 上安装 Ansible 很麻烦。 My advice is not a direct solution on how to install Ansible on Windows, but rather a workaround.我的建议不是如何在 Windows 上安装 Ansible 的直接解决方案,而是一种解决方法。

I use a Docker container with Ansible for developing playbooks on my Windows machine.我使用带有 Ansible 的 Docker 容器在我的 Windows 机器上开发剧本。 You'd need Docker for Windows on your machine.你需要在你的机器上安装Docker for Windows

Here's the Dockerfile:这是 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.使用docker build命令构建 docker 容器。 Afterwards you can create a small bash script that executes the docker run command and mounts your current directory into the container.之后你可以创建一个小的 bash 脚本来执行docker run命令并将你的当前目录挂载到容器中。 You may call it ansible-playbook.sh :你可以称之为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 .现在,您将能够在GIT BASH 中使用./ansible-playbook.sh <your playbook>启动 Ansible playbook。 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.如果你想在 PowerShell 中运行它,你可能需要删除winpty命令,但我还没有在 PS 中测试它。

It is not the finest solution but it gets the work done.这不是最好的解决方案,但可以完成工作。 Hope it helps you, too.也希望能帮到你。

I've managed to install ansible on Windows 10 with following steps ( ran in powershell ):我已经通过以下步骤(在 powershell 中运行)设法在 Windows 10 上安装了 ansible:

  • Clone ansible repository, eg to ansible folder克隆 ansible 存储库,例如到ansible文件夹
  • pip3 install -e .\\ansible\\ 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 . Ansible 将在某种程度上无法用于开发,因为它使用了一些仅限 Unix 的模块,如grppwd For example, you won't be able to run unit tests (eg module_utils/basic.py imports grp and pwd).例如,您将无法运行单元测试(例如module_utils/basic.py导入 grp 和 pwd)。 Downloading grp.py to site-packages folder won't help.将 grp.py 下载到site-packages文件夹无济于事。

To have a smoother experience, I recommend installing WSL (Windows Subsystem for Linux) plus install python with pip and just run pip install ansible .为了获得更流畅的体验,我建议安装 WSL(Linux 的 Windows 子系统)并使用 pip 安装 python,然后运行pip install ansible Here's how you can use WSL for development in Visual Studio Code 以下是在 Visual Studio Code 中使用 WSL 进行开发的方法

Another approach is to install Ubuntu 18.04 from the store.另一种方法是从商店安装 Ubuntu 18.04。 Or even newer when available.或者在可用时更新。 Then perform all changes regarding Ansible in the Linux environment.然后在 Linux 环境中执行有关 Ansible 的所有更改。

Of course, this will force you to do some tricks if you need to use Ansible as a controller.当然,如果您需要使用 Ansible 作为控制器,这将迫使您做一些技巧。

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).我有一个类似的要求 - 将 Ansible 安装为合法的 Python 库,以便我可以引用它并在我的 Windows 开发环境中浏览源代码(而不是在 Windows 上运行Ansible)。 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 (eg https://github.com/ansible/ansible/archive/v2.9.2.zip ).从 github 下载最新的zip发行版本(例如https://github.com/ansible/ansible/archive/v2.9.2.zip )。 Note, must be zip version, because the tar.gz has symbolic links in it).注意,必须是 zip 版本,因为 tar.gz 中有符号链接)。
  2. Unzip to (eg) C:\\Temp\\ansible-2.9.2解压到(例如) C:\\Temp\\ansible-2.9.2
  3. Remove the symlink dependency by changing setup.py to return immediately from _maintain_symlinks :通过将setup.py更改为立即从_maintain_symlinks返回来删除符号链接依赖项:
     def _maintain_symlinks(symlink_type, base_path): return
  4. cd C:\\Temp\\ansible-2.9.2 cd C:\\Temp\\ansible-2.9.2
  5. c:\\Python38\\python.exe setup.py install

Thanks @Kevin C for the tip, trying out now ...感谢@Kevin C的提示,现在就试试……

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.正如您所写,“Windows 下的 pip”解决方案正在运行,但是……在某种程度上没有用,因为 ansible 确实需要在 linux 环境中执行才能完全按设计工作。 To try out install python 3.8 via MS Store eg and pip install the cloned ansible git repo from here尝试通过 MS Store 安装 python 3.8 eg 和 pip 从这里安装克隆的 ansible git repo

As you suggested using the Windows Subsystem for Linux (WSL) will work.正如您所建议的,使用适用于 Linux 的 Windows 子系统 (WSL) 会起作用。

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.可以在适用于 linux 的 Win10 + Windows 子系统上而不是在 docker 容器中安装 ansible,您必须自己决定它是否更适合您的目的。 The setup time is also very reasonable, and eg integration into Visual Studio code works in order to test ansible development locally (ansible-lint or ansible-playbook --syntax-check eg).设置时间也非常合理,例如集成到 Visual Studio 代码中是为了在本地测试 ansible 开发(例如 ansible-lint 或 ansible-playbook --syntax-check)。 Also, other commands, like ansible-galaxy and ansible-inventory commands work as expected, basic tests done...此外,其他命令,如 ansible-galaxy 和 ansible-inventory 命令按预期工作,完成基本测试......

Walkthrough演练

You might want to make sure you are running the most recent Windows 10 release;您可能希望确保您运行的是最新的 Windows 10 版本; At the time of writing I am using Windows 10 pro, Version 2004.WSL2 is available for this Windows Version.在撰写本文时,我使用的是 Windows 10 专业版,版本2004。WSL2可用于此 Windows 版本。

WSL2 installation WSL2 安装

via Admin powershell as per MS instructions : 根据 MS 说明通过 Admin powershell :

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

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

wsl --set-default-version 2 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 内核映像,因此如果您看到此消息,请按照此处显示wsl2kernel 更新指南进行操作

WSL 2 requires an update to its kernel component. WSL 2 需要更新其内核组件。 For information please visit https://aka.ms/wsl2kernel有关信息,请访问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 .在撰写本文时, 无法直接通过 powershell 从 Microsoft 商店安装任何 linux“发行版”。

[EDIT] @2021-02: previously, https://aka.ms/wslstore worked... not any more. [编辑] @2021-02:以前, https ://aka.ms/wslstore 工作......不再有。 BUT you can now still either use the store and search or follow the instructions for manual installation eg:但是您现在仍然可以使用商店和搜索或按照手动安装的说明进行操作,例如:

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

Add-AppxPackage .\\Debian.appx添加-AppxPackage .\\Debian.appx

...as microsoft documents here . ...作为微软文档在这里

Finally: install ansible最后:安装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.因此,在可能需要 5 分钟(下载时间可能大不相同)的初始配置之后,您已经拥有了 Windows 下的 Linux shell。 As with every usual Ubuntu or Debian, now you can install ansible easily with与每个常用的 Ubuntu 或 Debian 一样,现在您可以使用以下命令轻松安装 ansible

sudo apt install ansible sudo apt 安装 ansible

which should install a recent version of ansible (ansible version is 2.9.12 @2020-08).它应该安装最新版本的 ansible(ansible 版本是 2.9.12 @2020-08)。

This should be it.应该是这样。

Optionally if you also want to use Visual Studio Code: (可选)如果您还想使用 Visual Studio Code:

Visual Studio code configuration Visual Studio 代码配置

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).要将此设置与 VS Code 一起使用,请在此处下载并安装(...在 Windows 10 中,而不是在 Debian / WSL 中)(...您也可以使用 Chocolatey 进行该安装,但 VS Code 通常会在默认情况下尽可能地自我更新) .

As per the originally linked article VS code & WSL "Configure VS Code to use Bash"根据最初链接的文章VS 代码和 WSL “配置 VS 代码以使用 Bash”

2 refinements there: 2处改进:

  • the settings are nowadays (Win 10 2004, 64 bit, Date 2020-08) found under:现在的设置(Win 10 2004,64 位,日期 2020-08)位于:

File -> Preferences -> Settings or reachable directly via the keys [CTRL] + [,]文件 -> 首选项 -> 设置或直接通过 [CTRL] + [,] 键访问

  • The complete path to start bash has also changed also a little bit:启动 bash 的完整路径也发生了一些变化:

"terminal.integrated.shell.windows": "C:\\Windows\\System32\\bash.exe" "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...尽管如此,您必须按照原始VS 代码和 WSL文章让自己熟悉WSL中安装的内容 - 您需要找到您的 ansible 项目位置才能继续...

Also you may want to install git as mentioned there.此外,您可能想要安装那里提到的 git。 Additionally the VS Code Extensions that the ansible project recommends might come in handy.此外,ansible 项目推荐的 VS Code 扩展可能会派上用场。 For a feature presentation of the basic "ansible" extension see here .有关基本“ansible”扩展的功能介绍,请参见此处

Happy ansible coding & testing (with VS Code)!愉快的 ansible 编码和测试(使用 VS Code)!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM