Skip to content

Instantly share code, notes, and snippets.

@reza
Last active December 25, 2017 06:04
Show Gist options
  • Save reza/40d8ef64908944d161827a6988d52095 to your computer and use it in GitHub Desktop.
Save reza/40d8ef64908944d161827a6988d52095 to your computer and use it in GitHub Desktop.
ansible-aws-cloud-init.sh
#!/bin/bash
# Ver 2.1, By Reza Hashemi
# Automate EC2 instance setup and Install latest Ansible and its dependencies via pip (recommended method)
# The cloud init script detects the EC2 instance platform at launch and uses preferred package manager
# AWS cloud init script to install ansible on startup, autodetects required package manager
# Add as user data inside advanced details at AWS EC2 configure instance details (step 3)
# Changes:
# Ver 2.1 Tested on Centos 6
# No issues with pynacl and cryptography
# Ver 2 Tested on RHEL, Centos 7, Ubuntu, SUSE, Fedora 23-25, Debian
# No issue with pycrypto (on Fedora)
# Install & Test
# Add as user data inside advanced details at AWS EC2 configure instance details (step 3)
# Check /var/log/cloud-init-output.log for Cloud-init finished at the end of file
#package manager to be run in non-interactive mode is chosen
if command -v dnf >/dev/null; then system_type="dnf";package_manager="dnf -y"
elif command -v zypper >/dev/null; then system_type="zypper";package_manager="zypper -n"
elif command -v yum >/dev/null; then system_type="yum";package_manager="yum -y"
elif command -v apt-get >/dev/null; then system_type="apt";package_manager="apt-get -y"
#elif command -v pkg >/dev/null; then package_manager="pkg"
fi
#make sure sudo and which are installed
$package_manager install sudo which
#sudo apt-get -y update; sudo apt-get -y upgrade
sudo $package_manager update
# using Ansible's recommended approach (pip)
sudo $package_manager install gcc
sudo $package_manager install python-setuptools
# pip install requirements, comment/uncomment based on distro
case "$system_type" in
yum|zypper )
# Required python.h for RHEL, Centos, SUSE
sudo $package_manager install python-devel
# Required dependency for pycrypto on centos 6
sudo $package_manager install libffi-devel
# Required dependency for cryptography on centos 6
sudo $package_manager install openssl-devel
;;
dnf )
# Required python.h for Fedora
sudo $package_manager install python-devel
# Required dependency for pycrypto on Fedora
sudo $package_manager install python2-crypto
;;
apt )
# required dependency for Ubuntu, Debian
sudo $package_manager install python-dev
;;
esac
#secure https index-url is provided to keep compatibility with centos6
sudo easy_install --index-url=https://pypi.python.org/simple --upgrade pip
sudo `which pip` install --upgrade setuptools
sudo `which pip` install --upgrade ansible
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment