How to set environment variables and /etc/hosts through Vagrantfile?

1.3k Views Asked by At

Vagrant version

Vagrant 1.9.7

Host operating system

Darwin bogon 16.6.0 Darwin Kernel Version 16.6.0: Fri Apr 14 16:21:16 PDT 2017; root:xnu-3789.60.24~6/RELEASE_X86_64 x86_64

Guest operating system

distributor ID: Ubuntu Description: Ubuntu 16.04.2 LTS Release: 16.04 Codename: xenial

Question

How to set environment variables and /etc/hosts through Vagrantfile?

In docker-composer, I can set environment and extra_hosts

In vagrant, how to do this?

1

There are 1 best solutions below

0
On

Vagrant manages Virtual Machines when used with Virtual Box, not a container.

If you want to have environment variable defined or specific files, the best is to use vagrant provisioner

You can push file directly using the file provisioner or have a shell script to edit the file and add some lines.

A simple shell script to add environment variable will be

  config.vm.provision "shell", privileged: false, inline: <<-SHELL
    echo "export VARIABLE=VALUE" > /home/vagrant/.profile
  SHELL

so the variable will be available when you ssh into the machine