尝试使用ansible在virtualenv内部安装时,权限被拒绝

尝试使用ansible在virtualenv内部安装时,权限被拒绝,ansible,ansible-playbook,Ansible,Ansible Playbook,我的ansible剧本里有这个剧本 - name: Setup virtualenv pip: requirements="/vagrant/webapp/requirements/{{ requirements_filename }}" virtualenv="$HOME/.envs/{{ project_name }}" 但是,我收到一个“权限被拒绝”错误。似乎ansible正在尝试将包安装到系统路径中,而不是安装到virtualenv中 以下是完整的回溯: TAS

我的ansible剧本里有这个剧本

- name: Setup virtualenv
  pip: 
    requirements="/vagrant/webapp/requirements/{{ requirements_filename }}" 
    virtualenv="$HOME/.envs/{{ project_name }}"
但是,我收到一个“权限被拒绝”错误。似乎ansible正在尝试将包安装到系统路径中,而不是安装到virtualenv中

以下是完整的回溯:

TASK: [deploy | Setup virtualenv] ********************************************* 
failed: [default] => {"cmd": "/usr/bin/pip install -r /vagrant/webapp/requirements/dev.txt", "failed": true}
msg: stdout: New python executable in /home/vagrant/.envs/venv/bin/python
Installing setuptools, pip...done.
Downloading/unpacking django>=1.8 (from -r /vagrant/webapp/requirements/requirements.txt (line 3))
Requirement already satisfied (use --upgrade to upgrade): wsgiref>=0.1.2 in /usr/lib/python2.7 (from -r /vagrant/webapp/requirements/requirements.txt (line 4))
Downloading/unpacking django-allauth>=0.19.1 (from -r /vagrant/webapp/requirements/requirements.txt (line 5))
  Running setup.py (path:/tmp/pip_build_vagrant/django-allauth/setup.py) egg_info for package django-allauth

Downloading/unpacking psycopg2>=2.6 (from -r /vagrant/webapp/requirements/requirements.txt (line 6))
  Running setup.py (path:/tmp/pip_build_vagrant/psycopg2/setup.py) egg_info for package psycopg2

Downloading/unpacking django-currencies>=0.3.3 (from -r /vagrant/webapp/requirements/requirements.txt (line 7))
  Downloading django-currencies-0.3.3.tar.gz
  Running setup.py (path:/tmp/pip_build_vagrant/django-currencies/setup.py) egg_info for package django-currencies

Downloading/unpacking django-countries>=3.2 (from -r /vagrant/webapp/requirements/requirements.txt (line 8))
  Running setup.py (path:/tmp/pip_build_vagrant/django-countries/setup.py) egg_info for package django-countries

Downloading/unpacking stripe>=1.22.1 (from -r /vagrant/webapp/requirements/requirements.txt (line 9))
  Running setup.py (path:/tmp/pip_build_vagrant/stripe/setup.py) egg_info for package stripe

Downloading/unpacking pillow>=2.8.0 (from -r /vagrant/webapp/requirements/requirements.txt (line 10))
  Running setup.py (path:/tmp/pip_build_vagrant/pillow/setup.py) egg_info for package pillow
    Single threaded build, not installing mp_compile: 1 processes

    warning: no files found matching 'LICENSE' under directory 'docs'
Downloading/unpacking django-compressor>=1.4 (from -r /vagrant/webapp/requirements/requirements.txt (line 11))
Downloading/unpacking djangorestframework>=3.1.1 (from -r /vagrant/webapp/requirements/requirements.txt (line 12))
Downloading/unpacking django-filter>=0.9.2 (from -r /vagrant/webapp/requirements/requirements.txt (line 13))
  Downloading django_filter-0.9.2-py2.py3-none-any.whl
Obtaining formtools from git+https://github.com/django/django-formtools.git#egg=formtools (from -r /vagrant/webapp/requirements/requirements.txt (line 14))
  Cloning https://github.com/django/django-formtools.git to ./src/formtools
  Running setup.py (path:/tmp/src/formtools/setup.py) egg_info for package formtools

  Installing extra requirements: 'egg'
Downloading/unpacking django-widget-tweaks>=1.3 (from -r /vagrant/webapp/requirements/requirements.txt (line 15))
  Downloading django-widget-tweaks-1.3.tar.gz
  Running setup.py (path:/tmp/pip_build_vagrant/django-widget-tweaks/setup.py) egg_info for package django-widget-tweaks

Downloading/unpacking django-braces>=1.4.0 (from -r /vagrant/webapp/requirements/requirements.txt (line 16))
  Downloading django_braces-1.4.0-py2.py3-none-any.whl
Downloading/unpacking django-debug-toolbar>=1.3.0 (from -r /vagrant/webapp/requirements/dev.txt (line 4))
Downloading/unpacking python-openid>=2.2.5 (from django-allauth>=0.19.1->-r /vagrant/webapp/requirements/requirements.txt (line 5))
  Running setup.py (path:/tmp/pip_build_vagrant/python-openid/setup.py) egg_info for package python-openid

    warning: no files found matching 'CHANGELOG'
    warning: no files found matching '*.css' under directory 'doc'
    warning: no files found matching '*.html' under directory 'doc'
Downloading/unpacking requests-oauthlib>=0.3.0 (from django-allauth>=0.19.1->-r /vagrant/webapp/requirements/requirements.txt (line 5))
  Downloading requests_oauthlib-0.4.2-py2.py3-none-any.whl
Requirement already satisfied (use --upgrade to upgrade): requests>=1.0.3 in /usr/lib/python2.7/dist-packages (from django-allauth>=0.19.1->-r /vagrant/webapp/requirements/requirements.txt (line 5))
Downloading/unpacking django-appconf>=0.4 (from django-compressor>=1.4->-r /vagrant/webapp/requirements/requirements.txt (line 11))
  Downloading django_appconf-1.0.1-py2.py3-none-any.whl
Requirement already satisfied (use --upgrade to upgrade): six in /usr/local/lib/python2.7/dist-packages (from django-braces>=1.4.0->-r /vagrant/webapp/requirements/requirements.txt (line 16))
Downloading/unpacking sqlparse (from django-debug-toolbar>=1.3.0->-r /vagrant/webapp/requirements/dev.txt (line 4))
  Running setup.py (path:/tmp/pip_build_vagrant/sqlparse/setup.py) egg_info for package sqlparse

Downloading/unpacking oauthlib>=0.6.2 (from requests-oauthlib>=0.3.0->django-allauth>=0.19.1->-r /vagrant/webapp/requirements/requirements.txt (line 5))
  Running setup.py (path:/tmp/pip_build_vagrant/oauthlib/setup.py) egg_info for package oauthlib

Installing collected packages: django, django-allauth, psycopg2, django-currencies, django-countries, stripe, pillow, django-compressor, djangorestframework, django-filter, formtools, django-widget-tweaks, django-braces, django-debug-toolbar, python-openid, requests-oauthlib, django-appconf, sqlparse, oauthlib
Cleaning up...
Exception:
Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/pip/basecommand.py", line 122, in main
    status = self.run(options, args)
  File "/usr/lib/python2.7/dist-packages/pip/commands/install.py", line 283, in run
    requirement_set.install(install_options, global_options, root=options.root_path)
  File "/usr/lib/python2.7/dist-packages/pip/req.py", line 1435, in install
    requirement.install(install_options, global_options, *args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/pip/req.py", line 671, in install
    self.move_wheel_files(self.source_dir, root=root)
  File "/usr/lib/python2.7/dist-packages/pip/req.py", line 901, in move_wheel_files
    pycompile=self.pycompile,
  File "/usr/lib/python2.7/dist-packages/pip/wheel.py", line 206, in move_wheel_files
    clobber(source, lib_dir, True)
  File "/usr/lib/python2.7/dist-packages/pip/wheel.py", line 193, in clobber
    os.makedirs(destsubdir)
  File "/usr/lib/python2.7/os.py", line 157, in makedirs
    mkdir(name, mode)
OSError: [Errno 13] Permission denied: '/usr/local/lib/python2.7/dist-packages/Django-1.8.dist-info'

Storing debug log for failure in /home/vagrant/.pip/pip.log


FATAL: all hosts have already failed -- aborting

同时尝试指定
pip
可执行路径:

executable="$HOME/.envs/{{ project_name }}/bin/pip"

没有搜索TBH的确切原因,但也必须这样做才能将内容安装到虚拟环境中。它似乎试图使用global
pip
安装到虚拟环境中。也许在某些情况下这是可能的,不是我的情况。

看起来它使用的是来自virtualenv的
/usr/bin/pip
,而不是
pip
。不知道为什么会发生这种情况-Ansible中的一个bug?您能否手动检查virtualenv是否具有
pip
命令?有一件事我可以从脑海中想出来,那就是为root(sudo)创建pip,您尝试以普通用户的身份运行该命令,但它没有对virtualenv'ed pip的读取权限

您可能可以像这样手动使用pip来解决问题

- name: Create virtualenv for app using chosen Python
  shell: virtualenv --python=python3.4 {{deploy_location}}/venv
  sudo: yes

- name: Install Python packages
  sudo: yes
  command: {{deploy_location}}/venv/bin/pip -r {{deploy_location}}/requirements.txt 

我通过显式地设置路径而不是使用
$HOME
变量解决了这个问题

- name: Setup virtualenv
  pip:
    virtualenv="/home/vagrant/.envs/{{ project_name }}"
    requirements="/vagrant/webapp/requirements/{{ requirements_filename }}"

pip模块似乎正在使用系统Python,因此它将尝试在
站点软件包的预期系统路径上安装软件包
,并且由于您不是以root用户身份或使用sudo运行它,因此无法写入“/usr/local/lib/python2.7/…”

我假设您想在virtualenv下安装pip包,对吗

如果是这样,请注意Ansible模块在默认情况下始终使用系统默认的Python解释器,除非您设置了
Ansible\u Python\u解释器
。 有关如何执行此操作的示例,请参见

链接到关于这个主题的简短讨论,以及为什么Ansible人不想改变
#/usr/bin/python
以获得更为虚拟友好的
!/可以找到usr/bin/env python


请注意,如果您从本地virtualenv运行Ansible并尝试使用say
ec2
模块,而没有在系统级别安装
boto
(但在virtualenv级别安装)。

对于较旧的Python,在过去,如果系统文件夹包含所述包,并且virtualenv中没有缺少任何站点包,则可能会发生这种情况。虽然
--没有网站包
应该是默认的,但我建议您在创建virtualenv之前在剧本中更新pip/virtualenv<代码>命令:sudo-pip-install-U-pip-virtualenv应该可以,我想。@mikkoohtama在游戏开始前尝试过升级pip和virtualenv,但没有用。谢谢你的建议。你的Ansible是最新的吗?@Mikkoohtama,是的。我正在使用Ansible
1.9.0.1
。这假设在播放之前已经存在virtualenv,对吗?@RodXavier我认为不是,应该可以一步完成。尝试了你的答案,但它给了我以下错误
msg:找不到所需的可执行文件$HOME/.envs/venv/bin/pip
@RodXavier刚刚用Ansible
1.9.0.1
测试过,从
requirements.txt
创建虚拟环境并安装软件包,一步到位。除了“检查你的路径”之外,我没有主意了。如果你想使用HOME-env变量,你仍然可以使用
{{lookup('env','HOME')}
。只有在作为参数传递给shell模块的字符串中使用$HOME这样的语法时,才有意义。pip模块可能不会在后台使用shell命令来完成其工作;因此$HOME之类的变量不会被扩展。警告:
{{lookup('env','HOME')}
将被转换为运行playbook的主机的HOME环境变量;它可能不会像您预期的那样转换为远程计算机的主环境变量。尝试使用ansible 1.9.0.1从mac到ubuntu服务器运行远程playbook和
-debug:msg=“{{lookup('env','HOME')}}”
。如果需要远程计算机的HOME变量,请使用
{{ansible_env.HOME}