Managing Multiple Versions of Ansible with virtualenv

Tadashi Shigeoka ·  Tue, August 6, 2019

This article introduces how to install multiple versions of Ansible using virtualenv.

Ansible

Prerequisites

  • macOS v10.14.6
  • Use Python 3.7.3 instead of Python 2.7
$ python3 --version
Python 3.7.3
$ pip3 --version
pip 19.0.3 from /usr/local/lib/python3.7/site-packages/pip (python 3.7)

Install virtualenv

Install virtualenv with pip3.

pip3 install virtualenv
pip3 install virtualenvwrapper

Add the following to .bashrc or .zshrc:

export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3
if [ -f /usr/local/bin/virtualenvwrapper.sh ]; then
    export WORKON_HOME=$HOME/.virtualenvs
    source /usr/local/bin/virtualenvwrapper.sh
fi
$ source ~/.zshrc
virtualenvwrapper.user_scripts creating /Users/shigeoka/.virtualenvs/premkproject
virtualenvwrapper.user_scripts creating /Users/shigeoka/.virtualenvs/postmkproject
virtualenvwrapper.user_scripts creating /Users/shigeoka/.virtualenvs/initialize
virtualenvwrapper.user_scripts creating /Users/shigeoka/.virtualenvs/premkvirtualenv
virtualenvwrapper.user_scripts creating /Users/shigeoka/.virtualenvs/postmkvirtualenv
virtualenvwrapper.user_scripts creating /Users/shigeoka/.virtualenvs/prermvirtualenv
virtualenvwrapper.user_scripts creating /Users/shigeoka/.virtualenvs/postrmvirtualenv
virtualenvwrapper.user_scripts creating /Users/shigeoka/.virtualenvs/predeactivate
virtualenvwrapper.user_scripts creating /Users/shigeoka/.virtualenvs/postdeactivate
virtualenvwrapper.user_scripts creating /Users/shigeoka/.virtualenvs/preactivate
virtualenvwrapper.user_scripts creating /Users/shigeoka/.virtualenvs/postactivate
virtualenvwrapper.user_scripts creating /Users/shigeoka/.virtualenvs/get_env_details

Create virtualenv Environment for Ansible

Install Latest Version of ansible

First, install the latest version of ansible.

mkvirtualenv ansible
(ansible) $ pip3 install ansible
(ansible) $ ansible --version
ansible 2.8.3
  config file = None
  configured module search path = ['/Users/shigeoka/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /Users/shigeoka/.virtualenvs/ansible/lib/python3.7/site-packages/ansible
  executable location = /Users/shigeoka/.virtualenvs/ansible/bin/ansible
  python version = 3.7.3 (default, Mar 27 2019, 09:23:15) [Clang 10.0.1 (clang-1001.0.46.3)]

Install Specific Version of ansible

mkvirtualenv ansible2.7.12 
(ansible2.7.12) $ pip3 install ansible==2.7.12
(ansible2.7.12) $ ansible --version   
ansible 2.7.12
  config file = None
  configured module search path = ['/Users/shigeoka/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /Users/shigeoka/.virtualenvs/ansible2.7.12/lib/python3.7/site-packages/ansible
  executable location = /Users/shigeoka/.virtualenvs/ansible2.7.12/bin/ansible
  python version = 3.7.3 (default, Mar 27 2019, 09:23:15) [Clang 10.0.1 (clang-1001.0.46.3)]

Exit virtualenv Environment

To exit the virtualenv environment, run the deactivate command.

(ansible2.7.12) $ deactivate

That’s all from the Gemba on switching between and using multiple versions of Ansible.

Reference Information