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)
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.
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.
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.
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.