Check if brokerage software has been completed.

I use Vagrant to deploy development virtual machines. One of the requirements is that the vagrant provision creates a new user (executed in the provisioning section of the script that I wrote), and then vagrant ssh connects to the mailbox as that user.

I cannot figure out how to determine if a field was provided or not.

I see that the backup Vagrant code clause sets env[:provision_enabled] if this run should be executed, so I thought I could 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 will use one connection for provisioning, and SSH connections for the rest will use another.

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

Is there any way to do this?

+7
vagrant
source share
4 answers

This is apparently determined by the action_provision file in the Vagrant dir data file ( .vagrant/ ). It is usually located in the same folder as your Vagrantfile .

Thus, a tough decision would be to set the ssh username in your Vagrantfile depending on whether the file exists or not. I could not verify this, though, but if you just rename or delete the action_provision file and go on vagrant reload , it should install again.

+7
source share

So, for those who are looking for ready-to-use Vagrantfile code Vagrantfile , here is a function that checks if a virtual machine has been provided and a 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 

Attention! This is a hacking method with checking the existence of the action_provision file. But it works, and at the time of publication there are no other good ways.

+9
source share

EDIT

Too fast to answer without trying first. The code in the link below may work in previous versions, however in 1.7.2 it will not work; You need to tweak it a bit:

 Vagrant.configure("2") do |config| if ARGV[0] == "ssh" config.ssh.username = 'other_username' end ... end 

original answer below

I came here to find the answer to the same question, but none of the existing ones was quite satisfactory, so with a bit more digging, I found this comment on github about how you can get around your requirements:

 Vagrant.configure("2") do |config| if VAGRANT_COMMAND == "ssh" config.ssh.username = 'other_username' end ... end 

You just check if the vagrant command you issue is ssh , then it will use the username you provided.

+2
source share

I cannot find an easy way to do this. It seems that the simplest solution would be to check the result of the provision, i.e. Can you log in with the user created after initialization?

Another option is to always run vagrant reload --provision to make sure the window is in backup access state before proceeding.

+1
source share

All Articles