Skip to content

Instantly share code, notes, and snippets.

@arnehormann
Created October 24, 2013 16:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arnehormann/7140310 to your computer and use it in GitHub Desktop.
Save arnehormann/7140310 to your computer and use it in GitHub Desktop.
ansible with virtualenv bootstrap script
#!/bin/bash
# exit on error, undeclared variables and pipe failures
set -e -u -o pipefail
# ansible is made for python 2.7+
# TODO: check version(s) and provide better errors if anything fails
# install virtualenv if it's not installed yet
which -s virtualenv || (sudo easy_install pip && sudo pip install virtualenv)
OLD_DIR=`pwd`
DIR=$(cd `dirname "$0"`; pwd)
cd "$DIR"
# create a lib folder
test -d lib || mkdir lib
# create virtualenv if it does not exist already
test -d lib/virtualenv || virtualenv lib/virtualenv
# activate virtualenv; activate uses undeclared variables, so use set +u
set +u;. lib/virtualenv/bin/activate;set -u
# make sure ansible is installed
{ pip show ansible | grep ansible &> /dev/null; } || pip install ansible
# print a message on how to use the virtualenv
cat <<MESSAGE
activate the virtualenv with
. lib/virtualenv/bin/activate
MESSAGE
# and from where to use it if not already there
test "$DIR" != "$OLD_DIR" && echo after changing to the project directory "$DIR"
# change to previous directory to avoid surprises
cd "$OLD_DIR"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment