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

Erika Heidi

August 22, 2014
Tweet

More Decks by Erika Heidi

Other Decks in Programming

Transcript

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

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

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

    easier code collaboration • Backend env tests / benchmark • Automation Tools learning and testing
  4. 1.1 Ansible Overview • Simple and Powerful • Tasks defined

    with YAML • Sequential Execution • Modules Directory: Ansible Galaxy • Requires installation of Ansible in the Host
  5. 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
  6. 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'], }
  7. 2.1 Variables --- - hosts: all sudo: yes vars: web_server:

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

    pkg={{ item }} state=latest with_items: - nginx - php5-fpm - git
  9. 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
  10. 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" },
  11. 2.2 Templates <VirtualHost *:80> ServerAdmin webmaster@localhost DocumentRoot {{ doc_root }}

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

    template: src=templates/apache.tpl dest=/etc/apache2/sites-available/000-default.conf
  13. 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
  14. 2.2 Templates <VirtualHost *:80> ServerAdmin webmaster@localhost DocumentRoot {{ doc_root }}

    <Directory {{ doc_root }}> AllowOverride All Require all granted </Directory> </VirtualHost>
  15. 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
  16. 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