Python Ansible 创建 virtualenv

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26402123/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:26:56  来源:igfitidea点击:

Ansible creating a virtualenv

pythonansible

提问by user204088

How do you create a virtualenv for a specific python version using ansible. Is there a command in the ansible standard library?

如何使用 ansible 为特定的 Python 版本创建 virtualenv。ansible 标准库中是否有命令?

I would like something like:

我想要类似的东西:

- virtualenv: dest=venv python_version:/usr/bin/python3

回答by Schnouki

You can do it with the pipmodule and a specific virtualenvbinary:

您可以使用pip模块和特定的virtualenv二进制文件来做到这一点:

- pip: virtualenv=/path/to/venv virtualenv_command=/path/to/virtualenv3 ...

回答by toast38coza

I have at times experienced some erratic behaviour with specifying virtualenv_command(e.g.: getting a python 2.7 executable in my virtualenv even though I specified to run the command with virtualenv-3.4.

我有时会在指定时遇到一些不稳定的行为virtualenv_command(例如:即使我指定使用virtualenv-3.4.

If you experience that problem, you cancreate the virtualenv manually with the commandmodule:

如果您遇到该问题,您可以使用该command模块手动创建 virtualenv :

- name: Manually create the initial virtualenv
  command:
    cmd: virtualenv /user/home/venvs/myenv -p python3.4
    creates: "/user/home/venvs/myenv"

(note: by specifying createsthis command will only run in the case that a virtualenv does not exist at /user/home/venvs/myenv).

(注意:通过指定creates此命令只会在 virtualenv 不存在的情况下运行/user/home/venvs/myenv)。

Then you can install your requirements as normal with the pipcommand:

然后您可以使用以下pip命令正常安装您的需求:

- name: Install requirements
  pip: 
    requirements=/my_app/requirements.txt 
    virtualenv=/user/home/venvs/myenv

Update

更新

I've found that specifying the virtualenv_python(available in Ansible 2.0+) also seems to work in the case stated above. For example:

我发现指定virtualenv_python(在 Ansible 2.0+ 中可用)似乎也适用于上述情况。例如:

- name: Install requirements
  pip: 
    requirements: /my_app/requirements.txt
    virtualenv: /user/home/venvs/myenv
    virtualenv_python: python3.4

(with this you shouldn't need to manually create the virtualenv first)

(有了这个,您不需要先手动创建 virtualenv)

回答by Tristan

With ansible 2.0 you can specify a python version for your virtualenv with virtualenv_python

使用 ansible 2.0,您可以为您的 virtualenv 指定一个 python 版本 virtualenv_python

For example:

例如:

- name: Initiate virtualenv
  pip: virtualenv="{{ virtualenv_dir }}" 
       virtualenv_python=python3.4
       requirements={{ app_dir }}/requirements.txt