Versions Compared

Key

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

...

Assuming you have the required basic tools available, run this bash script:

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

...