Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if Vagrant provisioning has been done

Tags:

vagrant

I am using Vagrant to deploy VMs for development. One of the requirements is that vagrant provision creates a new user (done in a provisioning script I wrote) and then vagrant ssh connects to the box as that user.

I cannot figure out how to tell if the box has been provisioned or not.

I see that the Vagrant provisioning code sets env[:provision_enabled] if this run is supposed to be doing provisioning, so I thought I would be able to do something like this:

if env[:provision_enabled]
  config.ssh.username = "#{data['ssh']['provision_username']}"
else
  config.ssh.username = "#{data['ssh']['username']}"
end

The idea is that SSH connections for provisioning would use one connection and SSH connections for everything else would use the other.

However, env[:provision_enabled] does not appear to be accessible in the Vagrantfile.

Is there a way to do this?

like image 337
Moshe Katz Avatar asked Jul 20 '14 22:07

Moshe Katz


2 Answers

So for those who's looking for ready to use Vagrantfile code instructions, here is a function which checks if VM was provisioned and usage example:

# Function to check whether VM was already provisioned
def provisioned?(vm_name='default', provider='virtualbox')
  File.exist?(".vagrant/machines/#{vm_name}/#{provider}/action_provision")
end

Vagrant.configure("2") do |config|
  # do something special if VM was provisioned
  config.ssh.username = 'custom_username' if provisioned?

  [...]
end

Warning! It's hackery method with checking action_provision file existence. But it works and at the moment of posting, there are no other good ways.

like image 141
armab Avatar answered Oct 19 '22 12:10

armab


This seems to be determined by the action_provision file in the Vagrant data dir (.vagrant/). It's usually located in the same folder as your Vagrantfile.

So a crude workaround would be to set the ssh username in your Vagrantfile depending on if the file exists or not. I haven't been able to test this though, but if you just rename or remove the action_provision file and go vagrant reload it should provision again.

like image 9
Jahaja Avatar answered Oct 19 '22 13:10

Jahaja