Search code examples
environment-variablesvagrantpuppetenv

Vagrant-env plugin throwing error - line "VAR_NAME=var_value" doesn't match format


I have ruby 2.1.5 installed on Ubuntu with Vagrant 1.7

I ran:

~$ vagrant plugin install vagrant-env

as described at gosuri/vagrant-env.

I should also mention that this now only works when I add:

require 'dotenv'
Dotenv.load

which the doc (README) does not specify to implement into the Vagrantfile. If I leave this out then I get an error saying that the line with:

config.env.enable

is an unknown configuration section.

I have my .env file in the same directory as Vagrantfile. No matter what I have in the file, it errors on the first line. I can leave it blank or make it a comment and it will still say the first line is incorrect format.

I am wanting to use this because it is the only solution I can find that will allow me to pass ENV variables to the Vagrant file as well as the manifest files Puppet.

Here is a snippet of my Vagrantfile

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

require 'dotenv'
Dotenv.load

Vagrant.configure("2") do |config|
  config.env.enable
  #config.dotenv.enabled = true
  config.vm.provider "virtualbox" do |vb|
    vb.customize ["modifyvm", :id, "--memory", "#{ENV['MEMORY_ALLOCATION']}"]
  end
  config.vm.network "public_network", ip: ENV['PUBLIC_IP']
end

Vagrant::Config.run do |config|
  ...
  config.vm.define "devboxserver" do |app|
    ...
    app.vm.provision :puppet do |puppet|
      puppet.facter = {
        "env" => ENV
      }

      puppet.module_path = "puppet/modules"
      puppet.manifests_path = "puppet/manifests"
      puppet.manifest_file = "site.pp"
      puppet.options="--verbose --debug"
    end
  end
end

As you can see, I also tried vagrant-dotenv (which you see is commented out) but that was also causing me problems as it was trying to call on config variables before the Vagrantfile was even loaded. But I guess that is another issue. I had to remove that gem from the .vagrant.d directory.

But getting back to the vagrant-env issue, I wrote my .env file as with what I could tell from the github plugin site and the referenced article

USER_NAME=jason
PUBLIC_IP=192.168.1.00
MEMORY_ALLOCATION=1024
GITREPO_ACCOUNT_URL=https://github.com/me
GITREPO_URL=https://github.com/me/my_app
POSTGRES_PASSWORD=secret-pass
DEV_DB_NAME=my_app_development
PROD_DB_NAME=my_app_production
TEST_DB_NAME=my_app_test

And the error I get is:

jason@host:~/folder/vagrant$ vagrant up
There is a syntax error in the following Vagrantfile. The syntax error
message is reproduced below for convenience:

Line "USER_NAME=jason" doesn't match format

Solution

  • Since the goal is to create environmental variables throughout my vagrant execution and share with Puppet, I did what any sane rubyist (or programmer) would do. I created my own simple hack by grabbing the .env variables and turning them into a hash.

    I added this hack to the top of my Vagrant file:

    # -*- mode: ruby -*-
    # vi: set ft=ruby :
    
    env = {}
    File.read(".env").split("\n").each do |ef|
      env[ef.split("=")[0]] = ef.split("=")[1]
    end
    
    Vagrant.configure("2") do |config|
      config.vm.provider "virtualbox" do |vb|
        vb.customize ["modifyvm", :id, "--memory", env['MEMORY_ALLOCATION']]
      end
      config.vm.network "public_network", ip: env['PUBLIC_IP']
    end
    ...
    

    However, since the Puppet files aren't really ruby files, using puppet.facter to grab to set the "env" variable ($env) to a hash does not work since we are talking about two different language constructs.

    Instead, I just extended the puppet.facter list and instead of one $env hash I now have a puppet.facter variable set for all the .env variables.

    ...
    app.vm.provision :puppet do |puppet|
      puppet.facter = {
        "fqdn"                => "#{env['BOX_NAME']}.local",
        "user_name"           => env['USER_NAME'],
        "box_name"            => env['BOX_NAME'],
        "gitrepo_account_url" => env['GITREPO_ACCOUNT_URL'],
        "gitrepo_url"         => env['GITREPO_URL'],
        "postgres_password"   => env['POSTGRES_PASSWORD'],
        "dev_db_name"         => env['DEV_DB_NAME'],
        "prod_db_name"        => env['PROD_DB_NAME'],
        "test_db_name"        => env['TEST_DB_NAME'],
        "my_email"            => env['MY_EMAIL']
      }
    
      puppet.module_path = "puppet/modules"
      puppet.manifests_path = "puppet/manifests"
      puppet.manifest_file = "site.pp"
      puppet.options="--verbose --debug"
    end
    ...
    

    In my puppet scripts I just call variables like normal. EX: $user_name, $dev_db_name, etc.

    I then removed vagrant-env as it is no longer needed.