local dev on virtual machines - vagrant, virtualbox and ansible
DESCRIPTION
Developing web applications and websites locally can be troublesome if you use pre-built server packages like WAMP or MAMP, or an install tool to get Java or Ruby on your computer. Develop using modern best practices by using Vagrant, VirtualBox and Ansible to manage your development environments!TRANSCRIPT
![Page 1: Local Dev on Virtual Machines - Vagrant, VirtualBox and Ansible](https://reader036.vdocuments.site/reader036/viewer/2022081507/554bb5afb4c90594278b48a3/html5/thumbnails/1.jpg)
Local Development EnvironmentsVagrant, VirtualBox, and Ansible
Jeff Geerling
![Page 2: Local Dev on Virtual Machines - Vagrant, VirtualBox and Ansible](https://reader036.vdocuments.site/reader036/viewer/2022081507/554bb5afb4c90594278b48a3/html5/thumbnails/2.jpg)
Prepackaged environments
• Traditional prepackaged development stacks:
• Apache, MySQL, PHP (WAMP/MAMP)
• Java/Tomcat
• Ruby/Rails
![Page 3: Local Dev on Virtual Machines - Vagrant, VirtualBox and Ansible](https://reader036.vdocuments.site/reader036/viewer/2022081507/554bb5afb4c90594278b48a3/html5/thumbnails/3.jpg)
Prepackaged environments
• Difficulties:
• Real-world environments are more complex
• Single stack for multiple/different projects
• Prod server differences cause bugs
• Hard to configure
• “Snowflakes”
![Page 4: Local Dev on Virtual Machines - Vagrant, VirtualBox and Ansible](https://reader036.vdocuments.site/reader036/viewer/2022081507/554bb5afb4c90594278b48a3/html5/thumbnails/4.jpg)
WAMP/MAMP/non-VM
“But it works on my machine!”
(we’re going to solve this problem)
![Page 5: Local Dev on Virtual Machines - Vagrant, VirtualBox and Ansible](https://reader036.vdocuments.site/reader036/viewer/2022081507/554bb5afb4c90594278b48a3/html5/thumbnails/5.jpg)
VM-based development
• VM (Virtual Machine):
• Match prod closely
• Configure to heart’s content
• Destroy and rebuild if broken
• BUT, annoying to hand-configure (not an out-of-the-box solution)
+
+
+
![Page 6: Local Dev on Virtual Machines - Vagrant, VirtualBox and Ansible](https://reader036.vdocuments.site/reader036/viewer/2022081507/554bb5afb4c90594278b48a3/html5/thumbnails/6.jpg)
![Page 7: Local Dev on Virtual Machines - Vagrant, VirtualBox and Ansible](https://reader036.vdocuments.site/reader036/viewer/2022081507/554bb5afb4c90594278b48a3/html5/thumbnails/7.jpg)
Vagrant
• Created to solve difficulties of VM-based development
• Bring up, destroy, rebuild VMs with ease
• Simple Ruby configuration file (Vagrantfile)
• Can define single-server or multi-server production-like environments
![Page 8: Local Dev on Virtual Machines - Vagrant, VirtualBox and Ansible](https://reader036.vdocuments.site/reader036/viewer/2022081507/554bb5afb4c90594278b48a3/html5/thumbnails/8.jpg)
![Page 9: Local Dev on Virtual Machines - Vagrant, VirtualBox and Ansible](https://reader036.vdocuments.site/reader036/viewer/2022081507/554bb5afb4c90594278b48a3/html5/thumbnails/9.jpg)
Ansible
• Born out of frustration with complexities and limitations of existing CM solutions like Puppet
• “A powerful automation engine that makes systems and apps simple to deploy. No more scripting. No custom code. No agents required. Just get in and get it done.”
• Easy idempotence <— I like!
![Page 10: Local Dev on Virtual Machines - Vagrant, VirtualBox and Ansible](https://reader036.vdocuments.site/reader036/viewer/2022081507/554bb5afb4c90594278b48a3/html5/thumbnails/10.jpg)
Simple, Repeatable VMs
+ +
(or)- shell scripts
- puppet- chef- salt
![Page 11: Local Dev on Virtual Machines - Vagrant, VirtualBox and Ansible](https://reader036.vdocuments.site/reader036/viewer/2022081507/554bb5afb4c90594278b48a3/html5/thumbnails/11.jpg)
Let’s see how this works
• We’re going to:
• Create a 64-bit CentOS 6.4 VM with Vagrant
• Configure the VM with Ansible
• Run the VM inside VirtualBox
(all free and open source, by the way…)
![Page 12: Local Dev on Virtual Machines - Vagrant, VirtualBox and Ansible](https://reader036.vdocuments.site/reader036/viewer/2022081507/554bb5afb4c90594278b48a3/html5/thumbnails/12.jpg)
Notes on Windows• Preference for POSIX-based systems (OS X,
Linux, etc.)… since that’s where everything is deployed anyways
• Windows sometimes often requires hand-holding
• Example: Ansible doesn’t run (easily) on Windows, so it’s run from within VM.
• Shared folders are slow. Might need to us CIFS/Samba on Windows (similar to NFS on Mac/Linux).
![Page 13: Local Dev on Virtual Machines - Vagrant, VirtualBox and Ansible](https://reader036.vdocuments.site/reader036/viewer/2022081507/554bb5afb4c90594278b48a3/html5/thumbnails/13.jpg)
Getting Started
• Install VirtualBox
• Install Vagrant
• Install Ansible:$ sudo pip install ansible(like I said, difficult on Windows, even with Cygwin)
![Page 14: Local Dev on Virtual Machines - Vagrant, VirtualBox and Ansible](https://reader036.vdocuments.site/reader036/viewer/2022081507/554bb5afb4c90594278b48a3/html5/thumbnails/14.jpg)
Build a CentOS VM
$ vagrant box add centos64 <url>$ vagrant init centos64$ vagrant up
•Done!
![Page 15: Local Dev on Virtual Machines - Vagrant, VirtualBox and Ansible](https://reader036.vdocuments.site/reader036/viewer/2022081507/554bb5afb4c90594278b48a3/html5/thumbnails/15.jpg)
Provision the VM
•Add to the Vagrantfile that was just created:
config.vm.provision "ansible" do |ansible| ansible.playbook = "playbook.yml" ansible.sudo = trueend
•Then create playbook.yml:
---- hosts: all tasks: - yum: pkg=httpd state=installed
![Page 16: Local Dev on Virtual Machines - Vagrant, VirtualBox and Ansible](https://reader036.vdocuments.site/reader036/viewer/2022081507/554bb5afb4c90594278b48a3/html5/thumbnails/16.jpg)
Provision the VM
• To run the provisioner:
vagrant provision
• Done!
![Page 17: Local Dev on Virtual Machines - Vagrant, VirtualBox and Ansible](https://reader036.vdocuments.site/reader036/viewer/2022081507/554bb5afb4c90594278b48a3/html5/thumbnails/17.jpg)
![Page 18: Local Dev on Virtual Machines - Vagrant, VirtualBox and Ansible](https://reader036.vdocuments.site/reader036/viewer/2022081507/554bb5afb4c90594278b48a3/html5/thumbnails/18.jpg)
Slightly More Realistic
---# Configure basic CentOS LAMP stack.- hosts: all tasks: - name: Install Dependencies. yum: src={{ items }} state=installed with_items: - httpd - mysql - mysql-server - php - php-common - etc… - name: Copy Apache configuration file. template: src=templates/httpd.conf dest=/etc/httpd/httpd.conf - name: Ensure Apache is running and starts at boot. service: name=httpd state=started enabled=yes
![Page 19: Local Dev on Virtual Machines - Vagrant, VirtualBox and Ansible](https://reader036.vdocuments.site/reader036/viewer/2022081507/554bb5afb4c90594278b48a3/html5/thumbnails/19.jpg)
Provisioning on Windows
• Shell script provisioning works on Windows
• Use shell script to set up Ansible dependencies, then run Ansible playbook from within VM:
config.vm.provision "shell" do |sh| sh.path = "windows.sh" sh.args = "playbook.yml inventory"end
• Since it’s Ruby, you can use ruby if/else for which provisioner to use, with RbConfig::CONFIG['host_os']
![Page 20: Local Dev on Virtual Machines - Vagrant, VirtualBox and Ansible](https://reader036.vdocuments.site/reader036/viewer/2022081507/554bb5afb4c90594278b48a3/html5/thumbnails/20.jpg)
Provisioning on Windows
• See: Running Ansible within Windows
• See: JJG-Ansible-Windows
![Page 21: Local Dev on Virtual Machines - Vagrant, VirtualBox and Ansible](https://reader036.vdocuments.site/reader036/viewer/2022081507/554bb5afb4c90594278b48a3/html5/thumbnails/21.jpg)
Resources
• Ansible documentation
• Vagrant documentation
• Ansible for Devops (book in progress)