The Goal
I am attempting to conditionally run the vagrant-berkshelf plugin. By default, enabling the plugin causes Berkshelf to resolve and vendor cookbooks on every single vagrant up (which is a relatively expensive operation) even if the current vagrant operation isn't a provisioning run. For example, I expect Berkshelf to run when I run:
- vagrant upthe first time, or
- when I execute vagrant reload --provision.
The source implies there ought to be a way to query Vagrant itself to determine if it's a provisioning run. Specifically, there ought to be a way to hook into @env[:provision_enabled] or vagrant.actions.vm.provision, but I'm unable to figure out how to do this from within the Vagrantfile itself.
Is this method actually bound to the Vagrant object? If not there, then where? And how can I introspect it?
Software Versions
- Vagrant 1.8.1
- vagrant-berkshelf 4.1.0
What I've Tried
As a workaround, I have tried moving the Berkshelf plugin inside the Chef block, intending that it only run when the Chef provisioner does. For example:
Vagrant.configure(2) do |config|
  config.berkshelf.enabled = false
  config.vm.provision :chef_solo do |chef|
    config.berkshelf.enabled = true
  end
end
However, Berkshelf still runs every time I vagrant up or vagrant reload, which is not the desired behavior. The cookbooks are still resolved and vendored on each run. Consider the following elided output:
==> default: Updating Vagrant's Berkshelf...
==> default: Resolving cookbook dependencies...
==> default: Using karaf (0.2.1)
==> default: Vendoring karaf (0.2.1) to /Users/foo/.berkshelf/vagrant-berkshelf/shelves/berkshelf20160215-19428-unzcx1-default/karaf
Questions That Aren't Duplicates of This One
There is a vaguely related question where the accepted answer is an ugly hack that looks for the presence of .vagrant/machines/default/virtualbox/action_provision or similar, but it is not an exact duplicate of this question as it doesn't address how to programmatically query Vagrant's internal state through the runtime objects or API Vagrant exposes. It may hold a pragmatic (if kludgey) answer based on filesystem semaphores, but it does not answer the question I'm actually asking.
 
                        
If it's possible for you to provision using
vagrant provisioninstead of the--provisionflag you can check with the following code in Vagrantfile:The code inside the conditional will only run on
vagrant provision. This won't work when usingvagrant reloadwith the--provisionflag as you were saying, but you could simply runvagrant provisionafter runningvagrant reload. You can also useif ARGV[0] == 'up'orif ARGV[0] == 'reload'to run when using other commands.See: Getting command line arguments inside the Vagrantfile