devopsgroup-io/vagrant-digitalocean

Variable interpolation for machine image in loop

ticktockhouse opened this issue · 2 comments

Hello,

I'm trying to use a hashes with a loop with this plugin to specify both the name of the droplet and its base image, then use ansible to provision them. My code is:

# vim: ft=ruby

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

  cluster = {
    0 => { 
      'name' => 'backups', 
      'image' => 'ubuntu-16-04-x64' 
    },
    1 => {
      'name' => 'client1',
      'image' =>  'debian-9-x64'
    },
    2 => {
      'name' => 'client2',
      'image' =>  'debian-8-x64'
    },
    3 => {
      'name' => 'client3',
      'image' =>  'ubuntu-16-04-x64'
  }
 } 

  config.vm.provision "ansible" do |ansible|
    ansible.playbook = "playbook/site.yml"
    ansible.host_vars = {

    }
  end

  cluster.each do |i, machine|
    config.vm.define "#{machine['name']}" do |machine_id|
      machine_id.ssh.private_key_path = '~/.ssh/id_rsa.digitalocean'
      machine_id.vm.box = 'digital_ocean'

      machine_id.vm.provider :digital_ocean do |provider, override|
        override.vm.box_url = "https://github.com/devopsgroup-io/vagrant-digitalocean/raw/master/box/digital_ocean.box"
        provider.token = `echo $DO_API_TOKEN`
        provider.region = 'lon1'
        provider.size = 's-1vcpu-1gb'
        provider.image = "#{machine_id['image']}"
      end
    end
  end
end

...but in this case the value for provider.image must not interpolate properly. I admit this is probably down to a lack of Ruby knowledge, e.g what are the two values between the pipe symbols?

When I look at Ruby documentation on this, there is only one value here, and I get that it's used in the block between the "do" and the "end". I can't believe I'm the only person to have stumbled on this, but I can't find a decent solution online. Can anyone help?

Thanks

Jerry

napcs commented

@ticktockhouse I am doing this daily to provision DO machines at work. If you still need to know how to do this let me know and I can take some time to clean up something as an example.

Hello, you have machine_id instead of machine