Last Updated: February 25, 2016
· 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

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

2 Responses
Add your response


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

over 1 year ago ·

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:


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 ·