Skip to content

Instantly share code, notes, and snippets.

@datherra
Forked from trinitronx/README.md
Created July 31, 2016 17:07
Show Gist options
  • Save datherra/58674b119ced19843841e80b057f69bc to your computer and use it in GitHub Desktop.
Save datherra/58674b119ced19843841e80b057f69bc to your computer and use it in GitHub Desktop.
A shell script to install latest Ansible via pip + dependencies

omnibus-ansible

Install latest Ansible via pip + dependencies via a shell script

This file is used to install ansible in test kitchen when you set in the kitchen.yaml file

require_ansible_omnibus: true

By default test kitchen will always download and use the latest version of this install file.

WARNING: AS SOON AS YOU MERGE CODE HERE IT IS INSTANTLY AVAILABLE TO EVERYONE DOING OMNIBUS KITCHEN ANSIBLE INSTALLS:

To get a previous version or lock the install script to a particular version update your kitchen.yaml file to: ansible_omnibus_url: https://raw.githubusercontent.com/neillturner/omnibus-ansible/ab2fa5771b6ef357786d7ff57d83fc8c97c22fed/ansible_install.sh

where ab2fa5771b6ef357786d7ff57d83fc8c97c22fed is the commit sha of the code.

Supported platforms:

  • centos-5.10 - Fails due to pip not working on python 2.4.. this article provides a workaround, but it seemed risky and wouldn't reflect most user's reality of running Ansible on CentOS 5.
  • centos-6.4 - Works! (Note: Has procps instead of procps-ng)
  • centos-6.5 - Works! (Note: Has procps instead of procps-ng)
  • centos-7.0 - Works! (Note: Has procps-ng instead of procps)
  • ubuntu-10.04 - Fails due to pycrypto conflict
  • ubuntu-12.04 - Works!
  • ubuntu-14.04 - Works!
  • ubuntu-14.10 - Works!
  • ubuntu-15.04 - Works!
  • debian-6.0.8 - Works!
  • debian-6.0.10 - Works!
  • debian-7.8 - Works!
  • debian-8.1 - Works!

It does some basic OS detection and tries to install all the things that Ansible needs, including some optional packages which are technically dependencies of some core Ansible modules (Ansible playbooks that use these modules would simply fail without them).

It also installs ca-certificates and has built-in yum failure retry ability (With RHEL / CentOS, this is important because yum tends to fail a lot due to stale cache and old SSL CA cert chain issues). It also tries to install via packaged python-pip, and if that fails, it tries easy_install pip.

This ends up being a lot of things to install (mainly due to optional Ansible module dependencies), but the hope is that it covers all the bases so that some Ansible modules don't just fail unexpectedly.

The tradeoff is the amount of download / install time this takes versus a more minimal install. This useful to avoid having to bootstrap Ansible's dependencies using a pre-role or pre_tasks: in the test playbook when just testing one playbook or role without redoing the same work that already done in this script.

This script was built with reliability, resiliency, and installing all "full-stack" dependencies (hence: Omnibus) in mind, not speed.

Feel free to fork this repo and create custom install scripts.

#!/bin/sh
#TODO.md
yum_makecache_retry() {
tries=0
until [ $tries -ge 5 ]
do
yum makecache && break
let tries++
sleep 1
done
}
if [ "x$KITCHEN_LOG" = "xDEBUG" -o "x$OMNIBUS_ANSIBLE_LOG" = "xDEBUG" ]; then
export PS4='(${BASH_SOURCE}:${LINENO}): - [${SHLVL},${BASH_SUBSHELL},$?] $ '
set -x
fi
if [ ! $(which ansible-playbook) ]; then
if [ -f /etc/centos-release ] || [ -f /etc/redhat-release ] || [ -f /etc/oracle-release ] || [ -f /etc/system-release ] || grep -q 'Amazon Linux' /etc/system-release; then
# Install required Python libs and pip
# Fix EPEL Metalink SSL error
# - workaround: https://community.hpcloud.com/article/centos-63-instance-giving-cannot-retrieve-metalink-repository-epel-error
# - SSL secure solution: Update ca-certs!!
# - http://stackoverflow.com/q/26734777/645491#27667111
# - http://serverfault.com/q/637549/77156
# - http://unix.stackexchange.com/a/163368/7688
yum -y install ca-certificates nss
yum clean all
rm -rf /var/cache/yum
yum_makecache_retry
yum -y install epel-release
# One more time with EPEL to avoid failures
yum_makecache_retry
yum -y install python-pip PyYAML python-jinja2 python-httplib2 python-keyczar python-paramiko git
# If python-pip install failed and setuptools exists, try that
if [ -z "$(which pip)" -a -z "$(which easy_install)" ]; then
yum -y install python-setuptools
easy_install pip
elif [ -z "$(which pip)" -a -n "$(which easy_install)" ]; then
easy_install pip
fi
# Install passlib for encrypt
yum -y groupinstall "Development tools"
yum -y install python-devel MySQL-python sshpass && pip install pyrax pysphere boto passlib dnspython
# Install Ansible module dependencies
yum -y install bzip2 file findutils git gzip hg svn sudo tar which unzip xz zip libselinux-python
[ -n "$(yum search procps-ng)" ] && yum -y install procps-ng || yum -y install procps
elif [ -f /etc/debian_version ] || [ grep -qi ubuntu /etc/lsb-release ] || grep -qi ubuntu /etc/os-release; then
apt-get update
# Install via package
# apt-get update && \
# apt-get install --no-install-recommends -y software-properties-common && \
# apt-add-repository ppa:ansible/ansible && \
# apt-get update && \
# apt-get install -y ansible
# Install required Python libs and pip
apt-get install -y python-pip python-yaml python-jinja2 python-httplib2 python-paramiko python-pkg-resources
[ -n "$( apt-cache search python-keyczar )" ] && apt-get install -y python-keyczar
if ! apt-get install -y git ; then
apt-get install -y git-core
fi
# If python-pip install failed and setuptools exists, try that
if [ -z "$(which pip)" -a -z "$(which easy_install)" ]; then
apt-get -y install python-setuptools
easy_install pip
elif [ -z "$(which pip)" -a -n "$(which easy_install)" ]; then
easy_install pip
fi
# If python-keyczar apt package does not exist, use pip
[ -z "$( apt-cache search python-keyczar )" ] && sudo pip install python-keyczar
# Install passlib for encrypt
apt-get install -y build-essential
apt-get install -y python-all-dev python-mysqldb sshpass && pip install pyrax pysphere boto passlib dnspython
# Install Ansible module dependencies
apt-get install -y bzip2 file findutils git gzip mercurial procps subversion sudo tar debianutils unzip xz-utils zip python-selinux
else
echo 'WARN: Could not detect distro or distro unsupported'
echo 'WARN: Trying to install ansible via pip without some dependencies'
echo 'WARN: Not all functionality of ansible may be available'
fi
mkdir /etc/ansible/
echo -e '[local]\nlocalhost\n' > /etc/ansible/hosts
pip install ansible
if [ -f /etc/centos-release ] || [ -f /etc/redhat-release ] || [ -f /etc/oracle-release ] || [ -f /etc/system-release ] || grep -q 'Amazon Linux' /etc/system-release; then
# Fix for pycrypto pip / yum issue
# https://github.com/ansible/ansible/issues/276
if ansible --version 2>&1 | grep -q "AttributeError: 'module' object has no attribute 'HAVE_DECL_MPZ_POWM_SEC'" ; then
echo 'WARN: Re-installing python-crypto package to workaround ansible/ansible#276'
echo 'WARN: https://github.com/ansible/ansible/issues/276'
pip uninstall -y pycrypto
yum erase -y python-crypto
yum install -y python-crypto python-paramiko
fi
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment