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.
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.
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.
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.
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With