Skip to content

Instantly share code, notes, and snippets.

@radicaldingos
Last active April 10, 2024 15:13
Show Gist options
  • Save radicaldingos/504d9db89b1ea89774b1 to your computer and use it in GitHub Desktop.
Save radicaldingos/504d9db89b1ea89774b1 to your computer and use it in GitHub Desktop.
Example of Vagrantfile with config stored in an external YAML file. It looks for a "vagrant.yml.dist" file, which can be overloaded by a "vagrant.yml".
# encoding: utf-8
# -*- mode: ruby -*-
# vi: set ft=ruby :
require 'yaml'
# Load settings from vagrant.yml or vagrant.yml.dist
current_dir = File.dirname(File.expand_path(__FILE__))
if File.file?("#{current_dir}/vagrant.yml")
config_file = YAML.load_file("#{current_dir}/vagrant.yml")
else
config_file = YAML.load_file("#{current_dir}/vagrant.yml.dist")
end
settings = config_file['configs'][config_file['configs']['use']]
# Deploy vagrant box
Vagrant.configure(2) do |config|
config.vm.box = settings['box_name']
config.vm.box_url = settings['box_url']
#config.ssh.private_key_path = "~/.ssh/authorized_keys"
# config.vm.network "forwarded_port", guest: 80, host: 8080
config.vm.network "private_network", ip: settings['vm_ip']
config.vm.synced_folder ".", settings['vm_project_path'], create: true, owner: "vagrant", group: "vagrant", mount_options: ["dmode=775", "fmode=774"]
config.vm.provider "virtualbox" do |vb|
vb.name = settings['vm_name']
vb.memory = settings['vm_memory']
vb.cpus = settings['vm_cpus']
end
# Shell provisioning
config.vm.provision "shell" do |s|
s.path = "bin/provisioning.sh"
s.args = [ settings['vm_project_path'], settings['vm_db_name'], settings['vm_db_user'] ]
s.privileged = true
end
end
@the-yaz
Copy link

the-yaz commented Nov 23, 2022

Can you explain this line : settings = config_file['configs'][config_file['configs']['use']]

@radicaldingos
Copy link
Author

radicaldingos commented Dec 10, 2022

It's the line which determine the selected configuration in a YAML with the following format:

configs:
  use: default
  default:
    box_name: Default box
    box_url: ...
    ...
  other:
    box_name: Other box
    box_url: ...

You can adapt it with your specific configuration format.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment