Last active
August 29, 2015 14:20
-
-
Save zh012/4a603330563d4551bcbd to your computer and use it in GitHub Desktop.
Vagrantfile dynamically loads multiple vm configures
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ##Vargantfile | |
| ```Ruby | |
| # -*- mode: ruby -*- | |
| # vi: set ft=ruby : | |
| # Vagrantfile API/syntax version. Don't touch unless you know what you're doing! | |
| VAGRANTFILE_API_VERSION = "2" | |
| Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| | |
| $config = config | |
| $project_name = File.basename(File.dirname(File.dirname(__FILE__))) | |
| config.vm.box = "ubuntu/trusty64" | |
| config.vm.synced_folder ".", "/vagrant", disabled: true | |
| config.ssh.forward_agent = true | |
| # HAX: fix ssh agent forwarding not being available during provisioning | |
| # see: https://github.com/mitchellh/vagrant/issues/1303 | |
| config.vm.provision :shell do |shell| | |
| shell.inline = "touch $1 && chmod 0440 $1 && echo $2 > $1" | |
| shell.args = %q{/etc/sudoers.d/root_ssh_agent "Defaults env_keep += \"SSH_AUTH_SOCK\""} | |
| end | |
| config.vm.provider "virtualbox" do |vb| | |
| # refer: https://www.virtualbox.org/manual/ch08.html#vboxmanage-modifyvm | |
| vb.customize [ | |
| "modifyvm", :id, | |
| "--cpus", 2, | |
| "--memory", 1024, | |
| ] | |
| end | |
| Dir.glob("vm_*.rb").each { |vm_conf| | |
| unless File.directory?(vm_conf) | |
| load vm_conf | |
| end | |
| } | |
| end | |
| ``` | |
| ##settings.rb | |
| ```Ruby | |
| module Settings | |
| ProjectName = File.basename(File.dirname(File.dirname(__FILE__))) | |
| end | |
| ``` | |
| ##vm configurations | |
| * vm_web.rb | |
| ```Ruby | |
| require_relative 'settings' | |
| project_name = Settings::ProjectName | |
| $config.vm.define "web" do |web| | |
| web.vm.hostname = "#{project_name}-web" | |
| web.vm.synced_folder ".", "/home/vagrant/#{project_name}" | |
| web.vm.network "private_network", ip: "192.168.21.10" | |
| web.vm.network "forwarded_port", guest: 8000, host: 8000 | |
| web.vm.provider "virtualbox" do |vb| | |
| vb.name = "#{project_name}-web" | |
| end | |
| end | |
| ``` | |
| * vm_db.rb | |
| ###... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment