Skip to content

Instantly share code, notes, and snippets.

@kesonno
Created February 26, 2016 09:50
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kesonno/11c458caea4698345d56 to your computer and use it in GitHub Desktop.
Save kesonno/11c458caea4698345d56 to your computer and use it in GitHub Desktop.
Ask for user input in Vagrantfile during provisioning phase
if Dir.glob("#{File.dirname(__FILE__)}/.vagrant/machines/#{VM_NAME}/*").empty? || ARGV[1] == '--provision'
print "Please insert your credentials\n"
print "Username: "
username = STDIN.gets.chomp
print "Password: "
password = STDIN.noecho(&:gets).chomp
print "\n"
config.vm.provision :shell, :path => "provision-vagrant.sh", :args => [username, password]
end
@cAstraea
Copy link

cAstraea commented Apr 1, 2016

Hello Can you help me out a bit with the if Dir.glob("#{File.dirname(FILE)}/.vagrant/machines/#{VM_NAME}/*").empty? || ARGV[1] == '--provision' please , the problem I'm having is that the prompt gets executed on every vagrant command , i only need to run it on vagrant up

@createvibe
Copy link

for anyone else coming up on this - VM_NAME needs to be your VM name. Try replacing it with default or look at the location .vagrant/machines for the directory and you will see.

@nickvoegeli
Copy link

nickvoegeli commented Aug 11, 2017

Super helpful, thanks! I used this basic idea to create shared folders for two related repos in the same VM:

  print "Enter the path to the sites repo: "
  sites_repo_path = STDIN.gets.chomp
  print "\n"

  # Share an additional folder to the guest VM. The first argument is the path on the host to the actual folder.
  # The second argument is the path on the guest to mount the folder.
  config.vm.synced_folder "./", "/var/www/html", group: "www-data", user: "vagrant"
  config.vm.synced_folder sites_repo_path + '/app/Config/Sites', "/var/www/html/app/Config/Sites", group: "www-data", user: "vagrant"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment