A Fresh Start with Ansible

26 April 2016

A guide to using Ansible to automatically setup your OSX development environment

It's Spring, a time for fresh beginnings. Today we are going to look at setting up a clean OSX development environment using Ansible.

Ansible is a technology used in the DevOps world for deploying applications and managing environments. It is a non-intrusive technology in that changes are pushed to target machines via SSH rather than requiring agents to be installed on each. In it's simpliest form Ansible can be viewed as distributed SSH with a Galaxy of modules for supporting common tasks like working with files, installing applications, provisioning on cloud platforms etc.

Getting ready

As we will be formatting the hard drive which will wipe all information, it's prudent to backup any important files along with your current profile.

For example, I copied the following files to a USB drive mounted on /Volumes/backup

cp ~/.bash_profile /Volumes/backup/setup/profile/
cp ~/.zshrc /Volumes/backup/setup/profile/
cp ~/.gitconfig /Volumes/backup/setup/profile/
cp ~/.gradle/gradle.properties /Volumes/backup/setup/profile/
cp -r ~/ssh /Volumes/backup/setup/ssh/

Creating a Mac OSX install disk

Next we wish to create a bootable USB drive with the OSX installer. Please use a USB drive without any important information as the drive will be erased by following steps.

  • Download El Capitan installer from the App store
  • Insert a 8GB or larger USB drive
  • Create Bootable OSX disk
# Assumes disk is mounted as /Volumes/Untitled - update path if required
sudo /Applications/Install\ OS\ X\ El\ Capitan.app/Contents/Resources/createinstallmedia --volume /Volumes/Untitled --applicationpath /Applications/Install\ OS\ X\ El\ Capitan.app --nointeraction
  • Follow instructions to setup bootable OSX disk
  • Restart Mac and pressing the options ⌥ key
  • Select to boot from El Capitan Installer and follow instuctions to do a clean install of OSX
  • Wait until install is complete
  • Add a network cable or WIFI settings for your local network

Installing ansible using homebrew

Now we are going to use Homebrew (a package manager for OSX) to install ansible. Run the following inside your terminal:

# Install Homebrew
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
# Install Ansible using Homebrew
brew install ansible

Playbook - what tasks to perform

A playbook is a set of instructions which tell Ansible what to do. We wish to install all the applications we use via Homebrew and reinstate our profile.

---
- hosts: all
  tasks:  
    # First let's update homebrew so it have the latest packages to install  
    - name: Update homebrew
      homebrew: update_homebrew=yes upgrade_all=yes

    # Then let use homebrew to install the command line applications
    - name: Install libraries with homebrew
      homebrew: name={{ item }} state=present
      with_items:
        - cask
        - git
        - gradle
        - groovy
        - tree
        - wget
        - zsh
    # Use homebrew cask to install applications and add link to ~/Applications/
    - name: Install libraries with homebrew cask
      homebrew_cask: name={{ item }} state=present
      with_items:
        - 1password
        - amazon-cloud-drive
        - amazon-music
        - burn
        - charles
        - clipmenu
        - dockertoolbox
        - dropbox   
        - dropzone
        - firefox
        - google-chrome
        - google-drive
        - google-hangouts
        - intellij-idea
        - istat-menus
        - java
        - ngrok
        - postgres
        - skype
        - slack
        - sourcetree
        - sublime-text
        - vagrant
        - virtualbox

    - file: path=~/.gradle state=directory mode=0755
    - name: Setup user profile
      file: src={{item.src}} dest={{item.dest}} state=link
      with_items:       
        - { src: 'setup/profile/bash_profile', dest: '~/.bash_profile' }
        - { src: 'setup/profile/zshrc', dest: '~/.zshrc' }
        - { src: 'setup/profile/gitconfig', dest: '~/.gitconfig' }
        - { src: 'setup/profile/gradle.properties', dest: '~/.gradle/gradle.properties' }
        - { src: 'setup/profile/ssh', dest: '~/.ssh' }

    #####
    # Instil repos
    #####
    - git: repo=git@gitlab.com:instil/komenco.git  dest=~/instilWorkspace/instil/komenco
    - git: repo=git@github.com:instil/rx-http.git  dest=~/instilWorkspace/instil/rx-http

Inventory - where to run playbook against

An inventory file defines the machines and groups of machine on which you wish to run a playbook against. As we are wishing to setup the local machine create a file called inventory with the following content:

localhost           ansible_connection=local

Running the Play book

Execute the following script and watch as all your applications and profile are setup:

ansible-playbook main.yml -i inventory
Article By
blog author

David McFarland

Principal Engineer