Ansible create a virtualenv using the venv module
Asked Answered
G

3

35

How can one use Ansible to create a virtualenv using the venv module from Python3's standard library?

Manually, one would do this to create a venv (virtual environment):

python3 -m venv <venv-name>

How do I do this using Ansible?

Glomma answered 3/3, 2017 at 12:41 Comment(3)
@kyslik I have already read the documentation multiple times, searched SO, and tried to make things work on my computer. I am aware of the virtualenv_command option in the ansible pip module. However, ansible gives an error that python3 -m venv is not a valid command. So I am at loss.Glomma
@kyslik Unfortunately, ansible will throw an error if you don't give pip a name.Glomma
Note that ansible's python 3 support might still be flakyFriede
B
52

I ran into the same issue tonight and found that specifying the full path to the interpreter, including the arguments, worked for me (at least it does in ansible==2.2.2.0):

- pip:
     requirements: /website/requirements.txt
     virtualenv: /opt/website-venv
     virtualenv_command: /usr/bin/python3.6 -m venv

or

- pip:
    requirements: /opt/project/requirements_prod.txt
    virtualenv: /opt/.virtualenv/project_env
    virtualenv_python: python3
Baikal answered 30/3, 2017 at 3:54 Comment(2)
It's important to use full path to the python executable. Otherwise ansible says it can't find it in PATH dirs.Vivid
Just FYI in ansible=2.7 to use python3 -m venv you have to use: virtualenv_command: <path_to_python> -m venv and do not include virtualenv_python. See: docs.ansible.com/ansible/latest/modules/pip_module.htmlMair
M
2

Here is an example using Ansible's command statement:

- name: Create Python virtual environment
  command:
    cmd: python3 -m venv {{ project_path }}/project_venv
    creates: "{{ project_path }}/project_venv"
Mutton answered 5/9, 2023 at 17:34 Comment(1)
using pip module is more suited because it also install packages inside. Thanks to recall that command/shell exist. I would just the ansible_python_interpreter instead of python3 directlySakovich
D
-3
# Install specified python requirements in indicated (virtualenv).
- pip:
  requirements: /my_app/requirements.txt
  virtualenv: /my_app/venv

If python3 is indeed being flaky, you can specify which version of python you'd like to use:

# Install specified python requirements in indicated (virtualenv).
- pip:
  requirements: /my_app/requirements.txt
  virtualenv: /my_app/venv
  virtualenv_command: virtualenv-2.7

I think that answers your question.

Delossantos answered 10/3, 2017 at 2:39 Comment(2)
Syntax is broken. And you seemingly did not understand the meaning of the remark on Python 3 support. It was not about the version of Python inside the virtualenv, but which version of Python Ansible uses to run its modules. You don't set that in your example.Callboy
I am using the venv module from the python standard library to create virtual environments. Not the virtualenv module.Glomma

© 2022 - 2024 — McMap. All rights reserved.