Skip to content

Instantly share code, notes, and snippets.

@bartmeuris
Last active December 19, 2015 12:29
Show Gist options
  • Save bartmeuris/5955728 to your computer and use it in GitHub Desktop.
Save bartmeuris/5955728 to your computer and use it in GitHub Desktop.
Load JSON from a file in a Vagrant config file while defining a default structure. The file is created if it doesn't exist containing the specified default structure. No error checking is performed.
# -*- mode: ruby -*-
# vi: set ft=ruby :
require 'json'
def load_json_default(filename, defaultstruct)
if File.exist?(filename)
json = File.read(filename)
data = JSON.parse(json)
else
data = defaultstruct
File.open(filename, "w") do |f|
f.write(JSON.pretty_generate(data))
end
end
return data
end
Vagrant.configure("2") do |config|
config.vm.box = "precise64"
config.vm.box_url = "http://files.vagrantup.com/precise64.box"
config.vm.provider :virtualbox do |vb|
vb.customize ["modifyvm", :id, "--memory", "256"]
end
config.vm.provision :chef_solo do |chef|
chef.add_recipe "test"
chef.json = load_json_default("settings.json",
{
'test' => {
'testuser' => 'my_user'
}
}
)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment