Vagrant test and development environment II

vagrant

My Vagrant post of last week grew and grew and half way I decided to dedicate another post on it because we can do so much more.

This week we’re expanding on the basic Vagrantfile we’ve already created and adding more machine with a few lines of code.

Let’s continue!

So just te recap a little bit, this is in our current Vagrantfile:

vagrant.configure("2") do |config|
  config.vm.define "ns1" do |ns1|
    ns1.vm.box = "ubuntu/bionic64"
    ns1.dns.tld = "cloudpro.vm"
    ns1.vm.hostname = "ns1"
    ns1.vm.network "private_network", ip: "192.168.33.10", netmask: "24"
    ns1.vm.network "forwarded_port", host: "5353", guest: "53"
    ns1.vm.provision "shell" do |shell|
      shell.inline = "apt-get update -y && apt-get upgrade -y && apt-get install bind9 -y"
    end
  end
end

Let’s get our secondary DNS server in there, mail, web and database server. Edit your file like so:

Vagrant.configure("2") do |config|
  config.vm.define "ns1" do |ns1|
    ns1.vm.box = "ubuntu/bionic64"
    ns1.dns.tld = "cloudpro.vm"
    ns1.vm.hostname = "ns1"
    ns1.vm.network "private_network", ip: "192.168.33.10", netmask: "24"
    ns1.vm.network "forwarded_port", host: "5353", guest: "53"
    ns1.vm.provision "shell" do |shell|
      shell.inline = "apt-get update -y && apt-get upgrade -y && apt-get install bind9 -y"
    end
  end
  config.vm.define "ns2" do |ns2|
    ns2.vm.box = "ubuntu/bionic64"
    ns2.dns.tld = "cloudpro.vm"
    ns2.vm.hostname = "ns2"
    ns2.vm.network "private_network", ip: "192.168.33.11", netmask: "24"
    ns2.vm.network "forwarded_port", host: "5454", guest: "53"
    ns2.vm.provision "shell" do |shell|
      shell.inline = "apt-get update -y && apt-get upgrade -y && apt-get install bind9 -y"
    end
  end
  config.vm.define "mail" do |mail|
    mail.vm.box = "ubuntu/bionic64"
    mail.dns.tld = "cloudpro.vm"
    mail.vm.hostname = "mail"
    mail.vm.network "private_network", ip: "192.168.33.20", netmask: "24"
    mail.vm.network "forwarded_port", host: "2525", guest: "25"
    mail.vm.network "forwarded_port", host: "1465", guest: "465"
    mail.vm.provision "shell" do |shell|
      shell.inline = "apt-get update -y && apt-get upgrade -y && DEBIAN_FRONTEND=noninteractive apt-get install postfix -y"
    end
  end
  config.vm.define "web" do |web|
    web.vm.box = "ubuntu/bionic64"
    web.dns.tld = "cloudpro.vm"
    web.vm.hostname = "web"
    web.vm.network "private_network", ip: "192.168.33.21", netmask: "24"
    web.vm.network "forwarded_port", host: "8080", guest: "80"
    web.vm.network "forwarded_port", host: "4443", guest: "443"
    web.vm.provision "shell" do |shell|
      shell.inline = "apt-get update -y && apt-get upgrade -y && apt-get install nginx -y"
    end
  end
  config.vm.define "db" do |db|
    db.vm.box = "ubuntu/bionic64"
    db.dns.tld = "cloudpro.vm"
    db.vm.hostname = "db"
    db.vm.network "private_network", ip: "192.168.33.22", netmask: "24"
    db.vm.network "forwarded_port", host: "13306", guest: "3306"
    db.vm.provision "shell" do |script|
      script.path = "update.sh"
    end
    db.vm.provision "shell" do |mysql|
      mysql.inline = "apt-get install mysql-server -y"
    end
  end
end

Before launching them, you might want to halt and/or destroy the single one you had already created before:

$ vagrant halt
$ vagrant destroy

Fire them up all at once with:

$ vagrant up

If you would like te reprovision the one you already had, don’t forget the appropriate flag:

$ vagrant up --provision

Within a couple of minutes, you’ve got 5 machines up and running that you can configure and finetune to do what you want. Mind you though that you should have enough resources (CPU, RAM, i.e.) to handle these virtual machines.

Check the status:

$ vagrant status

Now that we have multiple machines in one Vagrant config, we can also send commands to an individual machine instead of all of them at once. A few vagrant command examples:

$ vagrant halt ns2
$ vagrant reload mail
$ vagrant reload web --provision
$ vagrant up ns1
$ vagrant status ns1
$ vagrant destroy ns2

And so on.

I want to discus one more item and that is the shell provisioner. As you can see, every machine has got his own inline shell commands. What might be just as easy, is to use a shell script. Have a look at all the different possibilities here.

For our example, I’m just creating and running the most simple script possible and combining it with a shell inline command. In your vagrant directory, create an update.sh. Contents:

#!/bin/bash
apt-get update -y
apt-get upgrade -y

Make it executable:

$ chmod 750 update.sh

And use it in your Vagrantfile. I’m grabbing the db machine for this example:

  config.vm.define "db" do |db|
    db.vm.box = "ubuntu/bionic64"
    db.dns.tld = "cloudpro.vm"
    db.vm.hostname = "db"
    db.vm.network "private_network", ip: "192.168.33.22", netmask: "24"
    db.vm.network "forwarded_port", host: "13306", guest: "3306"
    db.vm.provision "shell" do |script|
      script.path = "update.sh"
    end
    db.vm.provision "shell" do |mysql|
      mysql.inline = "apt-get install mysql-server -y"
    end
  end

Test your changes:

$ vagrant reload db --provision

Or start over:

$ vagrant halt db
$ vagrant destroy db -f
$ vagrant up db --provision

It’s that easy and possibilities are virtually endless. Just fool around with it a bit and be sure to check out the other provisioners.

Update: Check out this mini vagrant series here and here.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.