Digital Ocean Ansible Setup UserData


Digital Ocean
For more information on Digital Ocean, visit digitalocean.com


Description:


This is a informational tutorial on how to launch Digital Ocean Droplets setup to run Ansible playbooks using userdata. When launching a droplet via the Digital Ocean console or the API, a user has the option of specifying optional userdata. The userdata section is a set of instructions in bash script format that allow the user to customize the droplet on launch by auto executing any instructions or commands in the userdata section of the droplet configuration. This informational tutorial contains a userdata script for multiple platforms on how to get a droplet launched, and ready to run playbooks on launch.


Pre-Requisites:


1.    OS:


   COMPATIBILITY:
_________________________________

  CentOS 6 and 7
  Debian 8 Jessie
  Ubuntu 14.04 Trusty and up


2.    Digital Ocean Account:

In order to test out the userdata scripts, you must already have a digital ocean account provisioned. When logged into the account, simply create a new droplet the way you normally would, with the additional step of selecting the User data option under the Select additional options section of the droplet launch window. This will open a text box that will allow you to paste in a userdata script that will be fired off on the droplet once the droplet has launched.


UserData Scripts:


  CentOS 6:


#!/bin/bash

# Install Epel, as it holds the latest Ansible RPM, then update
yum install epel-release -y
rpm --import http://download.fedoraproject.org/pub/epel/RPM-GPG-KEY-EPEL-6
yum update -y

# Install PIP
curl "https://bootstrap.pypa.io/get-pip.py" -o "/tmp/get-pip.py"
python /tmp/get-pip.py
pip install pip --upgrade
rm -fr /tmp/get-pip.py

# Install Ansible
yum install git ansible -y
mkdir -p /etc/ansible/roles || exit 0
echo `hostname` ansible_connection=local > /etc/ansible/hosts



  CentOS 7:


#!/bin/bash

# Install Epel, as it holds the latest Ansible RPM, then update
yum install epel-release -y
rpm --import http://download.fedoraproject.org/pub/epel/RPM-GPG-KEY-EPEL-7
yum update -y

# Install PIP
curl "https://bootstrap.pypa.io/get-pip.py" -o "/tmp/get-pip.py"
python /tmp/get-pip.py
pip install pip --upgrade
rm -fr /tmp/get-pip.py

# Install Ansible
yum install git ansible -y
mkdir -p /etc/ansible/roles || exit 0
echo `hostname` ansible_connection=local > /etc/ansible/hosts

yum install git ansible -y
mkdir -p /etc/ansible/roles || exit 0
echo `hostname` ansible_connection=local > /etc/ansible/hosts



  Debian 8
  Ubuntu Trusty and Up:


#!/bin/bash

# Update and install Git:
sudo apt-get update
sudo apt-get upgrade -y
sudo apt-get install -y git

# If using Ubuntu, install some required python packages:
sudo apt-get install -y python-dev python-openssl libffi-dev libssl-dev gcc

# Use Pip to install ansible:
sudo easy_install pip
sudo pip install pip --upgrade
sudo pip install ansible
sudo mkdir -p /etc/ansible/roles || exit 0
sudo echo `hostname` ansible_connection=local > /etc/ansible/hosts


Post Requisites:


None


References: