Child pages
  • Multiple Ansible versions

Versions Compared

Key

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

...

HTML
<script src="https://gitlab.com/snippets/1769242.js"></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:
# source ~/.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==$v $MODULES
  deactivate
  echo "alias ansible-activate-$v='source $ANSIBLES/ansible-$v/bin/activate'" >> $BASH_SNIPPET
done


When this finishes, open up a new terminal and start typing ansible-activate and use tab completion to pick the specific version (smile)

This works for Linux distros as well - each distro does require it's own preparations:

DistroRequirements
CentOS7
sudo yum install gcc libffi-devel python-devel openssl-devel

Obviously you can use ansible to install ansible - for instance onto a deployment VM or bastion host:

...

languagebash
themeMidnight

...

 python-virtualenv

...