1

I am using kitchen to test my chef cookbook. In .kitchen.yml I use

driver:
  name: vagrant
  vagrantfile_erb: Vagrantfile.rb

Then in the suites attribute I have

attributes: {
  "myattribute" : true,
 ...

Can I use this attribute value inside the Vagrantfile.rb? I need to run the Vagrantfile with both Kitchen and directly with Vagrant, so I need a way to distinguish between the two methods. I wanted to set a boolean attribute in kitchen like this, but it does not work

Vagrant.configure('2') do |config|
  myattribute = "<%= node['myattribute'] %>
  ...
end

I get the message

Message: Failed to complete #create action: [undefined local variable or method `node' for #<Kitchen::Driver::Vagrant:0x007febf431d138>]

Of course an easy solution would be to create 2 different vagrantfile, I just wanted to avoid that

MarMan
  • 328
  • 2
  • 3
  • 9

2 Answers2

0

According to the according README section of kitchen-vagrant, you should be able to access the attributes in the following way:

instance.provisioner[:attributes]

So I expect that you can access your myattribute using

Vagrant.configure('2') do |config|
  myattribute = instance.provisioner[:attributes][:myattribute]
  ...
end

However, explaining the overall plan in a new question might lead to a better solution for your actual problem.

StephenKing
  • 36,187
  • 11
  • 83
  • 112
-1

I haven't work with chef yet. However, you could load you .yml like this in your Vagrantfile:

require 'yaml'
set = YAML.load_file(ENV['DEVOPS_HOME'] + '/vagrant/server/settings.yml')

And then you could load the variable:

  ..
  vagrant.vm.define "dev.database", autostart: false do |dev|
    database = set['dev']['server']['database']
    vm = set['dev']['vm']
    dev.vm.box = vm['box']
    ..
Valter Silva
  • 16,446
  • 52
  • 137
  • 218
  • That wouldn't work in my case, since I want vagrant to run some lines just when it's used by .kitchen and not doing a vagrant up. So if that attribute is set then execute this additional lines, otherwise not. I will update the question to make it clear – MarMan Jun 24 '16 at 10:58