为什么我用setup.py安装ansible找不到?

Why can't I find ansible when I install it using setup.py?

因为我在使用 Ansible 时遇到了一些问题(我在 mac),今天似乎已在最新的开发版本中修复了我通过 pip(sudo pip uninstall ansible)卸载了 ansible 并重新安装了最新的使用经典 setup.py 方法从 github 回购开发版本,似乎成功结束(full output here

然后我尝试使用它:

$ ansible --version
-bash: ansible: command not found
$ which ansible
$

我检查了它的安装位置。从我上面链接的完整输出中,我发现它安装在 /usr/local/lib/python2.7/site-packages 中,而且我确实在那里找到了一个鸡蛋:

$ ls -l /usr/local/lib/python2.7/site-packages | grep ansible
drwxr-xr-x    4 root    admin     136 Aug 22 16:33 ansible-2.4.0-py2.7.egg

当我启动 Python 并检查 site-packages 文件夹时,我发现了一个不同的文件夹:

>>> import site; print site.getsitepackages()[0]
/usr/local/Cellar/python/2.7.13_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages

但这是指向同一文件夹的符号链接:

$ ls -l /usr/local/Cellar/python/2.7.13_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
lrwxr-xr-x  1 hielke  admin  54 Aug 13 22:36 /usr/local/Cellar/python/2.7.13_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages -> ../../../../../../../../../lib/python2.7/site-packages

所以我猜问题是没有为 /usr/local/bin/ 中的 ansible 包创建符号链接。但我不确定如何创建这样的符号链接以及为什么它不会首先出现。

有人知道我该如何从这里前进吗?欢迎所有提示!

我建议卸载 Ansible 并根据 the method suggested in the Ansible docs 使用 pip 重新安装它:

Or if you are looking for the latest development version:

pip install git+https://github.com/ansible/ansible.git@devel

If you are installing on OS X Mavericks, you may encounter some noise from your compiler. A workaround is to do the following:

$ sudo CFLAGS=-Qunused-arguments CPPFLAGS=-Qunused-arguments pip install ansible

Readers that use virtualenv can also install Ansible under virtualenv, though we’d recommend to not worry about it and just install Ansible globally. Do not use easy_install to install ansible directly.

当您从 shell 调用 ansible 时,bash 将在您的 $PATH 中搜索名为 ansible 的可执行文件。这可能不是唯一的问题,但这是您所看到的错误的直接原因。 .egg 文件本身不是可执行文件,它只是一个用于分发代码的文件。

如果 ansible 已正确安装,您应该能够使用 locate 或 OSX Finder GUI 找到它。名称应完全匹配,没有文件扩展名。您可能还会在找到 ansible 可执行文件的同一位置找到 ansible-connectionansible-console 等。如果你找到了,太好了!测试一下并将该目录添加到终端中的 $PATH,如下所示:

export PATH=$PATH:/path/to/ansible

其中 /path/to/ansible 是您找到可执行文件的目录。 $PATH 变量的这种变化是暂时的,当您关闭 shell 时就会消失。如果您现在可以从 bash 运行 ansible,那么您可以通过将该导出添加到 $HOME/.bash_profile 文件的末尾,或者通过在 /etc/paths.d 中添加规则来使更改永久化](苹果推荐)。如果您不熟悉它们,请参阅更多关于具体如何操作的信息 here

现在,如果这不是问题所在并且您找不到 ansible 可执行文件,那么安装本身就是您的问题。您也可以尝试使用虚拟环境(如果您安装了它)来确保您从 github 中提取的版本没有损坏:

git clone https://github.com/ansible/ansible.git
cd ansible
virtualenv venv
source venv/bin/activate
pip install .
which ansible

在撰写本文时,以上内容为我提供了一个有效的 ansible 安装。

找到 ansible 在你的 Mac 上的位置。大多数时候 /Users/<yourusername>/Library/Python/3.7/bin/Users/<yourusername>/Library/Python/2.7/bin。那么...

export PATH=$PATH:/Users/<yourusername>/Library/Python/3.7/bin

您可以将其存储在您的 .bashrc 文件中。

嗯,我想你只需要创建一个软 link

ln -s /Users/${yourname}/Library/Python/${python version}/bin/ansible /usr/local/bin/ansible

我在安装ansible时遇到了同样的问题。 运行 下面的命令可以解决这个问题。但是如果我们想使用 ansible,我们必须在每次打开 bash 会话时激活。

$ python -m virtualenv ansible 
$ source ansible/bin/activate
$ pip install ansible 

我正在使用 zsh,所以在我的 /Users/arojas/.zshrc 我添加了这一行,这是我的 Ansible 由 Python

安装的地方
export PATH="$PATH:$HOME/Library/Python/3.7/bin"

我遇到了同样的问题,安装后使用 pip3 install ansible 现在一切正常。

对于使用 Windows 10 Ubuntu 终端的用户,运行 此命令应该可以解决问题:

export PATH=$PATH:~/.local/bin
      system/instance used: ec2 RH8, as root
      # pip3 install ansible           (not recommended - should be by a user) 
      # ansible --version              ( not found - wtf?!)
      # yum install mlocate
      # update
      # locate ansible                 (long output; scroll to where you input command)
      # export PATH=$PATH:/usr/local/bin     
      # ansible --version               (success, Yes!)
  system/instance used: ec2 RH8, as root
  # pip3 install ansible           (not recommended - should be by a user) 
  # ansible --version              ( not found - :( )
  # yum install mlocate
  # updatedb (updatedb creates or updates a database used by locate)
  # locate ansible (TL;DR)
  # export PATH=$PATH:/usr/local/bin (Add this line to .bashrc)
  # source .bashrc (To reflect the changes in the bash)  
  # ansible --version               (success, Yes!)
pip3 install ansible --user

这安装在 ~/.local 中。只需将其包含在 PATH 中,它就会起作用 示例:export PATH="$PATH:~/.local/bin"