Vagrantfile

Pada tulisan sebelumnya saya menulis tentang bagaimana cara memasang Vagrant dan dalam tulisan kali ini saya akan membahas tentang Vagrantfile. Vagrantfile ini sendiri adalah berkas yang bertugas untuk konfigurasi-konfigurasi terkait vagrant box yang dipasang.

Pada tulisan kali ini saya menggunakan Vagrantfile dari box debian yang sudah saya pasang beberapa hari lalu. Isi standar dari Vagrantfile debian seperti ini :

# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
  # The most common configuration options are documented and commented below.
  # For a complete reference, please see the online documentation at
  # https://docs.vagrantup.com.

  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://atlas.hashicorp.com/search.
  config.vm.box = "debian/jessie64"

  # Disable automatic box update checking. If you disable this, then
  # boxes will only be checked for updates when the user runs
  # `vagrant box outdated`. This is not recommended.
  # config.vm.box_check_update = false

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine. In the example below,
  # accessing "localhost:8080" will access port 80 on the guest machine.
  # config.vm.network "forwarded_port", guest: 80, host: 8080

  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  # config.vm.network "private_network", ip: "192.168.77.10"

  # Create a public network, which generally matched to bridged network.
  # Bridged networks make the machine appear as another physical device on
  # your network.
  # config.vm.network "public_network"

  # Share an additional folder to the guest VM. The first argument is
  # the path on the host to the actual folder. The second argument is
  # the path on the guest to mount the folder. And the optional third
  # argument is a set of non-required options.
  # config.vm.synced_folder "../data", "/vagrant_data"

  # Provider-specific configuration so you can fine-tune various
  # backing providers for Vagrant. These expose provider-specific options.
  # Example for VirtualBox:
  #
  #  config.vm.provider "virtualbox" do |vb|
  #   # Display the VirtualBox GUI when booting the machine
  #   vb.gui = true
  #
  #   # Customize the amount of memory on the VM:
  #   vb.memory = "512"
  # end
  #
  # View the documentation for the provider you are using for more
  # information on available options.

  # Define a Vagrant Push strategy for pushing to Atlas. Other push strategies
  # such as FTP and Heroku are also available. See the documentation at
  # https://docs.vagrantup.com/v2/push/atlas.html for more information.
  # config.push.define "atlas" do |push|
  #   push.app = "YOUR_ATLAS_USERNAME/YOUR_APPLICATION_NAME"
  # end

  # Enable provisioning with a shell script. Additional provisioners such as
  # Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
  # documentation for more information about their specific syntax and use.
  # config.vm.provision "shell", inline: <<-SHELL
  #   apt-get update
  #   apt-get install -y apache2
  # SHELL
end

Tentu saya tidak akan membahas semuanya karena toh saya jarang juga menggunakan yang rumit-rumit, di sini saya akan membahas mengenai beberapa hal saja, seperti : Konfigurasi IP, Konfigurasi folder sinkron, dan konfigurasi RAM yang dipakai.

IP

Untuk mengatur alamat IP pada vagrant box yang sedang digunakan adalah, buka kembali Vagrantfile dan cari tulisan :

#  config.vm.network "private_network", ip: "192.168.77.10"

Ubah menjadi

config.vm.network "private_network", ip: "192.168.77.10"

Simpan dan nyalakan Vagrant dengan perintah Vagrant up. Lalu coba ping ke alamat tersebut. Jika berhasil maka akan menghasilkan seperti ini

$ ping 192.168.77.10

PING 192.168.77.10 (192.168.77.10) 56(84) bytes of data.
64 bytes from 192.168.77.10: icmp_seq=1 ttl=64 time=1.24 ms
64 bytes from 192.168.77.10: icmp_seq=2 ttl=64 time=0.529 ms
64 bytes from 192.168.77.10: icmp_seq=3 ttl=64 time=0.529 ms
64 bytes from 192.168.77.10: icmp_seq=4 ttl=64 time=0.627 ms
64 bytes from 192.168.77.10: icmp_seq=5 ttl=64 time=0.319 ms
^C
--- 192.168.77.10 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4002ms
rtt min/avg/max/mdev = 0.319/0.650/1.249/0.316 ms

Folder Sinkron

Folder sinkron ini berguna agar folder yang di lokal mesin bisa tersambung langsung ke folder yang ada di vagrant mesin. Pertama masuk dahulu ke vagrant mesin untuk menyiapkan folder yang akan digunakan sebagai folder sinkron

vagrant ssh

$ mkdir /home/vagrant/share

exit

Lalu buka kembali Vagrantfile dan cari baris berikut

# config.vm.synced_folder "../data", "/vagrant_data"

Ubah menjadi

config.vm.synced_folder "/var/www/html/share-folder", "/home/vagrant/share"

Dimana bagian path yang pertama merupakan path dari folder yang berada di lokal mesin, dan path yang kedua adalah path yang ada di vagrant mesin. Jika vagrant sebelumnya sedang menyala matikan terlebih dahulu dengan perintah vagrant halt setelah itu nyalakan kembali dengan vagrant up.

Jika menemukan error

Vagrant was unable to mount VirtualBox shared folders. This is usually
because the filesystem "vboxsf" is not available. This filesystem is
made available via the VirtualBox Guest Additions and kernel module.
Please verify that these guest additions are properly installed in the
guest. This is not a bug in Vagrant and is usually caused by a faulty
Vagrant box. For context, the command attempted was:

mount -t vboxsf -o uid=1000,gid=1000 home_vagrant_share /home/vagrant/share

The error output from the command was:

mount: unknown filesystem type 'vboxsf'

Gunakan perintah ini menyelesaikannya.

vagrant plugin install vagrant-vbguest

Setelah selesai masuk kembali ke vagrant mesin dengan mengetikan vagrant ssh. Untuk mengetes folder sudah tersinkron dengan baik tambahkan file apapun di folder lokal dan cek di folder vagrant mesin. Jika tersambung dengan benar file yang ada di lokal akan muncul di vagrant mesin.

Mengatur RAM

Secara default vagrant akan menyiapkan RAM sebesar 512, bisa dilihat di virtualbox jika tidak percaya.

Untuk menambahkan RAM buka kembali Vagrantfile dan cari bagian berikut

# config.vm.provider "virtualbox" do |vb|
  #   # Display the VirtualBox GUI when booting the machine
  #   vb.gui = true
  #
  #   # Customize the amount of memory on the VM:
  #   vb.memory = "512"
  # end

Ubah menjadi

config.vm.provider "virtualbox" do |vb|
  #   # Display the VirtualBox GUI when booting the machine
  #   vb.gui = true
  #
  #   # Customize the amount of memory on the VM:
     vb.memory = "1024"
   end

Matikan dan nyalakan lagi vagrant seperti sebelumnya. Maka jika dicek kembali RAM sudah akan bertambah. Namun perlu diingat jangan tambahkan RAM melibehi RAM asli yang ada di lokal komputer.

Sebenarnya masih ada beberapa konfigurasi lainnya seperti public_networkport, dan lainnya tapi berhubung saya hanya fokus ke tiga hal tadi jadi saya haya bisa berbagi hal aygn sudah saya kerjakan saja.

Show Comments