0

And so, I have a Vagrantfile with code:

VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
...
  config.vm.provision "shell", inline: 'echo "Read line:" && read t && echo "$t"'
...
end

and when I execute vagrant provision i have that output:

$ vagrant provision
...
==> default: Running provisioner: shell...
    default: Running: inline script
==> default: Read line:
==> default: exit

that is no suggest enter a line from keyboard/stdin!

What I should do for reading line from keyboard/stdin during provision shell script execution?

Alexey Egorov
  • 2,221
  • 2
  • 18
  • 21

1 Answers1

2

I do it this way, in my Vagrantfile

puts "Input text: "
input = STDIN.gets.chomp

config.vm.provision :shell, inline: "echo #{input}"
Frederic Henri
  • 51,761
  • 10
  • 113
  • 139
  • Yes, it is solution of my example `echo "Read line:" && read t && echo "$t"`. But I need more general solution. **How make it if in my provision shell script used command which requires keyboard input directly?** – Alexey Egorov Apr 30 '16 at 05:21
  • 1
    as far as I last see, it was not possible ([reference](http://vagrant.1086180.n5.nabble.com/bootstrap-sh-pause-for-input-after-each-command-td1399.html)) and so I've been using this way - might have changed but I doubt, provisioning vagrant goal is really to be 100% automatic, in your case might not be perfect but you could workaround with the example I gave – Frederic Henri Apr 30 '16 at 07:08
  • Thanks for answer. I've read your reference. And I will use yours way. – Alexey Egorov May 02 '16 at 01:54
  • 2
    Hi. I use yours way. It works! But now `vagrant destroy` required keyboard input:) – Alexey Egorov May 08 '16 at 01:53
  • Oh right :) you can maybe had a check on the first argument of vagrant if "up" then add the key board input – Frederic Henri May 08 '16 at 07:40
  • 2
    This helped me but I wanted user input not to be displayed as they entered which is useful for a password prompt. `pass = STDIN.noecho(&:gets)` – mrjamesmyers Dec 30 '17 at 00:03
  • this will ask every time you run vagrant up , cant use once option with it – Muhammad Omer Aslam Sep 23 '21 at 16:11
  • @mrjamesmyers A line break is entered. Instead `pass = STDIN.noecho(&:gets).chomp` – YujiSoftware Nov 02 '21 at 12:50