Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Vagrant Provisioners in a Nutshell

Vagrant Provisioners in a Nutshell

Presented at PhpDay Verona - 16/05/2014

Erika Heidi

May 16, 2014
Tweet

More Decks by Erika Heidi

Other Decks in Programming

Transcript

  1. View Slide

  2. whoami

    Brazilian, living in Amsterdam since
    2012

    PHP developer and
    eventually sysadmin

    Author of Vagrant Cookbook on
    LeanPub

    View Slide

  3. What to expect from this talk
    1)Quick Vagrant overview
    2)Provisioner Tasting: Ansible, Puppet and Chef
    3)Useful Resources
    4)What's new

    View Slide

  4. View Slide

  5. Why
    Vagrant?

    View Slide

  6. “It works on my machine”
    - every developer, ever.

    View Slide

  7. Why Vagrant?

    Reproducible and portable development environment

    Enables easier code collaboration

    Backend env tests / benchmark

    Automation Tools learning and testing

    View Slide

  8. View Slide

  9. The simplest thing that does something
    Vagrant.configure("2") do |config|
    config.vm.box = "hashicorp/precise64"
    config.vm.provision "shell", inline: "echo Hello World!"
    end

    View Slide

  10. DEMO

    View Slide

  11. View Slide

  12. 1. Ansible

    Tasks, Playbooks, Roles

    Tasks are defined with YAML

    3rd most used

    Modules Directory: Ansible Galaxy

    Requires installation of Ansible in the Host

    View Slide

  13. 1.1 A Task
    - name: Install Nginx
    apt: pkg=nginx state=latest

    View Slide

  14. 1.1 A Task
    - name: Install Nginx
    apt: pkg=nginx state=latest
    - name: Install PHP Packages
    apt: pkg={{ item }} state=latest
    with_items:
    - php5-fpm
    - php5-cli

    View Slide

  15. 1.2 A Playbook
    # playbook.yml
    ---
    - hosts: all
    sudo: true
    tasks:
    - name: Update apt-cache
    apt: update_cache=yes
    - name: Install Nginx and php5-fpm
    apt: pkg={{ item }} state=latest
    with_items:
    - nginx
    - php5-fpm

    View Slide

  16. 1.3 A Role
    .
    ├── playbook.yml
    └── roles
    ├── init
    │ └── tasks
    │ └── main.yml
    └── nginxphp
    ├── tasks
    │ └── main.yml
    └── templates
    └── vhost.tpl
    #playbook.yml
    ---
    - hosts: all
    sudo: true
    vars:
    doc_root: /vagrant/web
    roles:
    - init
    - nginxphp

    View Slide

  17. 1.4 Vagrantfile
    Vagrant.configure("2") do |config|
    config.vm.box = "hashicorp/precise64"
    config.vm.provision "ansible" do |ansible|
    ansible.playbook = "playbook.yml"
    end
    end

    View Slide

  18. View Slide

  19. 2. Puppet (puppet-apply)

    Resources, Manifests, Modules

    Non-sequential execution order

    Custom language based on Ruby

    1st most used

    Modules Directory: Puppet Forge

    View Slide

  20. 2.1 A Resource
    package { 'nginx':
    ensure => 'installed'
    }

    View Slide

  21. 2.1 A Resource
    package { 'nginx':
    ensure => 'installed'
    }
    package { ['php5-fpm', 'php5-cli']:
    ensure => 'installed'
    }

    View Slide

  22. 2.2 A Manifest
    # manifests/default.pp
    exec { 'apt-get update':
    command => 'apt-get update'
    }
    package { ['nginx', 'php5-fpm']:
    ensure => 'installed',
    require => Exec['apt-get update']
    }

    View Slide

  23. 2.3 A Module
    .
    ├── manifests
    │ └── default.pp
    └── modules
    └── nginxphp
    ├── manifests
    │ └── init.pp
    └── templates
    └── vhost.erb
    # manifests/default.pp
    exec { 'apt-get update':
    command => 'apt-get update',
    before => Class['nginxphp'],
    }
    class { 'nginxphp':
    doc_root => '/vagrant/web',
    }

    View Slide

  24. 2.4 Vagrantfile
    Vagrant.configure("2") do |config|
    config.vm.box = "hashicorp/precise64"
    config.vm.provision :puppet do |puppet|
    puppet.module_path = "modules"
    end
    end

    View Slide

  25. View Slide

  26. 3. Chef (chef_solo)

    Resources, Recipes, Cookbooks

    Resources defined with Ruby

    2nd most used, 1st with Ruby devs

    Modules Directory: Cookbooks

    Complex but very powerful

    View Slide

  27. 3.1 A Resource
    apt_package "nginx" do
    action :install
    end

    View Slide

  28. 3.1 A Resource
    apt_package "nginx" do
    action :install
    end
    ["nginx", "php5-fpm"].each do |p|
    apt_package p do
    action :install
    end
    end

    View Slide

  29. 3.2 A Recipe
    # cookbooks/main/recipes/default.rb
    execute "apt-get update" do
    command "apt-get update"
    end
    ["nginx", "php5-fpm"].each do |p|
    apt_package p do
    action :install
    end
    end

    View Slide

  30. 3.3 A Cookbook
    .
    └── cookbooks
    ├── main
    │ └── recipes
    │ └── default.rb
    └── nginxphp
    ├── recipes
    │ └── default.rb
    └── templates
    └── default
    └── vhost.erb
    # cookbooks/main/recipes/default.rb
    execute "apt-get update" do
    command "apt-get update"
    end
    include_recipe 'nginxphp'

    View Slide

  31. 3.4 Vagrantfile
    Vagrant.configure("2") do |config|
    config.vm.box = "hashicorp/precise64"
    config.vm.provision "chef_solo" do |chef|
    chef.add_recipe "main"
    end
    end

    View Slide

  32. View Slide

  33. Some Cool
    New Features

    View Slide

  34. View Slide

  35. Vagrant Share
    Demo

    View Slide

  36. View Slide

  37. Global status and control

    View Slide

  38. Useful
    Resources

    View Slide

  39. View Slide

  40. View Slide

  41. Vagrant Cookbook
    Special discount coupon for PhpDay
    http://bit.ly/vc-phpday

    View Slide

  42. Questions?

    View Slide

  43. https://joind.in/11299

    View Slide

  44. View Slide

  45. 6.1 Debugging

    Unknown Vagrant error
    – Use VirtualBox / Vmware GUI

    Unknown Provisioner error
    – Increase provisioner verbosity

    Not working as expected
    – Login, fix, automate

    View Slide

  46. 6.2 NFS Performance

    Synchronization has a cost

    Symfony cache/logs
    – Too much writing operations on disk
    – We don't need this in our synced folder

    View Slide

  47. View Slide

  48. View Slide

  49. View Slide