Skip to content

Instantly share code, notes, and snippets.

@cgsmith
Created March 4, 2015 01:57
Show Gist options
  • Save cgsmith/09aa97c8467e59a82b26 to your computer and use it in GitHub Desktop.
Save cgsmith/09aa97c8467e59a82b26 to your computer and use it in GitHub Desktop.
Vagrant example with Ansible. This is a simple setup I use for Vagrant and Ansible for the majority of my projects. Note that my provisioning stuff is in a folder.

Install/Config

Hello! There is a fairly simple process, please raise issues if installing gives you any issues.

Prerequisites

Now:

  1. Clone the repo to your local machine with git clone
  2. Run vagrant up

Yay! You should be able to navigate to your local though by doing www.kbpattern.local

You may also:

  • Set appropriate usernames and passwords in the config.php file

Versioning

We are going to follow Semantic Versioning (http://semver.org/spec/v2.0.0.html). Version 1.0 will indicate a public release where as we are starting the stable release at v0.8.0.

Change Log

v0.8.0

  • Initial stable release
  • Vagrant file for development
[kbpattern]
192.168.56.120 ansible_connection=ssh ansible_ssh_user=vagrant
---
- hosts: kbpattern
tasks:
- name: Add repo php5 to get lastest php
# To get previous version, replace with php5-oldstable or for new use php5
apt_repository: repo='ppa:ondrej/php5-oldstable' update_cache=yes
- name: Install apache2,php5,php5-common,git,mysql,curl
apt: pkg={{ item }} state=present update_cache=yes
with_items:
- apache2
- php5
- php5-common
- php5-tidy
- php5-mcrypt
- php5-mysql
- php5-curl
- php5-cli
- git
- mysql-server
- curl
- python-mysqldb # required for mysql commands below
- name: http service state
service: name=apache2 state=started enabled=yes
- name: Start Mysql Service
service: name=mysql state=started enabled=true
# install composer
- name: install composer
shell: curl -sS https://getcomposer.org/installer | php creates=/usr/local/bin/composer
tags: composer
- name: rename composer.phar to composer
shell: mv composer.phar /usr/local/bin/composer creates=/usr/local/bin/composer
tags: composer
- name: make composer executable
shell: chmod a+x /usr/local/bin/composer
tags: composer
# Create a new db with 'kbpattern' as name
- name: make kbpattern database
mysql_db: name=kb-pattern state=present
# copy file over and restore it, create kbpattern user for access
- name: copy sql file
copy: src=../data/current-backup.sql dest=/tmp
- name: update kbpattern database
mysql_db: name=kb-pattern state=import target=/tmp/current-backup.sql
# - name: Create Application DB User
# mysql_user: name=kbpattern password=kbpattern priv=*.*:ALL host='%' state=present
# copy config file over to www path
- name: Copying configuration file over from source
shell: cp /var/www/model/config.php.example /var/www/model/config.php
# Downloads and installs all the libs and dependencies outlined in the /var/www/composer.lock
- name: Install libs and dependencies
command: composer install
args:
chdir: /var/www/
---
vm:
box: hashicorp/precise64
box_url: hashicorp/precise64
ip_address: 192.168.56.120
hostname: kbpattern.local
alias1: kbpattern.local
alias2: www.kbpattern.local
sync_host: /home/csmith/dev/cgs/kb-pattern
sync_vm: /var/www
# -*- mode: ruby -*-
# vi: set ft=ruby :
require 'yaml'
# Variables
data = YAML.load_file("provisioning/variables.yml")
Vagrant.require_version ">= 1.6.0"
Vagrant.configure("2") do |config|
# All Vagrant configuration is done below. see vagrantup.com.
config.vm.box = "#{data['vm']['box']}"
config.vm.box_url = "#{data['vm']['box_url']}"
# configure network, ports
config.vm.network "private_network", ip: "#{data['vm']['ip_address']}"
config.vm.network "forwarded_port", guest: 22, host: 9093
config.vm.network "forwarded_port", guest: 80, host: 8080
config.vm.hostname = "#{data['vm']['hostname']}"
config.hostsupdater.aliases = ["#{data['vm']['alias1']}","#{data['vm']['alias2']}"]
# sync folders
config.vm.synced_folder "#{data['vm']['sync_host']}", "#{data['vm']['sync_vm']}",
group: 'www-data', owner: 'www-data'
# Provisioning configuration for Ansible
config.vm.provision "ansible" do |ansible|
ansible.playbook = "provisioning/playbook.yml"
ansible.inventory_path = "provisioning/ansible_hosts"
ansible.host_key_checking = false
ansible.sudo = true
ansible.limit = 'all'
ansible.extra_vars = { ansible_ssh_args: '-o ForwardAgent=yes'}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment