yz-ngw
Last Updated: February 25, 2016
·
2.269K
· mlafeldt

Detect if cookbook is run by Vagrant

If you, for whatever reason, want to know whether your cookbook is being run by Vagrant, you can find it out this way:

if node[:instance_role] == 'vagrant'
  # do something that should only be done inside
  # a Vagrant box
end

Of course, this assumes that you do not overwrite node[:instance_role].

2 Responses
Add your response

16830

i'm running chefsolo with vagrant and for me node[:instancerole] is undefined

over 1 year ago ·
21281

we had the problem where we wanted to have a single test-kitchen .kitchen.yml file for vagrant and ec2 kitchen drivers (to prevent divergent configurations between .kitchen.yml and .kitchen.local.yml). it looks something like this:

http://stackoverflow.com/questions/26516373/alternate-drivers-with-test-kitchen/30385233#30385233

you can then add attributes that apply to only a given driver, something like this:

attributes: {
  kitchen_driver: "<%= kitchen_driver %>"
}

... or, if you have a larger block...

attributes: {
  someapp: {
    <% if kitchen_driver == 'vagrant' %>
    owner: "vagrant",
    group: "vagrant"
    <% end %>
  },

you should be able to do something similar with vagrant if not using test-kitchen.

over 1 year ago ·