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

Vagrant + Ansible

Michael Heap
October 05, 2013

Vagrant + Ansible

Vagrant is a tool for automating the creation of virtual machines using either Virtualbox or VMware. Of course, automating the creation of a VM is only half of the battle – once you have one you need to configure it. That’s where Ansible comes in

Ansible is a third generation configuration tool (think Puppet or Chef, but easier), allowing you to install software and configure things as you need in an automated manor. Once we can spin up VM’s and provision them with software automatically, we add some form of predictability to our environments.

This talk introduces everyone to Vagrant and Ansible, and walks through creating an initial VM that installs PHP, Apache2 and configures an example website with a custom VirtualHost.

Michael Heap

October 05, 2013
Tweet

More Decks by Michael Heap

Other Decks in Technology

Transcript

  1. Get a Vagrant Box A box is a virtual machine

    image. There are official boxes available More at http://vagrantbox.es Saturday, 5 October 13
  2. Vagrant.configure(2) do |config| config.vm.box = "precise64" config.vm.network :private_network, ip: "192.168.33.10"

    config.vm.provider :virtualbox do |vb| vb.customize ["modifyvm", :id, "--memory", "1024"] end end Saturday, 5 October 13
  3. Vagrant.configure(2) do |config| config.vm.box = "precise64" config.vm.network :private_network, ip: "192.168.33.10"

    config.vm.provider :virtualbox do |vb| vb.customize ["modifyvm", :id, "--memory", "1024"] end end Saturday, 5 October 13
  4. Vagrant.configure(2) do |config| config.vm.box = "precise64" config.vm.network :private_network, ip: "192.168.33.10"

    config.vm.provider :virtualbox do |vb| vb.customize ["modifyvm", :id, "--memory", "1024"] end end Saturday, 5 October 13
  5. Vagrant.configure(2) do |config| config.vm.box = "precise64" config.vm.network :private_network, ip: "192.168.33.10"

    config.vm.provider :virtualbox do |vb| vb.customize ["modifyvm", :id, "--memory", "1024"] end end Saturday, 5 October 13
  6. $ vagrant up This will boot up your machine. If

    it already exists, it’ll just boot. If it doesn’t, it’ll import your base box Saturday, 5 October 13
  7. $ vagrant ssh Log in to your new VM This

    uses INSECURE KEYS. Saturday, 5 October 13
  8. $ vagrant destroy Destroy your VM, including any changes you

    made. Next time you $ vagrant up, it’ll be a fresh box Saturday, 5 October 13
  9. Now what? We have a machine, but it doesn’t actually

    do anything. Saturday, 5 October 13
  10. Demo Time! Install apache2, php, mysql and git Configure a

    virtualhost Clone a site from Github Visit it in Chrome Saturday, 5 October 13
  11. Installing Ansible Install from source, or from a package manager

    Use pip, apt or yum Saturday, 5 October 13
  12. ansible-playbook Once you’ve told Ansible where to run, we need

    to tell it what to run Saturday, 5 October 13
  13. --- - hosts: all sudo: true vars: - domain: demo.dev

    # Some packages omitted tasks: - name: Install apache apt: name=apache2 state=installed - name: create vhost template: src=templates/demo dest=/etc/apache2/sites-enabled/demo notify: - restart apache - name: clone repo git: repo=https://github.com/mheap/demo.git dest=/var/www/demo handlers: - name: restart apache service: name=apache2 state=restarted Saturday, 5 October 13
  14. --- - hosts: all sudo: true vars: - domain: demo.dev

    # Some packages omitted tasks: - name: Install apache apt: name=apache2 state=installed - name: create vhost template: src=templates/demo dest=/etc/apache2/sites-enabled/demo notify: - restart apache - name: clone repo git: repo=https://github.com/mheap/demo.git dest=/var/www/demo handlers: - name: restart apache service: name=apache2 state=restarted Saturday, 5 October 13
  15. --- - hosts: all sudo: true vars: - domain: demo.dev

    # Some packages omitted tasks: - name: Install apache apt: name=apache2 state=installed - name: create vhost template: src=templates/demo dest=/etc/apache2/sites-enabled/demo notify: - restart apache - name: clone repo git: repo=https://github.com/mheap/demo.git dest=/var/www/demo handlers: - name: restart apache service: name=apache2 state=restarted Saturday, 5 October 13
  16. --- - hosts: all sudo: true vars: - domain: demo.dev

    # Some packages omitted tasks: - name: Install apache apt: name=apache2 state=installed - name: create vhost template: src=templates/demo dest=/etc/apache2/sites-enabled/demo notify: - restart apache - name: clone repo git: repo=https://github.com/mheap/demo.git dest=/var/www/demo handlers: - name: restart apache service: name=apache2 state=restarted Saturday, 5 October 13
  17. --- - hosts: all sudo: true vars: - domain: demo.dev

    # Some packages omitted tasks: - name: Install apache apt: name=apache2 state=installed - name: create vhost template: src=templates/demo dest=/etc/apache2/sites-enabled/demo notify: - restart apache - name: clone repo git: repo=https://github.com/mheap/demo.git dest=/var/www/demo handlers: - name: restart apache service: name=apache2 state=restarted Saturday, 5 October 13
  18. --- - hosts: all sudo: true vars: - domain: demo.dev

    # Some packages omitted tasks: - name: Install apache apt: name=apache2 state=installed - name: create vhost template: src=templates/demo dest=/etc/apache2/sites-enabled/demo notify: - restart apache - name: clone repo git: repo=https://github.com/mheap/demo.git dest=/var/www/demo handlers: - name: restart apache service: name=apache2 state=restarted Saturday, 5 October 13
  19. Roles Ansible has a concept of roles. You can define

    roles e.g. “web” that installs Apache and PHP, and “db” that installs mysql and then apply them on demand. Saturday, 5 October 13
  20. Parameterised includes Imagine you have a Wordpress playbook but want

    different database passwords for each of them. Saturday, 5 October 13
  21. Conditional actions When collecting facts with Facter or ohai, you

    can run actions conditionally. Saturday, 5 October 13
  22. Rolling Deploys You can set the maximum number of nodes

    to run on, and a maximum failure percentage. No need to take down your entire cluster with a bad deploy. Saturday, 5 October 13
  23. More! We’d be here all day if I listed all

    of the available features, so go read the docs. Saturday, 5 October 13
  24. Orchestration! You can do crazy things like take machines out

    of a load balancer pool, upgrade and add them again Saturday, 5 October 13
  25. Packaging: apt, yum, pip, npm, gem Files: file, copy, template,

    lineinfile Cloud: ec2, linode, digital_ocean, s3, rds Notification: campfire, hipchat, irc, jabber, email DB: mysql_user, mongodb_user, postgresql_user Alerting: monit, nagios,pagerduty, pingdom CLI: command, shell (this is a small selection) Saturday, 5 October 13
  26. Write your own Use PHP if you want. Just because

    Ansible is Python, it doesn’t mean modules have to be. Saturday, 5 October 13
  27. ansible all -m “shell” -a “uname -r” 192.168.33.10 | success

    | rc=0 >> 3.2.0-23-generic Saturday, 5 October 13
  28. Big wins It uses SSH as it’s PKI. There’s no

    daemon to run on a server. You choose when to run it, not a cron job. Saturday, 5 October 13
  29. ansible-pull If you want it though, you can emulate the

    “pull” functionality. Clones your playbooks, runs them locally. Infinitely* scaleable. * If your Git host can handle infinite clones Saturday, 5 October 13
  30. Thanks! I’ve been @mheap, you’ve been awesome. Please leave feedback

    on Joind.in https://joind.in/9297 Saturday, 5 October 13