Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid reprovisioning a Vagrant VM if it is already provisioned

Tags:

vagrant

I am trying to get a shell provisioner to avoid reprovisioning a VM instance if it has already done so previously.

Consider the following Vagrantfile:

Vagrant::Config.run do |config|
  config.vm.define :minimal do |config|
    # Base image
    config.vm.box = "lucid32"
    config.vm.box_url = "http://files.vagrantup.com/lucid32.box"

    config.vm.provision :shell, :inline => "mkdir /tmp/foobar"
  end
end

If you run vagrant up minimal, it will create the box and provision it initially. If you then run vagrant provision minimal it will attempt to reprovision the box but will fail (since the /tmp/foobar directory already exists).

Is there a way to make Vagrant remember whether it has provisioned a machine in the past and to avoid reprovisioning it later?

More context: If I run vagrant up minimal, restart my host machine, and then run vagrant up minimal again, it will try to reprovision the box and fail. This happens reasonably often since VirtualBox frequently causes kernel panics on my host machine.

like image 530
David Foster Avatar asked Jan 10 '13 19:01

David Foster


People also ask

What does vagrant provision mean?

Command: vagrant provision [vm-name]Runs any configured provisioners against the running Vagrant managed machine. This command is a great way to quickly test any provisioners, and is especially useful for incremental development of shell scripts, Chef cookbooks, or Puppet modules.

What is the provision that used in vagrant by default?

By default, provisioners are only run once, during the first vagrant up since the last vagrant destroy , unless the --provision flag is set, as noted above.

Which of following can be used as Provisioner with vagrant?

5) What is Provisioner in Vagrant? A) A provisioner is a tool to set up the virtual environment, and can be as simple as a shell script, but alternatively a more advanced tool like Chef, Puppet, or Ansible can be used.

What does vagrant reload do?

Vagrant reload will reboot the VM, if the VM was provisioned already in the next run reload will skip those by default. vagrant reload --provision will reboot the VM and run the provision steps if any. in the home of the users are a lot of files needed to ssh.


1 Answers

This might not be the answer you want, but if you change that mkdir to a mkdir -p, it will work ;)

In all seriousness, though, I think Vagrant expects provisioners to be idempotent (that is, if run the second time, it will take no action).

It might be tricky to achieve true idempotency, depending on what you're actually doing in your provisioning script, but mkdir -p is a good start. You could also create a flag file on the system, and check existence of that flag file first thing; if it exists, just exit 0.

like image 91
LeeXGreen Avatar answered Sep 17 '22 12:09

LeeXGreen