简体   繁体   中英

Ansible - Virtualenv executable not found when trying python3.5

Is there a way to fix the pip module not being able to find the right python version? The key issue seems to be with virtualenv_python

- name: Create venv and install requirements
  pip:
    requirements: /home/admin/dev/python/filepro/requirements.txt
    virtualenv: /home/admin/venvs/filepro
    virtualenv_python: python3.5
  tags:
    - venv

The error:

Error message:
FAILED! => {"changed": false, "failed": true, "msg": "Failed to find required executable virtualenv"}

/usr/bin/python3.5 is where python 3.5 is and I'm using Ansible 2.2.1.0

First, you need to make sure virtualenv is installed for the version of Python you intend to use. You can do that prior running the pip module by:

- name: Install virtualenv via pip
  pip:
    name: virtualenv
    executable: pip3

If you don't want (or cannot) install virualenv as root, Ansible will fail to pick up the virtualenv executable. You can add it manually to the PATH environmental variable:

- name: Create venv and install requirements
  pip:
    requirements: /home/admin/dev/python/filepro/requirements.txt
    virtualenv: /home/admin/venvs/filepro
    virtualenv_python: python3.5
  tags:
    - venv
  environment:
    PATH: "{{ ansible_env.PATH }}:{{ ansible_user_dir }}/.local/bin"

Alternatively, you can install vitualenv as a root user:

- name: Install virtualenv via pip
  pip:
    name: virtualenv
    executable: pip3
  become: yes
  become_user: root

you can use the following to create a virtual env using the python3-venv module

First of all, you need to have python3-venv package in your destination server

install it using $ sudo apt install python3-venv

then in your ansible task you can create the virtual env as follows

- name: "Setup Virtual Env",
  pip:
     - requirements: path/to/requirements.txt # this is optional
     - virtualenv: path/to/required/virtual_env_destination
     - virtualenv_command: 'python3 -m venv'

No need to use virtualenv_python parameter as well

The problem was that virtualenv was not installed as sudo.

Please correct me if I'm wrong, but appears that to get the pip module to work with virtualenv you need to run sudo pip install virtualenv

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM