Vagrant for DevOps: Ansible provisioning

I was asked to show how Ansible can work together with Vagrant.
In my example I’ll install, enable and configure UFW firewall on Ubuntu, then machine will be rebooted and uptime will be shown.
It’s really┬ásimple task, so just have a look at Vagrantfile and playbook:

Vagrant.configure("2") do |config|
    
    # Box to create VM from
    config.vm.box = "ubuntu/trusty64"

    # Link by which box can be found, f.e. for URL check
    config.vm.box_url = "https://atlas.hashicorp.com/ubuntu/boxes/trusty64"

    # Box version to use (latest)
    config.vm.box_version = ">= 0"

    # Check box update during vagrant up
    config.vm.box_check_update = true

    # Check checksum after box download
    config.vm.box_download_checksum = true

    # Set checksum type
    config.vm.box_download_checksum_type = "sha1"

    # Check SSL certificate before box download
    config.vm.box_download_insecure = false

    # SSH for linux and winrm for Windows
    config.vm.communicator = "ssh"

    # SSH username 
    config.ssh.username = "vagrant"

    # Replace default insecure SSH key
    config.ssh.insert_key = false

    # Set VM network type
    config.vm.network "private_network", type: "dhcp"
    
    # Set VM hostname
    config.vm.hostname = "svr1"

    # Set port forwarding 
    config.vm.network "forwarded_port", guest: "80", host: "8080", host_ip: "127.0.0.1", protocol: "tcp", auto_correct: true
    config.vm.network "forwarded_port", guest: "443", host: "8090", host_ip: "127.0.0.1", protocol: "tcp", auto_correct: true

    # Set VM CPU & RAM limits
    config.vm.provider "virtualbox" do |v|
        v.customize ["modifyvm", :id, "--cpuexecutioncap", "75"]
        v.memory = "512"
        v.cpus = "1"
    end    

    # Seconds to wait during gracefull halt
    config.vm.graceful_halt_timeout = "120"

    config.vm.provision "ansible" do |ansible|
        ansible.verbose = "v"
        ansible.playbook = "playbook.yml"
  end

end
---
- hosts: all
  sudo: true
  tasks:

  - name: Update apt cache
    apt: update_cache=yes

  - name: Install ufw package
    apt: name=ufw

  - name: Setup ufw
    ufw: state=enabled policy=deny
 
  - name: Allow SSH in ufw
    ufw: rule=allow name=OpenSSH

  - name: Reboot server
    command: shutdown -r now

  - name: Display uptime 
    command: /usr/bin/uptime

And the final result:

Screen Shot 2016-06-07 at 17.14.57

Screen Shot 2016-06-07 at 17.15.55

Vagrant for DevOps table of contents