Automating your infrastructure with Vagrant and Puppet
Vagrant, mixed in with Puppet, allows you to create a reproducible server environment. After a bit of experimentation I've been able to use the following script to generate a LAMP environment:
(default.pp)
# Basic Puppet Apache manifest
class apache {
exec { "apt-get update":
command => "/usr/bin/apt-get update"
}
package { "apache2":
ensure => present,
}
service { "apache2":
ensure => running,
require => Package["apache2"],
}
file { "default-apache2":
path => "/etc/apache2/sites-available/default",
ensure => file,
require => Package["apache2"],
source => "puppet:///modules/apache2/default",
notify => Service["apache2"]
}
}
class php {
package { "php5":
ensure => present,
}
package { "php5-cli":
ensure => present,
}
package { "php5-mysql":
ensure => present,
}
package { "libapache2-mod-php5":
ensure => present,
}
}
class mysql {
package { "mysql-server":
ensure => present,
}
service { "mysql":
ensure => running,
require => Package["mysql-server"],
}
exec { "set-mysql-password":
unless => "mysql -uroot -proot",
path => ["/bin", "/usr/bin"],
command => "mysqladmin -uroot password root",
require => Service["mysql"],
}
exec { "create-database":
unless => "/usr/bin/mysql -usite_development -psite_development site_development",
command => "/usr/bin/mysql -uroot -proot -e \"create database site_development; grant all on site_development.* to site_dev@localhost identified by 'site_development';\"",
require => Service["mysql"],
}
}
class groups {
group { "puppet":
ensure => present,
}
}
include apache
include php
include mysql
include groups
I've also created a folder called private/puppet/modules/apache2/files which contains "default" which is the new virtual host configuration file for Apache. This is copied across and sets the document root to /vagrant/ so your site just works.
Useful resources to get there included:
- Puppet and MySQL: create databases and users, just beware that MySQL limits usernames to a maximum of 16 characters which was initially throwing me off with strange errors.
- Building Vagrant Baseboxes with Veewee
- Vagrant sometimes hangs on "Waiting for VM to boot. This can take a few minutes" which was a problem I ran into building your own box seems to help. However if you don't want to do that, switching the Vagrant config to enable GUI mode and logging in from VirtualBox and running "sudo dhclient" sorts it out.