Sunday, April 5, 2015

Provisioning MySQL with Vagrant and Ansible

Now that we're a bit more familiar with Vagrant and Ansible, we're going to utilize them both to quickly provision an Ubuntu VM running MySQL on our local machines. If you're just now learning about Ansible, Chef, Puppet, or any of the other auto-provisioning solutions out there, I hope you're able to find a way to incorporate them into your development workflow. This VM will also have all of the necessary software to enable python applications to connect.

Prerequisites: Clone the respository at and run vagrant up from the root directory.

The Playbook

Our playbook.yml consists of the following:

- hosts: all
  gather_facts: True
    - name: Update OS
      command: apt-get update

    - name: Install MySQL
      apt: name=mysql-server state=installed

    - name: Install Apt Pip
      apt: name=python-pip state=installed

    - name: Install Pip
      command: pip install -U pip

    - name: Install python-dev
      apt: name=python-dev state=installed

    - name: Install Python-MySQL Dependencies
      apt: name=libmysqlclient-dev state=installed

    - name: Install MySQL-python
      apt: name=python-mysqldb state=installed

    - name: Ensure MySQL is running
      service: name=mysql state=started enabled=true

    - name: Ensure MySQL starts on boot
      command: update-rc.d mysql defaults 

Accessing the Database

vagrant ssh

mysql -uroot

Manual Process

In case you'd like to carry out the process, manually, here are the steps:

Set Up the Vagrantfile

mkdir -p ~/vagrants/mysql && cd ~/vagrants/mysql
vagrant init ubuntu/trusty64

Now, you'll want to replace your Vagrantfile with the following content:

# -*- mode: ruby -*-
# vi: set ft=ruby :

# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| = "ubuntu/trusty64" "forwarded_port", guest: 3306, host: 3306 "private_network", ip: ""

  config.vm.provider "virtualbox" do |vb|
     vb.customize ["modifyvm", :id, "--memory", "2048"]

Set Up MySQL

# boot up and enter the VM
vagrant up
vagrant ssh

# install dependencies
sudo apt-get update
sudo apt-get install mysql-server
sudo apt-get install python-pip
pip install -U pip
sudo apt-get install python-dev libmysqlclient-dev
sudo apt-get install python-mysqldb

No comments:

Post a Comment