Check if Vagrant provisioning has been done

2020-03-01 08:31发布

问题:

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?

回答1:

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.



回答2:

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.



回答3:

I can't find a straightforward way of doing this. Sounds like the simplest solution would be to check the provision result i.e. can you log in with user created after provisioning?

Another option would be to always run vagrant reload --provision to ensure the box is in a provisioned state before continuing.



回答4:

EDIT

Was too fast to answer without trying it out first. The code in the link below may have worked 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

Came here looking for an answer to the same question but none of existing ones were quite satisfying, so with a bit more digging I found this comment on github about how you could go around for 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're issuing is ssh then it will use the username you specified.



标签: vagrant