Skip to content

Instantly share code, notes, and snippets.

@ahmedsajid
Last active April 16, 2020 15:37
Show Gist options
  • Save ahmedsajid/351c5437817d4ef0c4dfe86af2d503e1 to your computer and use it in GitHub Desktop.
Save ahmedsajid/351c5437817d4ef0c4dfe86af2d503e1 to your computer and use it in GitHub Desktop.
Ansible version switcher

Ansible version switcher

Script which installs variety of Ansible versions and sets them up using virtualenv.

Loading ansible

source ./ansible_version 2.7.0

Mostly based on original script by @gbolo

#!/bin/bash
# Some default versions
ANSIBLE_VERSIONS="2.4.3 2.4.6 2.5.0 2.5.11 2.6.7 2.7.0 2.7.1"
# Setting virtualenv directory
VIRTUALENV_DIR=${HOME}/virtualenv/ansible
# Function to install ansible versions
INSTALL_ANSIBLE_VERSIONS() {
local version=${1}
# If a version is specified then use that for installation instead of using a list of versions
if [ "x${version}" != "x" ]; then
ANSIBLE_VERSIONS=${version}
fi
if [ ! -d "${VIRTUALENV_DIR}" ]; then
echo ">> CREATING VIRTUALENV DIR: ${VIRTUALENV_DIR}"
mkdir -p "${VIRTUALENV_DIR}"
fi
for v in ${ANSIBLE_VERSIONS}; do
if [ ! -d "${VIRTUALENV_DIR}/${v}" ]; then
echo ">> INSTALLING ANSIBLE VERSION: ${v}"
virtualenv "${VIRTUALENV_DIR}/${v}"
source "${VIRTUALENV_DIR}/${v}/bin/activate"
# Installing docker-py for docker operations on local machine
# Installing jmespath for json_query
# Installing mitogen for speed up
pip install ansible==${v} docker-py jmespath mitogen
deactivate
fi
done
}
# List installed versions
SHOW_INSTALLED_VERSIONS() {
echo ">> AVAILABLE ANSIBLE VERSIONS:"
for v in ${ANSIBLE_VERSIONS}; do
echo " ${v}"
done
echo
}
# Load specified version of ansible
LOAD_ANSIBLE_VERSION() {
local CHOSEN_VERSION="${1}"
if [ -d "${VIRTUALENV_DIR}/${CHOSEN_VERSION}" ]; then
source "${VIRTUALENV_DIR}/${CHOSEN_VERSION}/bin/activate"
# Setting environment variables to use mitogen
export ANSIBLE_STRATEGY_PLUGINS=`find "${VIRTUALENV_DIR}/${CHOSEN_VERSION}" -type d -name "ansible_mitogen"`
export ANSIBLE_STRATEGY="mitogen_linear"
else
echo "VERSION NOT AVAILABLE"
exit 1
fi
}
# ------------------------------------------------------------------------------
# check for Required program(s)
req_progs=(virtualenv pip)
for p in ${req_progs[@]}; do
hash "$p" 2>&- || \
{ echo >&2 " Required program \"$p\" not installed."; exit 1; }
done
# Calling install function
INSTALL_ANSIBLE_VERSIONS ${1}
# If all good load it
if [ $# -eq 0 ]; then
SHOW_INSTALLED_VERSIONS
read -p "SELECT VERSION: " ANSIBLE_VERSION
LOAD_ANSIBLE_VERSION ${ANSIBLE_VERSION}
else
LOAD_ANSIBLE_VERSION ${1}
fi
@gbolo
Copy link

gbolo commented Mar 7, 2019

nice!

can you add a quick check for required bins? for example:

# Required program(s)
req_progs=(virtualenv pip)
for p in ${req_progs[@]}; do
  hash "$p" 2>&- || \
  { echo >&2 " Required program \"$p\" not installed."; exit 1; }
done

@ahmedsajid
Copy link
Author

nice!

can you add a quick check for required bins? for example:

# Required program(s)
req_progs=(virtualenv pip)
for p in ${req_progs[@]}; do
hash "$p" 2>&- || \
{ echo >&2 " Required program \"$p\" not installed."; exit 1; }
done

Done!!!
Thanks for suggestion 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment