Vagrant file with multi OS machines?

249 Views Asked by At

I've being playing around with Vagrant for awhile and everything has been as expected. It just work. But now when I tried to create few Linux and few Windows nodes on same file, the problems started. Normally it's with provisioning, either Linux is tried to be contacted with winrm or winbox if telling me that apt is not something PowerShell can do. These boxes are within their own

  • Vagrant.configure("2") do |config|
  • Vagrant.configure("2") do |windows|

Etc.

Any ideas how to tackle this?

1

There are 1 best solutions below

2
Frederic Henri On

It seems you're confusing Vagrant configuration (with version) and [Vagrant VM definition](https://www.vagrantup.com/docs/multi-machine#defining-multiple-machines]

you will have 1 configuration for your Vagrantfile and multiple VM definition.

here is a sample I have been using in the past of 4 VM (3 are centos based and 1 Windows VM)

# -*- mode: ruby -*-
# vi: set ft=ruby :

require 'yaml'
settings = YAML.load_file File.join(File.dirname(__FILE__), "puppet/hieradata/common.yaml")

selenium_version = settings['selenium_version']

Vagrant.configure("2") do |config|

  config.vm.box = settings['host_box'] || "pws/centos65"
  config.ssh.username = settings['ariba_user']

  config.vm.define "db" do |db|
    db.vm.hostname = settings['db_hostname']
    db.vm.network "private_network", ip: settings['host_db_address']

    db.vm.synced_folder "dump/", "/dump"

    db.vm.provider "vmware_workstation" do |vm|
      vm.vmx["memsize"] = "3072"
    end

    db.vm.provider "virtualbox" do |vb|
      vb.memory = "3072"
    end

    db.vm.provision "shell", path: "puppet/script/install-puppet-modules-db.sh"
    db.vm.provision :puppet do |puppet|
      puppet.manifests_path = "puppet/manifests"
      puppet.manifest_file = "base-db.pp"
      puppet.module_path = "puppet/modules/db"
      puppet.hiera_config_path = "puppet/hiera.yaml"
      #puppet.options = "--verbose --debug"
    end
  end

  config.vm.define "app", primary: true do |app|
    app.vm.hostname = settings['ariba_hostname']
    app.vm.network "private_network", ip: settings['host_app_address']
    app.vm.synced_folder "puppet/install_ariba", "/home/ariba/install_sources"

    app.ssh.forward_agent = true
    app.ssh.forward_x11 = true

    app.vm.provider "vmware_workstation" do |vm|
      vm.vmx["memsize"] = "4096"
    end

    app.vm.provider "virtualbox" do |vb|
      vb.memory = "3072"
    end

    app.vm.provision "shell", path: "puppet/script/install-puppet-modules-app.sh"
    app.vm.provision :puppet do |puppet|
      puppet.manifests_path = "puppet/manifests"
      puppet.manifest_file = "base-app.pp"
      puppet.module_path = "puppet/modules"
      puppet.hiera_config_path = "puppet/hiera.yaml"
      #puppet.options = "--verbose --debug"
    end
    app.vm.provision "shell", path: "puppet/script/run-ariba-app.sh", privileged: false, run: 'always'
  end

  config.vm.define "win_10" do |win10|
    win10.vm.box = "windows_10"
    
    win10.vm.synced_folder "puppet/install_ariba/test", "/test"

    win10.vm.provision "shell", path: "puppet/install_ariba/test/install_win_jdk.ps1", privileged: false
    win10.vm.provision "shell", path: "puppet/install_ariba/test/install_browsers.ps1", privileged: false

    win10.vm.provision "shell", path: "puppet/install_ariba/test/start_win_selenium.bat", run: 'always', args: ["#{selenium_version}", settings['host_hub_address']]
  end

  config.vm.define "hub" do |hub|
    hub.vm.hostname = settings['hub_hostname']
    hub.vm.network "private_network", ip: settings['host_hub_address']

    hub.vm.provider "vmware_workstation" do |vm|
      vm.vmx["memsize"] = "1024"
    end

    hub.vm.provider "virtualbox" do |vb|
      vb.memory = "3072"
    end

    hub.vm.synced_folder "puppet/install_ariba/test", "/test"

    hub.vm.provision "shell", path: "puppet/script/install-puppet-modules-hub.sh"
    hub.vm.provision :puppet do |puppet|
      puppet.manifests_path = "puppet/manifests"
      puppet.manifest_file = "base-hub.pp"
      puppet.module_path = "puppet/modules/hub"
      puppet.hiera_config_path = "puppet/hiera.yaml"
      #puppet.options = "--verbose --debug"
    end
    hub.vm.provision "shell", path: "puppet/script/run-test.sh", privileged: false, run: 'always', args: "#{selenium_version}"
  end
end

Linux provisioning is done through shell script or puppet and windows provisioning is done with PowerShell script or bat file. Pay attention how VM are defined and how we use variable name in their corresponding block.

As mentioned in the Vagrant doc

For POSIX-like machines, the shell provisioner executes scripts with SSH. For Windows guest machines that are configured to use WinRM, the shell provisioner executes PowerShell and Batch scripts over WinRM.