Vagrant box运行python并等待

时间:2017-05-11 19:24:36

标签: python vagrant vagrantfile

我目前正在配置一个流浪盒并运行一个python脚本;

  config.vm.provision :shell, :inline => "python first.py", :privileged => false

现在这个工作并且它会激活但是在python脚本本身,我要求通过python脚本 raw_input。但是,在配置流浪者时,输入才会被跳过。

==> default: Checking if box 'box' is up to date...
==> default: Running provisioner: shell...
    default: Running: inline script
==> default: Do you want to download the database? [Y/n] Please respond with 'yes' or 'no' (or 'y' or 'n').
==> default: Do you want to download the database? [Y/n]
==> default: Traceback (most recent call last):
==> default:   File "first.py", line 110, in <module>

有没有人知道为什么会这样,以及如何阻止它并等待回复。

1 个答案:

答案 0 :(得分:0)

Vagrant通过ssh连接运行脚本,因此没有stdin,所以你不能运行这个命令(或者你可以运行它们但是它们没有预期的输出)

解决此案例的一种方法是在Vagrantfile本身期间提出问题并将答案传递给脚本。像这样的东西

Vagrant.configure("2") do |config|
  ....
  print "Do you want to download the database? [Y/n] Please respond with 'yes' or 'no' (or 'y' or 'n')."
  answer = STDIN.gets.chomp

  config.vm.provision :shell, :path => "setup.sh", :args => [ answer ]
  ....
end

主要缺点是,每次运行vagrant命令时都会询问这个问题,因此可能会有点痛苦。