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

Portable Development Environments with Vagrant and Ansible

Portable Development Environments with Vagrant and Ansible

Slides for my talk "Portable Development Environments with Vagrant Ansible", as presented at DayCamp4Developers 2014

719435d98d452de7ac367c828266cf01?s=128

Erika Heidi

August 22, 2014
Tweet

Transcript

  1. None
  2. whoami • Brazilian, living in Amsterdam since 2012 • PHP

    developer with sysadmin background | devop • Author of Vagrant Cookbook on LeanPub and phansible.com
  3. What to expect from this talk 1)Quick Vagrant overview 2)Provisioning

    with Ansible 3)Quickly getting started with Phansible (demo)
  4. None
  5. Why Vagrant? • Reproducible and portable development environment • Enables

    easier code collaboration • Backend env tests / benchmark • Automation Tools learning and testing
  6. Important Terms • Host and Guest • Provider • Provisioner

    • Vagrantfile
  7. None
  8. None
  9. 1.1 Ansible Overview • Simple and Powerful • Tasks defined

    with YAML • Sequential Execution • Modules Directory: Ansible Galaxy • Requires installation of Ansible in the Host
  10. 1.2 Vagrantfile Vagrant.configure("2") do |config| config.vm.box = "hashicorp/precise64" config.vm.provision "ansible"

    do |ansible| ansible.playbook = "playbook.yml" end end
  11. 1.3 The Playbook # playbook.yml --- - hosts: all sudo:

    true tasks: - name: Update apt-cache apt: update_cache=yes - name: Install Nginx apt: pkg=nginx state=latest
  12. 1.4 Playbook x Manifest #playbook.yml --- - hosts: all sudo:

    true tasks: - name: Update apt-cache apt: update_cache=yes - name: Install Nginx apt: pkg=nginx state=latest #default.pp exec { 'apt-get update': command => '/usr/bin/apt-get update' } package { 'nginx': ensure => "installed", require => Exec['apt-get update'], }
  13. DEMO 1 basic playbook

  14. Writing Playbooks

  15. 2.1 Variables --- - hosts: all sudo: yes vars: web_server:

    nginx tasks: - name: Install {{ web_server }} apt: pkg={{ web_server }} state=latest
  16. 2.1 Variables - arrays tasks: - name: Install Packages apt:

    pkg={{ item }} state=latest with_items: - nginx - php5-fpm - git
  17. 2.1 Variables - arrays --- - hosts: all sudo: yes

    vars: sys_packages: [ 'nginx', 'php5-fpm', 'git' ] tasks: - name: Install Packages apt: pkg={{ item }} state=latest with_items: sys_packages
  18. 2.1 Variables - facts • Information discovered from systems •

    Globally available • Example: ansible_eth0.ipv4.address "ansible_eth0": { "active": true, "device": "eth0", "ipv4": { "address": "162.243.203.85", "netmask": "255.255.255.0", "network": "162.243.203.0" }, "ipv6": [ { "address": "fe80::601:23ff:fe9c:8a01", "prefix": "64", "scope": "link" } ], "macaddress": "04:01:23:9c:8a:01", "mtu": 1500, "promisc": false, "type": "ether" },
  19. 2.2 Templates <VirtualHost *:80> ServerAdmin webmaster@localhost DocumentRoot {{ doc_root }}

    <Directory {{ doc_root }}> AllowOverride All Require all granted </Directory> </VirtualHost>
  20. 2.2 Templates - Usage - name: Change default apache vhost

    template: src=templates/apache.tpl dest=/etc/apache2/sites-available/000-default.conf
  21. 2.3 Handlers (services) --- - hosts: all sudo: yes vars:

    - doc_root: /vagrant tasks: - name: Change default apache vhost template: src=templates/apache.tpl dest=/etc/apache2/sites- available/000-default.conf notify: restart apache handlers: - name: restart apache service: name=apache2 state=restarted
  22. DEMO 2 Variables, templates, handlers

  23. 2.2 Templates <VirtualHost *:80> ServerAdmin webmaster@localhost DocumentRoot {{ doc_root }}

    <Directory {{ doc_root }}> AllowOverride All Require all granted </Directory> </VirtualHost>
  24. Organization

  25. 3.1 Including Tasks --- - hosts: all sudo: true vars:

    doc_root: /vagrant/web tasks: - include: tasks/init.yml - include: tasks/nginxphp.yml handlers: - name: restart nginx service: name=nginx state=restarted
  26. 3.2 Roles . ├── playbook.yml └── roles ├── init │

    └── tasks │ └── main.yml └── nginxphp ├── handlers │ └── main.yml ├── tasks │ └── main.yml └── templates └── vhost.tpl #playbook.yml --- - hosts: all sudo: true vars: doc_root: /vagrant/web roles: - init - nginxphp
  27. None
  28. None
  29. None
  30. DEMO 3 phansible

  31. Vagrant Cookbook Get a special discount with this coupon http://bit.ly/vc-dc4devs

  32. Questions?

  33. erikaheidi.com/vagrant https://joind.in/1166 3