CRThaze
CRThaze

Reputation: 564

Vagrant: different provisioner for different machines

I'm trying to have my vagrant configuration run a different shell script for each machine in my Multi-Machine environment.

I have a definition for smartos as well as one for centos, however I want to run a different shell provider configuration for each, before running the same chef-solo provider configuration on both.

#!/usr/bin/env ruby

# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"

$smartos_script = <<-SHELL
echo "http://10.40.95.5" > /opt/local/etc/pkgin/repositories.conf
rm -rf /var/db/pkgin && pkgin -y update
SHELL

$centos_script = <<-SHELL
touch /opt/my_file
SHELL

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

  config.berkshelf.enabled = true
  config.ssh.forward_agent = true

  config.vm.define :smartos do |smartos|

    smartos.vm.box = "smartos"
    smartos.vm.box_url = 'http://dlc-int.openindiana.org/aszeszo/vagrant/smartos-base1310-64-virtualbox-20130806.box'
    smartos.vm.guest = :solaris

    config.vm.provision :shell do |shell|
      shell.inline = $smartos_script
    end

  end

  config.vm.define :centos do |centos|

    centos.vm.box = "centos"
    centos.vm.box_url = 'http://dlc-int.openindiana.org/aszeszo/vagrant/smartos-base1310-64-virtualbox-20130806.box'

    config.vm.provision :shell do |shell|
      shell.inline = $centos_script
    end

  end

  config.vm.provision :chef_solo do |chef|
    chef.add_recipe 'test'
  end

end

I have also tried using smartos.vm.provision instead of config, but have seen no difference.

Does anyone have any idea how I can do this?

Upvotes: 1

Views: 491

Answers (1)

cocheese
cocheese

Reputation: 512

You were on the right track with

I have also tried using smartos.vm.provision instead of config

Try this simple Vagrantfile out

$smartos_script = <<-SHELL
touch /opt/foo
SHELL

$centos_script = <<-SHELL
touch /opt/bar
SHELL

# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

  config.vm.define :smartos do |smartos|

    smartos.vm.box = "smartos"
    smartos.vm.box_url = 'http://dlc-int.openindiana.org/aszeszo/vagrant/smartos-base1310-64-virtualbox-20130806.box'


    smartos.vm.provision :shell do |shell|
      shell.inline = $smartos_script
    end

  end

  config.vm.define :centos do |centos|

    centos.vm.box = "centos"
    centos.vm.box_url = 'http://dlc-int.openindiana.org/aszeszo/vagrant/smartos-base1310-64-virtualbox-20130806.box'

    centos.vm.provision :shell do |shell|
      shell.inline = $centos_script
    end

  end

end

When you run "vagrant up" and ssh into a machine, e.g. vagrant ssh smartos and cd to /opt you will see that the file "foo" has been created. And when you ssh into to cents machine you see that the file "bar" is created.

Upvotes: 1

Related Questions