Ansible creating a virtualenv

PythonAnsible

Python Problem Overview


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

I would like something like:

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

Python Solutions


Solution 1 - Python

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.

If you experience that problem, you can create the virtualenv manually with the command module:

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

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

Then you can install your requirements as normal with the pip command:

- 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:

- 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)

Solution 2 - Python

You can do it with the pip module and a specific virtualenv binary:

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

Solution 3 - Python

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

For example:

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

Solution 4 - Python

On Centos-7:

Ansible Version: 2.9 (this script should work for ansible version 2 +)

Complete ansible script to Create Python3.6 Virtual Environment

  - name: Enable EPEL Repository on CentOS 7
    yum:
      name: epel-release
      state: latest


  - name: check if virtualenv library already installed or not
    stat:
      path: /usr/bin/virtualenv
    register: pip_virtualenv_installed
    

  - name: Download Pip-Installer
    get_url:
      url: https://bootstrap.pypa.io/pip/2.7/get-pip.py
      dest: /tmp/get-pip.py
    when: pip_virtualenv_installed.stat.exists == False


  - name: Install pip
    shell: /usr/bin/python /tmp/get-pip.py
    when: pip_virtualenv_installed.stat.exists == False


  - name: Install virtualenv module
    pip:
      name: virtualenv
    when: pip_virtualenv_installed.stat.exists == False


  - name: Install Python 3.6
    yum:
      name:
        - python36
        - python36-devel
        - python36-libs
        - python3-setuptools
        - gcc
        - gcc-c++
      state: present


  - name: Create Python3-virtual environment folder
    file:
      name: /opt/python3-virtualenv
      state: directory


  - name: Initiate virtualenv
    pip:
      virtualenv: /opt/python3-virtualenv
      virtualenv_python: python3.6
      requirements: /opt/python3-virtualenv/requirements.txt

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser204088View Question on Stackoverflow
Solution 1 - Pythontoast38cozaView Answer on Stackoverflow
Solution 2 - PythonSchnoukiView Answer on Stackoverflow
Solution 3 - PythonTristanView Answer on Stackoverflow
Solution 4 - PythonManju NView Answer on Stackoverflow