当 init 运行时,无论盒子如何,我的配置都必须始终存在于 Vagrantfile 中。
例如,当我运行 vagrant init ubuntu/trusty64
或 vagrant init centos/7
时,这必须出现在两个 Vagrantfiles 中:
config.vm.provider "virtualbox" do |vb|
vb.memory = 2048
end
config.vm.provision "shell", inline: <<-SHELL
# obligatory provisioning goes here
SHELL
有什么方法可以配置 vagrant,以便当我运行 vagrant init
时,这行代码已经存在于 Vagrantfile 中?
最佳答案
试试这个 https://www.vagrantup.com/docs/other/environmental-variables.html#vagrant_default_template
将 export VAGRANT_DEFAULT_TEMPLATE=~/.vagrant.d/Vagrantfile.erb
放入 ~/.bash_profile
touch ~/.vagrant.d/Vagrantfile.erb
内容
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
然后source ~/.bash_profile
vagrant init ubuntu/bionic64
效果很好!
关于 Vagrant 默认配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42041419/