Skip to content

Instantly share code, notes, and snippets.

@budiantoip
Last active August 3, 2025 03:18
Show Gist options
  • Save budiantoip/25174e9f8bbdb9e39c00e075dac615c1 to your computer and use it in GitHub Desktop.
Save budiantoip/25174e9f8bbdb9e39c00e075dac615c1 to your computer and use it in GitHub Desktop.
Vagrant Template
Vagrant.configure("2") do |config|
    # Vagrant.require_version ">= 1.7"

    config.vm.box = "ubuntu/jammy64" 
    # config.vm.box_version = "4.3.8"

    # Install VirtualBox's Guest Additions CD image
    if Vagrant.has_plugin?("vagrant-vbguest") then
        # config.vbguest.auto_update = true
        # config.vbguest.no_remote = false   # allow downloading Guest Additions ISO
        
        # If the installation failed, go to https://download.virtualbox.org/virtualbox/
        # Then choose the version that matches your VirtualBox version
        # Then find and click VBoxGuestAdditions_7.1.6.iso
        # Once downloaded, copy to the same folder where the Vagrantfile is stored
    end

    if Vagrant.has_plugin?("vagrant-disksize") then
        config.disksize.size = '40GB'
    end
    

    # config.vm.network "private_network", type: "dhcp"

    config.vm.network "public_network", bridge: "en0: Wi-Fi", use_dhcp_assigned_default_route: true
    # config.vm.network "public_network", use_dhcp_assigned_default_route: true

    # config.vm.network "forwarded_port", guest: 3306, host: 3306
    # config.vm.network "forwarded_port", guest: 22, host: 22220

    config.vm.provision "file", source: "~/.ssh/id_rsa.pub", destination: "~/.ssh/me.pub"
    config.vm.provision "shell", inline: <<-SHELL
        cat /home/vagrant/.ssh/me.pub >> /home/vagrant/.ssh/authorized_keys
    SHELL
  
    # config.ssh.forward_agent = true
    # # config.vm.synced_folder ".", "/vagrant", :group=>'www-data'
    # config.vm.synced_folder ".", "/vagrant"

    # config.vm.disk :disk, name: "backup", size: "10GB"
    # config.vm.disk :dvd, name: "installer", file: "./VBoxGuestAdditions_7.1.12.iso"
    # config.vm.disk :floppy, name: "cool_files"

  
    config.vm.define "dns-client" do | dns_client |
        dns_client.vm.provider "virtualbox" do |m|
            m.name   = "dns-client"
            m.memory = 4096
            m.cpus   = 1
            # m.customize ["modifyvm", :id, "--audio", "none"]

            guest_additions_iso = File.expand_path("VBoxGuestAdditions_7.1.12.iso", __dir__)

            # You may need to run this first to install a third-party plugin
            # vagrant plugin install vagrant-persistent-storage
            # Get the storage controller by running VBoxManage showvminfo dns-client
            m.customize ["storageattach", :id,
                "--storagectl", "IDE",
                "--port", 1,
                "--device", 0,
                "--type", "dvddrive",
                "--medium", guest_additions_iso]
        end

        dns_client.hostmanager.aliases = %w(dns-client)
  
        dns_client.vm.hostname = "dns-client"
        dns_client.vm.provision :shell, path: "provisioners/setup.sh"
        config.vm.provision "shell", inline: "hostname dns-client && hostname > /etc/hostname", run: "always"
    end

    unless Vagrant.has_plugin?('vagrant-hostmanager')
        raise 'vagrant-hostmanager is not installed!'
    else
        config.hostmanager.enabled = true
        config.hostmanager.manage_host = true
        config.hostmanager.ignore_private_ip = false
        config.hostmanager.include_offline = true
        config.hostmanager.ip_resolver = proc do |vm, resolving_vm|
            if hostname = (vm.ssh_info && vm.ssh_info[:host])
                `vagrant ssh -c "hostname -I"`.split()[1]
            end
        end
    end
  
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment