Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

It is impractical to refactor large projects to work with the latest Ansible version, so there is a need to use to specific Ansible versions for specific deployments.

On MacOS I use the HomeBrew package manager to provide me with Ansible, and this works OK. Switching between different Ansible versions is possible, but in practise this is only works if you have cached the old version, i.e. you have been installing and updating versions continuously. Picking a random older version is very tedious at best.

...

Assuming you have the required basic tools available, install this in your home directoryrun this bash script:

Code Block
languagebash
themeMidnight
linenumberstrue
#!/usr/bin/env bash
# Maintains a directory of Ansible virtual environments, and
# an accompanying snippet of bash aliases to activate them.
# Put the following into your .bash_rc to be able to use the aliases:
# sourcemkdir ~/.virtualenvs
virtualenv ~/.virtualenvs/ansible-2.3.3
source ~/.virtualenvs/ansible-2.3.3/bin/activate
bash_ansible

BASH_SNIPPET="$HOME/.bash_ansible"
ANSIBLES="$HOME/.ansible_virtualenvs"
# Update this to keep regularly to have the latest of each minor version
VERSIONS="2.1.6  2.2.3  2.3.3  2.4.6  2.5.10  2.6.6  2.7.0"
# Needed for the projects we work on
MODULES="python-keyczar jmespath netaddr dnspython boto botocore boto3 natsort"
# Switch between different python versions. Remember to wipe the old virtualenvs if you do this.
PYTHON="python"

if [ ! -d "$ANSIBLES" ]; then
  echo "Ansible collection directory $ANSIBLES does not exist yet, creating it."
  mkdir -v $ANSIBLES
fi

echo "# Generated at `date` from $0" > $BASH_SNIPPET

for v in $VERSIONS; do
  virtualenv -p "$PYTHON" "$ANSIBLES/ansible-$v"
  source "$ANSIBLES/ansible-$v/bin/activate"
  pip install ansible==2.3.3
deactivate

Repeat this for every version you want to be able to use.

Then add a line like this to your .bash_profile for each version:

Code Block
$v $MODULES
  deactivate
  echo "alias ansible-activate-2.3.3="source ~/.virtualenv$v='source $ANSIBLES/ansible-2.3.3$v/bin/activate'" >> $BASH_SNIPPET
done


When this finishes, you can just start typing You can just type ansible-activate and use tab completion to pick the specific version.

...