nsidc / vagrant-vsphere

VMware vSphere provider for Vagrant
Other
608 stars 166 forks source link

How to define multi-machine vagrantfile using different vm templates? #276

Closed MystaMax closed 5 years ago

MystaMax commented 5 years ago

Hello,

I'm trying to build a 3 node cluster of VMs, two of them are Ubuntu and last being CentOS. I have VM templates for both. I can build a multi-machine environment using the same template, but not with different templates.

I tried using the block of code below, to no avail:

Vagrant.configure("2") do |config|
  # General Vagrant VM configuration
    config.vm.box = 'dummy'
    config.vm.box_url = '/home/$user/vagrant/vsphere/dummy.box'
    config.ssh.insert_key = false

    config.vm.provider :vsphere do |vsphere, override|
      override.nfs.functional = false  
      vsphere.host = 'vcsa01.host.net'
      vsphere.user = '$user@vsphere.host.net'
      vsphere.password = '$password'
      vsphere.insecure = true
      vsphere.compute_resource_name = 'Cluster01'
      # vsphere.resource_pool_name = 'YOUR RESOURCE POOL'
      vsphere.template_name = 'vagrant/vagrant-tmpl/vagrant-centos7'
      # vsphere.template_name = 'vagrant/vagrant-tmpl/vagrant-ubuntu1804'
      vsphere.vm_base_path = 'vagrant/vagrant-vms'
      vsphere.linked_clone = true
      # vsphere.name = 'vc7'
    end

    # Application server 1.
    config.vm.define "app1" do |app|
      app.vm.hostname = "orc-app1.test"
      app.vm.template_name = 'vagrant/vagrant-tmpl/vagrant-ubuntu1804'
    end
    # Application server 2.
    config.vm.define "app2" do |app|
      app.vm.hostname = "orc-app2.test"
      app.vm.template_name = 'vagrant/vagrant-tmpl/vagrant-centos7'
    end      
  end

Is this possible?

DarK501 commented 5 years ago

Yes, however config.vm.provider is a child of config.vm so it needs to be configured after your VM define statement.

config.vm.define "app1" do |app|
    [...]
    config.vm.provider :vsphere do |vsphere, override|
       [...]
   end
end

This model allows a single configuration to be provisioned by multiple providers, allowing portability.

Remember that the Vagrant file is a parsed ruby file, so anything that goes in ruby should apply to the Vagrant file

MystaMax commented 5 years ago

Thank you very much, @DarK501! Your comments helped me get where I needed to be. Below is a working configuration for others to use:

Vagrant.configure("2") do |config|
    # General Vagrant VM configuration
      config.vm.box = 'dummy'
      config.vm.box_url = '/home/$user/vagrant/vsphere/dummy.box'
      config.ssh.insert_key = false

      #App 1 Definition - CentOS
      config.vm.define "db01" do |app01|
        app01.vm.hostname = "apptest01.host.net"
        app01.vm.provider :vsphere do |vsphere, override|
          override.nfs.functional = false  
          vsphere.host = 'vcsa01.host.net'
          vsphere.user = '$user@vsphere.host.net'
          vsphere.password = '$password'
          vsphere.insecure = true
          vsphere.compute_resource_name = 'Cluster01'
          vsphere.template_name = 'vagrant/vagrant-tmpl/vagrant-centos7'
          # vsphere.template_name = 'vagrant/vagrant-tmpl/vagrant-ubuntu1804'
          vsphere.vm_base_path = 'vagrant/vagrant-vms'
          vsphere.linked_clone = true
        end
      end

      #App 2 Definition - Ubuntu
      config.vm.define "app01" do |app02|
        app02.vm.hostname = "apptest02.host.net"
        app02.vm.provider :vsphere do |vsphere, override|
          override.nfs.functional = false  
          vsphere.host = 'vcsa01.host.net'
          vsphere.user = '$user@vsphere.host.net'
          vsphere.password = '$password'
          vsphere.insecure = true
          vsphere.compute_resource_name = 'Cluster01'
          # vsphere.template_name = 'vagrant/vagrant-tmpl/vagrant-centos7'
          vsphere.template_name = 'vagrant/vagrant-tmpl/vagrant-ubuntu1804'
          vsphere.vm_base_path = 'vagrant/vagrant-vms'
          vsphere.linked_clone = true
        end
      end

      #App 3 Definition - Ubuntu
      config.vm.define "app02" do |app03|
        app03.vm.hostname = "apptest02.host.net"
        app03.vm.provider :vsphere do |vsphere, override|
          override.nfs.functional = false  
          vsphere.host = 'vcsa01.host.net'
          vsphere.user = '$user@vsphere.host.net'
          vsphere.password = '$password'
          vsphere.insecure = true
          vsphere.compute_resource_name = 'Cluster01'
          # vsphere.template_name = 'vagrant/vagrant-tmpl/vagrant-centos7'
          vsphere.template_name = 'vagrant/vagrant-tmpl/vagrant-ubuntu1804'
          vsphere.vm_base_path = 'vagrant/vagrant-vms'
          vsphere.linked_clone = true
        end
      end

end