From 30bc9843c34f28623bad8d0ed24e05809fbbf161 Mon Sep 17 00:00:00 2001 From: Scott Lowe Date: Fri, 15 Jan 2016 23:36:07 -0700 Subject: [PATCH] Add initial set of files for using JSON data file with Vagrant Add JSON data file. Add modified Vagrantfile to read external JSON data file. --- vagrant-json/README.md | 25 ++++++++++++++ vagrant-json/Vagrantfile | 71 ++++++++++++++++++++++++++++++++++++++ vagrant-json/machines.json | 27 +++++++++++++++ 3 files changed, 123 insertions(+) create mode 100644 vagrant-json/README.md create mode 100644 vagrant-json/Vagrantfile create mode 100644 vagrant-json/machines.json diff --git a/vagrant-json/README.md b/vagrant-json/README.md new file mode 100644 index 0000000..7afe86d --- /dev/null +++ b/vagrant-json/README.md @@ -0,0 +1,25 @@ +# Generic Debian 8.0 ("Jessie") VM + +These files were created to allow users to use Vagrant ([http://www.vagrantup.com](http://www.vagrantup.com)) to quickly and easily spin up a generic Debian 8.0 ("Jessie") 64-bit VM. The configuration was tested using Vagrant 1.7.2, VMware Fusion 6.0.5, and the Vagrant VMware plugin. + +**NOTE:** There's really nothing special here; I created these files because I often had a need to quickly and easily spin up a generic Debian VM for some purpose (building a package or testing a command). I'm including them here just for the sake of completeness. + +## Contents + +* **README.md**: This file you're currently reading. + +* **servers.yml**: This YAML file contains a list of VM definitions and associated configuration data. It is referenced by `Vagrantfile` when Vagrant instantiates the VMs. + +* **Vagrantfile**: This file is used by Vagrant to spin up the virtual machines. This file is fairly extensively commented to help explain what's happening. You should be able to use this file unchanged; all the VM configuration options are stored outside this file. + +## Instructions + +These instructions assume you've already installed VMware Fusion, Vagrant, and the Vagrant VMware plugin. Please refer to the documentation for those products for more information on installation or configuration. + +1. Use `vagrant box add` to add a 64-bit Debian 8.0 ("Jessie") base box to be used by this `Vagrantfile`. You'll need to specify a box that provides support for the `vmware_desktop` provider (it should work with either VMware Workstation or VMware Fusion). + +2. Edit the `servers.yml` file to ensure the box you downloaded in step 1 is specified on the "box:" line of this file. + +3. Run `vagrant up`, and when the VM is up use `vagrant ssh` to access the generic Debian VM. + +Enjoy! diff --git a/vagrant-json/Vagrantfile b/vagrant-json/Vagrantfile new file mode 100644 index 0000000..818d368 --- /dev/null +++ b/vagrant-json/Vagrantfile @@ -0,0 +1,71 @@ +# -*- mode: ruby -*- +# vi: set ft=ruby : + +# Specify Vagrant version, Vagrant API version, and desired clone location +Vagrant.require_version '>= 1.6.0' +VAGRANTFILE_API_VERSION = '2' +ENV['VAGRANT_DEFAULT_PROVIDER'] = 'vmware_fusion' +ENV['VAGRANT_VMWARE_CLONE_DIRECTORY'] = '~/.vagrant' +#ENV['VAGRANT_DEFAULT_PROVIDER'] = 'virtualbox' +#ENV['VAGRANT_DEFAULT_PROVIDER'] = 'vmware_appcatalyst' + +# Require 'json' module +require 'json' + +# Read JSON file with VM details (name, box, RAM, CPU, and IP address) +machines = JSON.parse(File.read(File.join(File.dirname(__FILE__), 'machines.json'))) + +# Create and configure the VMs +Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| + + # Always use Vagrant's default insecure key + config.ssh.insert_key = false + + # Iterate through entries in YAML file to create VMs + machines.each do |machine| + config.vm.define machine['name'] do |srv| + + # Don't check for box updates + srv.vm.box_check_update = false + srv.vm.hostname = machine['name'] + srv.vm.box = machine['box'] + + # Configure default synced folder (disable by default) + if machine['sync_disabled'] != nil + srv.vm.synced_folder '.', '/vagrant', disabled: machine['sync_disabled'] + else + srv.vm.synced_folder '.', '/vagrant', disabled: true + end #if machine['sync_disabled'] + + # Assign additional private network + if machine['ip_addr'] != nil + srv.vm.network 'private_network', ip: machine['ip_addr'] + end # if machine['ip_addr'] + + # Configure CPU & RAM per settings in machines.yml (Fusion) + srv.vm.provider :vmware_fusion do |vmw| + vmw.vmx['memsize'] = machine['ram'] + vmw.vmx['numvcpus'] = machine['vcpu'] + if machine['nested'] == true + vmw.vmx['vhv.enable'] = 'TRUE' + end #if machine['nested'] + end # srv.vm.provider vmware_fusion + + # Configure CPU & RAM per settings in machines.yml (AppCatalyst) + srv.vm.provider :vmware_appcatalyst do |vmw| + vmw.vmx['memsize'] = machine['ram'] + vmw.vmx['numvcpus'] = machine['vcpu'] + #vmw.vmx['guestos'] = 'other3xlinux-64' + if machine['nested'] == true + vmw.vmx['vhv.enable'] = 'TRUE' + end #if machine['nested'] + end # srv.vm.provider vmware_appcatalyst + + # Configure CPU & RAM per settings in machines.yml (VirtualBox) + srv.vm.provider :virtualbox do |vb| + vb.memory = machine['ram'] + vb.cpus = machine['vcpu'] + end # srv.vm.provider virtualbox + end # config.vm.define + end # machines.each +end # Vagrant.configure diff --git a/vagrant-json/machines.json b/vagrant-json/machines.json new file mode 100644 index 0000000..d2109df --- /dev/null +++ b/vagrant-json/machines.json @@ -0,0 +1,27 @@ +[ + { + "name": "jessie", + "box": "slowe/debian-81-x64", + "ram": 512, + "vcpu": 1, + "ip_addr": "192.168.100.101", + "nested": false + }, + + { + "name": "trusty", + "box": "slowe/ubuntu-trusty-x64", + "ram": 512, + "vcpu": 1, + "ip_addr": "192.168.100.102", + "nested": true + }, + + { + "name": "precise", + "box": "slowe/ubuntu-precise-x64", + "ram": 512, + "vcpu": 1, + "ip_addr": "192.168.100.103" + } +]