0

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 /root/.pyenv/versions/myenv/bin/pip
/root/.pyenv/versions/myenv/bin/pip

I cant use executable with virtualenv variable.

3 Answers 3

1

can you make sure your pip is updated? From the virtualenv you run: easy_install --upgrade pip. I had the same error but I am not using virtualenv. Running easy_install upgrade fixed my problem.

1
1

Looks like you are hitting an open issue in Ansible.

Check if setting an absolute path for the virtualenv parameter resolves your problem. (As suggested by the Ansible devs in the issue report.)

Also there is nothing in the Ansible documentation indicating it is possible to set parameters for the virtualenv_command in the Ansible virtualenv_command parameter. So you might want to try setting it to /root/.pyenv/bin/pyenv.

1
  • Thanks to pointing this open issue. I cant understand your last ile to setting to /root/.pyenv/bin/pyenv, can you tell me what changes I have to do to make this things work?
    – Nilesh
    Jan 19, 2018 at 15:36
1

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .