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

Vagrant and Ansible: A love story

Sebastian
February 05, 2015

Vagrant and Ansible: A love story

My talk "vagrant + ansible: A love story" will introduce the audience not yet familiar with vagrant to this wonderfull tool which aims at "Create[ing] and configur[ing] lightweight, reproducible, and portable development environments". This will include a short overview over the architecture of vagrant and the different use cases. Two different ways of using ansible with vagrant will be discussed together with pro's and con's of both. Best practices of working withvagrant and ansible will be described.

Sebastian

February 05, 2015
Tweet

More Decks by Sebastian

Other Decks in Technology

Transcript

  1. Building blocks Configuration + Provider + Base Boxes + Provisioner

    + Plugins = Vagrant http://pixabay.com/static/uploads/photo/2010/12/10/08/salad-1105_640.jpg
  2. Configuration Vagrant.configure(2) do |config| config.vm.box = "Sgoettschkes/debian7-ansible" config.vm.network "private_network", ip:

    "127.0.0.101" config.vm.synced_folder "../", "/srv/workspace" config.vm.provision "ansible" do |ansible| ansible.playbook = "playbook.yml" end end
  3. Base Boxes Base Boxes • Image from VM • Provider-specific

    • Distribution through ◦ HTTP ◦ “Atlas” http://upload.wikimedia.org/wikipedia/commons/9/91/Shipping_containers_at_Clyde.jpg
  4. Plugins # AWS provider $ vagrant plugin install vagrant-aws #

    Boxen provisioner $ vagrant plugin install ventriloquist # Executing on the host $ vagrant plugin install vagrant-hostmanager # Executing on the guest $ vagrant plugin install vagrant-camera
  5. Example $ cat Vagrantfile Vagrant.configure(2) do |config| config.vm.box = "Sgoettschkes/debian7-ansible"

    config.vm.network "private_network", ip: "127.0.0.101" config.vm.provision "ansible" do |ansible| ansible.playbook = "playbook.yml" end end
  6. Example $ cat Vagrantfile Vagrant.configure(2) do |config| config.vm.box = "Sgoettschkes/debian7-ansible"

    config.vm.network "private_network", ip: "127.0.0.101" config.vm.provision "ansible" do |ansible| ansible.playbook = "playbook.yml" end end
  7. Example $ cat Vagrantfile Vagrant.configure(2) do |config| config.vm.box = "Sgoettschkes/debian7-ansible"

    config.vm.network "private_network", ip: "127.0.0.101" config.vm.provision "ansible" do |ansible| ansible.playbook = "playbook.yml" end end
  8. Example $ cat Vagrantfile Vagrant.configure(2) do |config| config.vm.box = "Sgoettschkes/debian7-ansible"

    config.vm.network "private_network", ip: "127.0.0.101" config.vm.provision "ansible" do |ansible| ansible.playbook = "playbook.yml" end end
  9. Example $ cat playbook.yml --- hosts: all tasks: - name:

    update apt apt: update_cache=yes - name: install git apt: pkg=git state=latest
  10. Better example $ tree . |-- .git |-- .vagrant |--

    hosts |-- playbook.yml |-- Vagrantfile
  11. Better example $ tree . |-- .git |-- .vagrant |--

    hosts |-- playbook.yml |-- Vagrantfile
  12. Better example Vagrant.configure(2) do |config| # ... config.vm.provision "shell", inline:

    "cp /vagrant/hosts /etc/ansible/hosts" if Vagrant.has_plugin?("vagrant-ansible-local”) config.vm.provision "ansibleLocal", playbook: "playbook.yml" else config.vm.provision "shell", inline: "ansible-playbook /vagrant/playbook.yml" end
  13. Better example Vagrant.configure(2) do |config| # ... config.vm.provision "shell", inline:

    "cp /vagrant/hosts /etc/ansible/hosts" if Vagrant.has_plugin?("vagrant-ansible-local”) config.vm.provision "ansibleLocal", playbook: "playbook.yml" else config.vm.provision "shell", inline: "ansible-playbook /vagrant/playbook.yml" end
  14. Better example Vagrant.configure(2) do |config| # ... config.vm.provision "shell", inline:

    "cp /vagrant/hosts /etc/ansible/hosts" if Vagrant.has_plugin?("vagrant-ansible-local”) config.vm.provision "ansibleLocal", playbook: "playbook.yml" else config.vm.provision "shell", inline: "ansible-playbook /vagrant/playbook.yml" end
  15. Better example Vagrant.configure(2) do |config| # ... config.vm.provision "shell", inline:

    "cp /vagrant/hosts /etc/ansible/hosts" if Vagrant.has_plugin?("vagrant-ansible-local”) config.vm.provision "ansibleLocal", playbook: "playbook.yml" else config.vm.provision "shell", inline: "ansible-playbook /vagrant/playbook.yml" end
  16. Basic workflow $ vagrant init Sgoettschkes/debian7-ansible $ vagrant up #

    box is booting $ vagrant ssh # It’s actually a normal, headless VM! $ exit
  17. Basic workflow $ vim Vagrantfile # change config # add

    provisioners and so on $ git init $ git commit -Am ‘I am using Vagrant now!’ # Keep the Vagrantfile and files needed for provisioning # in git (or any other scm) to share with others
  18. Basic workflow $ git clone [email protected]:awesomecompany/awesomeVm.git $ cd awesomeVm &&

    vagrant up # VM is setup the same as on your co-workers machine # You can now work on your project
  19. Basic workflow # Co-worker updated Vagrantfile/provisioning files $ git pull

    --rebase $ vagrant provision # VM is up to date again
  20. Basic workflow # You wanna change some Vagrant related stuff

    $ vim Vagrantfile # Change it! $ vim playbook.yml # Change something here as well, maybe? $ vagrant provision # Your box is up to date again $ git commit -am ‘Incredible changes’ && git push
  21. Testing playbooks Vagrant.configure(2) do |config| config.vm.box = "Sgoettschkes/debian7" config.vm.define "web1"

    do |web1| web1.vm.network "private_network", ip: "192.168.1.150" end config.vm.define "db1" do |db1| db.vm.network "private_network", ip: "192.168.1.151" end end
  22. Testing playbooks Vagrant.configure(2) do |config| config.vm.box = "Sgoettschkes/debian7" config.vm.define "web1"

    do |web1| web1.vm.network "private_network", ip: "192.168.1.150" end config.vm.define "db1" do |db1| db.vm.network "private_network", ip: "192.168.1.151" end end
  23. Testing playbooks Vagrant.configure(2) do |config| config.vm.box = "Sgoettschkes/debian7" config.vm.define "web1"

    do |web1| web1.vm.network "private_network", ip: "192.168.1.150" end config.vm.define "db1" do |db1| db.vm.network "private_network", ip: "192.168.1.151" end end
  24. Testing playbooks $ vagrant destroy --force && vagrant up #

    Now you have freshly setup machines # Run your playbooks like you would for staging/production $ ansible-playbook -i testing site.yml # Maybe run a testscript $ ./test_setup.sh
  25. Vagrant share $ vagrant share # Your VM is now

    accessible through a public url # You can go back doing awesome work!