Vagrantfile utilizing an Environmental variable to define the machine name fails, after successfully building the VM

I’m currently working with Vagrant, and have been for several months now. While I’m still new to Vagrant, I haven’t had any issues creating VM’s up until now.

What I would like to do is use the systems, Linux, environment variables to pass the value of the VM name. I will be writing a script will launch multiple vm’s from the same Vagrantfile. I’m not sure as to exactly how I’m going to do this yet, possibly a loop within the Vagrantfile with different parameters for each VM that I’m going to launch, but I digress.

Let me show you what works, and what doesn’t

Vagrantfile - (works)

     # -*- mode: ruby -*-
     # vi: set ft=ruby :
     
     Vagrant.configure("2") do |config|
     
       config.vm.define 'simple' do |h|
         h.vm.box = "ubuntu/trusty64"
       end
     end

Not an issue, builds the VM with the defined machined name and I’m able to access it with no issues. either with vagrant ssh or vagrant ssh simple

Vagrantfile - (works)

    # -*- mode: ruby -*-
    # vi: set ft=ruby :
    
    hostname = 'test'
    
    Vagrant.configure("2") do |config|
    
      config.vm.define hostname do |h|
        h.vm.box = "ubuntu/trusty64"
      end
    end

Again works with no issues. I’m able to create a variable and assign a string, which I then use to define the machine name. ssh, halt, destroy, etc all work with no issues.

 `Vagrantfile` - (fails, sort of)
 
     # -*- mode: ruby -*-
     # vi: set ft=ruby :
     
     hostname =  ENV['HOSTNAME']
     
     Vagrant.configure("2") do |config|
     
       config.vm.define hostname do |h|
         h.vm.box = "ubuntu/trusty64"
       end
     end

terminal

HOSTNAME=testVM vagrant up

The VM builds just fine, as far as I can see. There are no errors on the terminal itself. Where I start having an issue when I attempt to do anything after this point.

vagrant ssh
    vagrant ssh testVM
    vagrant halt
    vagrant destroy
    vagrant global-status
    etc

It doesn’t matter which of the commands I try, they all result in the same message.

Vagrant failed to initialize at a very early stage:
    
    There was an error loading a Vagrantfile. The file being loaded
    and the error message are shown below. This is usually caused by
    an invalid or undefined variable.
    
    Path: /opt/vagrant/embedded/gems/2.2.19/gems/vagrant-2.2.19/plugins/kernel_v2/config/vm.rb
    Line number: 0
    Message: undefined method `to_sym'

I haven’t been able to find a solution. I did find one option which was to repair the plugin with

vagrant plugin repair
vagrant plugin update

The message does say something along the lines of a undefined variable. WhichI assume its referencing the ‘hostname’. However, I don’t understand how this works. Even vagrant destroy which references the Vagrantfile itself, still shows the message, which ‘hostname’ is defined within the Vagrantfile.

Does anyone have any insight?