I am currently working on rewriting an old tool that uses ansible to create a virtual environment in which it hosts a python based app. The failing playbook is as follows:
- name: Ensure bash, OpenSSL, and libssl are the latest versions
become: true
apt: name={{ item }} update_cache=yes state=latest
with_items:
- bash
- openssl
- libssl-dev
- libssl-doc
tags: packages
- name: install minimum packages
apt: name={{ item }} update_cache=yes state=installed
with_items:
- libav-tools
- build-essential
- python-pip
- python-numpy
- python-scipy
- python-software-properties
- python-dev
- python-setuptools
- python-libxml2
- python-libxslt1
- python3.4-dev
- libatlas-base-dev
- gcc
- gfortran
- g++
- binutils
- libproj-dev
- gdal-bin
- python-gdal
- python-lxml
- python3-lxml
- libblas-dev
- pkg-config
- gfortran
- python-dev
- libxml2-dev
- libjpeg8-dev
- libjpeg-dev
- libpq-dev
- libpng12-0
- libpng-dev
- libpng12-dev
- libfreetype6
- libfreetype6-dev
- supervisor
- nginx
- git
- ntp
- htop
- libcurl4-openssl-dev
- libgdal-dev
- libtool
- libxml2
- libxslt1.1
- libxslt1-dev
- redis-server
- freetype*
- php5-gd
- python-memcache
- name: upgrade packages
apt: upgrade=safe
- name: Install virtualenv
pip: name=virtualenv
tags: packages
- name: Create the virtualenv
command: virtualenv /home/mrv/www/v_env --no-site-packages
creates=/home/mrv/www/v_env/bin/activate
It fails when it tries to "Create the virtualenv" as it is unable to install the zipp module. The error line is as follows (truncated):
File "/usr/local/lib/python2.7/dist-packages/importlib_metadata/_init_.py", line 9, in <module>", " import zipp", "ImportError: No module named zipp"], "stdout": "", "stdout_lines": []}
I learnt that zipp now requires python 3.6 and upwards while this virtualenv seems to be using python 2.7
Is there any way around this? Or can I mention what version of python I want to use in the playbook itself? I barely know Ansible thus asking for help.
EDIT: I came across this post that mentions specifying the python version – Ansible creating a virtualenv
Following this, I tried:
- name: Create the virtualenv
command:
cmd: virtualenv /home/mrv/www/v_env --no-site-packages -p python3.6
creates: "/home/mrv/www/v_env/bin/activate"
But this did not work either.
2
Answers
There is a variable in ansible to set the python interpreter(indirectly version) in the host:
Example:
You can set it through
group_vars
orinventory
orextra-vars
. Set the value to the desired python version. So For example, In my virtual environments, I have python defaulted at/home/ps/.pyenv/shims/python
, so if I do not setansible_python_interpreter
ansible will use this path,but if you set it to something else, ansible would honor it.I have a recommendation for your tasks with
apt
module, when this module is used with a loop (with_items
) ansible runs individually it’s much efficient use a list directly in thename
option.Try as below