Reputation: 21315
I have ansible task
- name: Install setuptools in virtual environment
pip:
name: setuptools-git
virtualenv: "myenv"
virtualenv_command: "/root/.pyenv/bin/pyenv virtualenv 2.7.13"
But it gives error
fatal: [localhost]: FAILED! => {
"changed": false,
"invocation": {
"module_args": {
"chdir": null,
"editable": false,
"executable": null,
"extra_args": "",
"name": [
"setuptools-git"
],
"requirements": null,
"state": "present",
"umask": null,
"use_mirrors": true,
"version": null,
"virtualenv": "myenv",
"virtualenv_command": "/root/.pyenv/bin/pyenv virtualenv 2.7.13",
"virtualenv_python": null,
"virtualenv_site_packages": false
}
},
"msg": "Unable to find pip in the virtualenv, myenv, under any of these names: pip2, pip. Make sure pip is present in the virtualenv."
}
When I check the pip file in virtualenv, its already there
# ls -alh /root/.pyenv/versions/myenv/bin/pip
-rwxr-xr-x 1 root root 243 Jan 16 17:40 /root/.pyenv/versions/myenv/bin/pip
There are 2 virtualenvs for same name
# /root/.pyenv/bin/pyenv virtualenvs
2.7.13/envs/myenv (created from /root/.pyenv/versions/2.7.13)
myenv (created from /root/.pyenv/versions/2.7.13)
I cant use executable
with virtualenv
variable.
Upvotes: 4
Views: 1921
Reputation: 232
Basically for virtualenv
attribute, you have to pass virtual env path. See the below example.
- hosts: 1.2.3.4
user: remoteuser
environment:
PYENV_ROOT: /home/<user home dir>/.pyenv
PYENV_VERSION: myenv
PYENV_SHELL: bash
PYENV_ACTIVATE_SHELL: 1
tasks:
- pip:
name: setuptools-git
virtualenv: "/home/<user home dir>/.pyenv/versions/myenv"
virtualenv_command: /home/<user home dir>/.pyenv/bin/pyenv virtualenv 3.4.0 myenv
I Hope, this will solve your issue.
Upvotes: 1
Reputation: 1022
I found a workaround to install packages with pip into a virtualenv managed by pyenv. It does avoid using the pip module and instead just executes some shell commands. I am providing some variables I used, a .pyenvrc file I created, and the ansible task that does the shell magic.
Variables:
# Installation paths
pyenv_root: "{{ ansible_env.HOME }}/.pyenv"
pyenv_rc: "{{ pyenv_root }}/.pyenvrc"
# Whatever your virtualenv is named
pyenv_venv_name: "foo_virtualenv"
project_dir: /path/to/your/project
Tasks:
- name: Pip - install requirements using shell
shell: |
# cd to project directory
cd {{ project_dir }}
# Check to see if we are already inside a virtualenv
if ! [[ ${VIRTUAL_ENV} ]]; then
# Load pyenv into the shell
source {{ pyenv_rc }}
# Activate the virtualenv
pyenv activate {{ pyenv_venv_name }}
fi
# Install python requirements
pip install -r requirements.txt
args:
executable: /bin/bash
register: pip_script_result
Contents of .pyenvrc:
# Add pyenv into path if installed into default location
export PYENV_ROOT="{{ pyenv_root }}"
export PATH="${PYENV_ROOT}/bin:${PATH}"
# Initialise pyenv and pyenv-virtualenv if installed
if [[ -d $HOME/.pyenv ]];then
eval "$(pyenv init -)"
if [ -d "${PYENV_ROOT}/plugins/pyenv-virtualenv" ]; then
eval "$(pyenv virtualenv-init -)"
fi
fi
# Disable prompt changing
export PYENV_VIRTUALENV_DISABLE_PROMPT=1
In the above example, I installed the contents of requirements.txt using pip
into my pyenv managed virtualenv. You should be able to adapt the pip install
line to do whatever you need.
Upvotes: 2