Categories
DevOps

How to use specific Vagrantfile

Define the environment variable to use specific Vagrantfile.

Display Vagrantfiles in the current directory.

$ ls -l
total 16
-rw-r--r-- 1 milosz milosz 358 Aug 22 17:27 Vagrantfile-single-centos-box
-rw-r--r-- 1 milosz milosz 883 Aug 22 17:24 Vagrantfile-single-debian-box
-rw-r--r-- 1 milosz milosz 882 Aug 22 17:30 Vagrantfile-single-ubuntu-box
-rw-r--r-- 1 milosz milosz 956 Aug 22 17:24 Vagrantfile-three-debian-boxes

Define VAGRANT_VAGRANTFILE environment variable to use specific Vagrantfile during vagrant operations.

$ VAGRANT_VAGRANTFILE=Vagrantfile-single-debian-box vagrant status
Current machine states:
debian                    not created (virtualbox)
The environment has not yet been created. Run `vagrant up` to
create the environment. If a machine is not created, only the
default provider will be shown. So if a provider is not listed,
then the machine is not created for that environment.
$ VAGRANT_VAGRANTFILE=Vagrantfile-single-debian-box vagrant up
Bringing machine 'debian' up with 'virtualbox' provider...
==> debian: Importing base box 'debian/buster64'...
==> debian: Matching MAC address for NAT networking...
==> debian: Checking if box 'debian/buster64' version '10.4.0' is up to date...
==> debian: Setting the name of the VM: multiple_debian_1598110769599_8750
==> debian: Fixed port collision for 22 => 2222. Now on port 2201.
==> debian: Clearing any previously set network interfaces...
==> debian: Preparing network interfaces based on configuration...
    debian: Adapter 1: nat
    debian: Adapter 2: hostonly
==> debian: Forwarding ports...
    debian: 22 (guest) => 2201 (host) (adapter 1)
==> debian: Running 'pre-boot' VM customizations...
==> debian: Booting VM...
==> debian: Waiting for machine to boot. This may take a few minutes...
    debian: SSH address: 127.0.0.1:2201
    debian: SSH username: vagrant
    debian: SSH auth method: private key
    debian:
    debian: Vagrant insecure key detected. Vagrant will automatically replace
    debian: this with a newly generated keypair for better security.
    debian:
    debian: Inserting generated public key within guest...
    debian: Removing insecure key from the guest if it's present...
    debian: Key inserted! Disconnecting and reconnecting using new SSH key...
==> debian: Machine booted and ready!
==> debian: Checking for guest additions in VM...
    debian: The guest additions on this VM do not match the installed version of
    debian: VirtualBox! In most cases this is fine, but in rare cases it can
    debian: prevent things such as shared folders from working properly. If you see
    debian: shared folder errors, please make sure the guest additions within the
    debian: virtual machine match the version of VirtualBox you have installed on
    debian: your host and reload your VM.
    debian:
    debian: Guest Additions Version: 5.2.0 r68940
    debian: VirtualBox Version: 6.1
==> debian: Setting hostname...
==> debian: Configuring and enabling network interfaces...
==> debian: Configuring proxy for Apt...
==> debian: Configuring proxy environment variables...
==> debian: Installing rsync to the VM...
==> debian: Rsyncing folder: /home/milosz/Projekty/vagrantboxes/multiple/ => /vagrant
==> debian: Machine 'debian' has a post `vagrant up` message. This is a message
==> debian: from the creator of the Vagrantfile, and not from Vagrant itself:
==> debian:
==> debian: Vanilla Debian box. See https://app.vagrantup.com/debian for help and bug reports
$ VAGRANT_VAGRANTFILE=Vagrantfile-single-debian-box vagrant status
Current machine states:
debian                    running (virtualbox)
The VM is running. To stop this VM, you can run `vagrant halt` to
shut it down forcefully, or you can run `vagrant suspend` to simply
suspend the virtual machine. In either case, to restart it again,
simply run `vagrant up`.
$ VAGRANT_VAGRANTFILE=Vagrantfile-single-debian-box vagrant halt
==> debian: Attempting graceful shutdown of VM...
$ VAGRANT_VAGRANTFILE=Vagrantfile-single-debian-box vagrant destroy -f
==> debian: Destroying VM and associated drives...