I am using a basic Ansible Playbook in order to install some Python packages on a Ubuntu 18.04 system. For the sake of demonstration, take the following example playbook test.yml
.
---
- name: Installing demo package.
hosts: localhost
tasks:
- name: Installing with pip.
pip:
name:
- "numpy"
virtualenv: "venv"
virtualenv_python: python3.6
I installed a virtual environment using python3 -m venv venv
and activated it using source venv/bin/activate/
. However, if I run ansible-playbook test.yml
, I get the following error:
fatal: [localhost]: FAILED! => {"changed": false, "cmd": "venv/bin/pip install numpy", "msg": "[Errno 2] No such file or directory", "rc": 2}
Error analysis:
I ran a couple of commands from the this related question.
ansible localhost -a "which pip"
returns:
localhost | SUCCESS | rc=0 >>
/home/mydir/venv/bin/pip
I tried to clear the hash using ansible localhost -m shell -a "hash -r"
and obtained
localhost | SUCCESS | rc=0 >>
Afterwards, ansible localhost -m shell -a "type pip""
returns:
localhost | SUCCESS | rc=0 >>
pip is /home/mydir/venv/bin/pip
But nevertheless, the problem persists, even though I could install numpy using venv/bin/pip install numpy
in a shell without any disturbances.
The version of Ansible is 2.5.1.
The actual path evaluated from virtualenv: "venv"
might be different from what you expect. Once you have an existing virtualenv, then you can be more explicit in convincing Ansible to use it: with the executable
parameter of the pip module instead of virtualenv
.
---
- name: Installing demo package.
hosts: localhost
tasks:
- name: Installing with pip.
pip:
name:
- "numpy"
executable: /home/mydir/venv/bin/pip