tsh
tsh

Reputation: 947

vagrant default configuration

I have configs that must always be present in Vagrantfile, when init is run, regardless of box.
For example when I run vagrant init ubuntu/trusty64 or vagrant init centos/7 this must be present in both Vagrantfiles:

config.vm.provider "virtualbox" do |vb|
  vb.memory = 2048
 end

 config.vm.provision "shell", inline: <<-SHELL
   # obligatory provisioning goes here
 SHELL

Is there any way I can configure vagrant, so that when I run vagrant init this lines already be present in Vagrantfile?

Upvotes: 2

Views: 2670

Answers (2)

marskid
marskid

Reputation: 21

Try this https://www.vagrantup.com/docs/other/environmental-variables.html#vagrant_default_template

put export VAGRANT_DEFAULT_TEMPLATE=~/.vagrant.d/Vagrantfile.erb in ~/.bash_profile

touch ~/.vagrant.d/Vagrantfile.erb 

with content

Vagrant.configure("2") do |config|
  config.vm.define "main"
  config.vm.box = "<%= box_name %>"
  <% if box_version -%>
  config.vm.box_version = "<%= box_version %>"
  <% end -%>
  <% if box_url -%>
  config.vm.box_url = "<%= box_url %>"
  <% end -%>
  # config.vm.network "forwarded_port", guest: 80, host: 8080
  config.vm.network "private_network", type: "dhcp"
  config.vm.provider "virtualbox" do |vb|
    vb.memory = "4096"
    vb.cpus = 4
  end
end

then source ~/.bash_profile

vagrant init ubuntu/bionic64

It works great!

Upvotes: 2

Frederic Henri
Frederic Henri

Reputation: 53703

By default you cant really change this behavior, unfortunately vagrant init does not propose an option to use your own Vagrantfile (that would be neat but not there). There are 2 templates (see https://github.com/mitchellh/vagrant/tree/master/templates/commands/init) that can be used, the default one and a minimal one.

You're left with the following options:

  1. overwrite the default template Vagrantfile.erb

search the file in your system (on mac it should be somewhere in /opt/vagrant/embedded/gems/gems/vagrant-1.9.1/templates/commands/init/Vagrantfile.erb depending your version) and change it to what you need.

  1. You can have a global Vagrantfile under the .vagrant.d directory that will be applied to all your Vagrantfiles. See LOAD ORDER AND MERGING in vagrantfile documentation.

  2. Write a plugin with a new command to create the Vagrantfile. that is probably overkill though

Upvotes: 1

Related Questions