Slide 1

Slide 1 text

Ansible and Vagrant A love story AnsibleFest London, 2015 http://pixabay.com/get/f72f42c0c92d9ee12bc3/1422473642/cloud-600224_1920.jpg @Sgoettschkes

Slide 2

Slide 2 text

http://pixabay.com/get/5c603ff58720250803c9/1422648022/forest-438432_1280.jpg

Slide 3

Slide 3 text

http://pixabay.com/get/3489448873097c5aa562/1422648090/apple-195628_1280.jpg

Slide 4

Slide 4 text

“It works on my machine”

Slide 5

Slide 5 text

“It’s all in SETUP.md”

Slide 6

Slide 6 text

Enter vagrant

Slide 7

Slide 7 text

Building blocks Configuration + Provider + Base Boxes + Provisioner + Plugins = Vagrant http://pixabay.com/static/uploads/photo/2010/12/10/08/salad-1105_640.jpg

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

Providers

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

Provisioners

Slide 12

Slide 12 text

Provisioners

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

Synced folders http://upload.wikimedia.org/wikipedia/commons/5/5d/BalticServers_data_center.jpg # Vagrantfile config.vm.synced_folder "~/local/path/to/project", # on host "/opt/project" # on guest

Slide 15

Slide 15 text

SSH http://pixabay.com/static/uploads/photo/2013/04/08/22/48/tunnel-101976_640.jpg $ vagrant ssh # or $ ssh -p 2222 vagrant@localhost # with password “vagrant”

Slide 16

Slide 16 text

Example $ tree . |-- .git |-- .vagrant |-- playbook.yml |-- Vagrantfile

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

Example $ cat playbook.yml --- hosts: all tasks: - name: update apt apt: update_cache=yes - name: install git apt: pkg=git state=latest

Slide 22

Slide 22 text

More dependencies, anyone? https://www.dropbox.com/s/xdeyi95buvofdxy/169H.jpg

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

Better example $ cat hosts localhost ansible_connection=local

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

http://freehddesktopwallpaper.info/wp-content/uploads/2013/06/Blue-Flowers-hd-Wallpapers.jpg Basic workflow

Slide 31

Slide 31 text

Basic workflow $ vagrant init Sgoettschkes/debian7-ansible $ vagrant up # box is booting $ vagrant ssh # It’s actually a normal, headless VM! $ exit

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

Basic workflow # Co-worker updated Vagrantfile/provisioning files $ git pull --rebase $ vagrant provision # VM is up to date again

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

http://www.gratisography.com/pictures/114_1.jpg Playbook testing?

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

Testing playbooks $ cat testing [webservers] 192.168.1.150 ansible_ssh_user=vagrant ansible_ssh_private_key=path/to/key [dbservers] 192.168.1.151 ansible_ssh_user=vagrant ansible_ssh_private_key=path/to/key

Slide 41

Slide 41 text

Testing playbooks $ cat testing [webservers] 192.168.1.150 ansible_ssh_user=vagrant ansible_ssh_private_key=path/to/key [dbservers] 192.168.1.151 ansible_ssh_user=vagrant ansible_ssh_private_key=path/to/key

Slide 42

Slide 42 text

Testing playbooks $ cat testing [webservers] 192.168.1.150 ansible_ssh_user=vagrant ansible_ssh_private_key=path/to/key [dbservers] 192.168.1.151 ansible_ssh_user=vagrant ansible_ssh_private_key=path/to/key

Slide 43

Slide 43 text

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

Slide 44

Slide 44 text

https://www.dropbox.com/s/mfmusz1qjotilbs/2H.jpg Best practices

Slide 45

Slide 45 text

Base Boxes The fewer, the better

Slide 46

Slide 46 text

Provisioning Idempotent

Slide 47

Slide 47 text

Provisioning One step

Slide 48

Slide 48 text

Provisioning # Bad: $ vagrant up $ install software x $ run script y $ create z

Slide 49

Slide 49 text

Provisioning # Good: $ vagrant up

Slide 50

Slide 50 text

Vms per Developer The fewer, the better (again)

Slide 51

Slide 51 text

http://nos.twnsnd.co/image/108652898544 What else is there?

Slide 52

Slide 52 text

Vagrant share $ vagrant share # Your VM is now accessible through a public url # You can go back doing awesome work!

Slide 53

Slide 53 text

Vagrant push config.push.define "ftp" do |push| push.dir = "/srv/workspace" push.host = "ftp.awesomeCompany.com" end

Slide 54

Slide 54 text

Vagrant push config.push.define "local-exec" do |push| push.script = "deploy.sh" end

Slide 55

Slide 55 text

Vagrant “Development environments made easy” http://fc02.deviantart.net/fs50/i/2009/315/4/c/Tweaked_toy_airplane_by_afd.jpg

Slide 56

Slide 56 text

https://www.dropbox.com/s/6z5aiwa8l09g2pa/86H.jpg