Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I programmatically query Vagrant for its provisioning status?

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 up the 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.

like image 338
Todd A. Jacobs Avatar asked Feb 15 '16 19:02

Todd A. Jacobs


People also ask

What is provisioning in vagrant?

Provisioners in Vagrant allow you to automatically install software, alter configurations, and more on the machine as part of the vagrant up process. This is useful since boxes typically are not built perfectly for your use case.

What is the Provisioner that used in vagrant by default?

Every Vagrant provisioner accepts a few base options. The only required option is what type a provisioner is: name (string) - The name of the provisioner.

How do I destroy vagrant machine?

Command: vagrant destroy [name|id] This command stops the running machine Vagrant is managing and destroys all resources that were created during the machine creation process. After running this command, your computer should be left at a clean state, as if you never created the guest machine in the first place.

What is vagrant Vagrantfile?

Vagrant is an open-source tool that helps us to automate the creation and management of Virtual Machines. In a nutshell, we can specify the configuration of a virtual machine in a simple configuration file, and Vagrant creates the same Virtual machine using just one simple command.


1 Answers

If it's possible for you to provision using vagrant provision instead of the --provision flag you can check with the following code in Vagrantfile:

if ARGV[0] == 'provision'
  # Run berkshelf plugin
end

The code inside the conditional will only run on vagrant provision. This won't work when using vagrant reload with the --provision flag as you were saying, but you could simply run vagrant provision after running vagrant reload. You can also use if ARGV[0] == 'up' or if ARGV[0] == 'reload' to run when using other commands.

See: Getting command line arguments inside the Vagrantfile

like image 192
Danny Harding Avatar answered Nov 04 '22 10:11

Danny Harding