Rom*_*hin 17 vagrant vagrantfile
我需要在vagrant up命令完成时显示一条消息.
我试过定义一个函数:
def hello
puts 'hello'
end
Run Code Online (Sandbox Code Playgroud)
然后调用它和文件的结尾:
hello
Run Code Online (Sandbox Code Playgroud)
但它始终打印在输出的开头而不是结尾.我怎么能在最后打印一条消息?
Cor*_*ein 24
Vagrant现在内置支持消息显示vagrant up.只需将此添加到您的Vagrantfile:
config.vm.post_up_message = "This is the start up message!"
Run Code Online (Sandbox Code Playgroud)
然后在您的VM出现后,您将看到绿色的此消息:
==> default: Machine 'default' has a post `vagrant up` message. This is a message
==> default: from the creator of the Vagrantfile, and not from Vagrant itself:
==> default:
==> default: This is the start up message!
Run Code Online (Sandbox Code Playgroud)
slm*_*slm 16
您也可以使用HEREDOC样式变量,如下config.vm.post_up_message所示:
$msg = <<MSG
------------------------------------------------------
Local Websphere, accessible at 127.0.0.1
URLS:
- app under test - http://localhost:8080/<app url>/
- ibm console - http://localhost:9060/ibm/console
------------------------------------------------------
MSG
...
...
Vagrant.configure("2") do |config|
config.vm.post_up_message = $msg
end
Run Code Online (Sandbox Code Playgroud)
这将产生如下输出:
==> default: Machine 'default' has a post `vagrant up` message. This is a message
==> default: from the creator of the Vagrantfile, and not from Vagrant itself:
==> default:
==> default: ------------------------------------------------------
==> default: Local Websphere, accessible at 127.0.0.1
==> default:
==> default: URLS:
==> default: - app under test - http://localhost:8080/<app url>/
==> default: - ibm console - http://localhost:9060/ibm/console
==> default:
==> default: ------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)
Vagrant最后不需要插件来显示消息,只需在所有其他配置程序之后添加一个shell配置程序,并根据需要进行回显.
config.vm.provision "ansible" do |ansible|
# ... or other existing provisioners
config.vm.provision "shell", privileged: false, inline: <<-EOF
echo "Vagrant Box provisioned!"
echo "Local server address is http://#{$hostname}"
EOF
Run Code Online (Sandbox Code Playgroud)
有了这个,vagrant up应该结束这样的事情:
==> default: Running provisioner: shell...
default: Running: inline script
==> default: Vagrant Box provisioned!
==> default: Local server address is http://vagrant.dev
Run Code Online (Sandbox Code Playgroud)
添加privileged: false(如Vagrant Issue 1673中所述)对于抑制Ubuntu的stdin: is not a tty错误是必要的.
一旦我开始学习Ruby,我就找到了理想的解决方案:)
BEGIN声明在程序运行之前要调用的代码.
#!/usr/bin/ruby
puts "This is main Ruby Program"
BEGIN {
puts "Initializing Ruby Program"
}
Run Code Online (Sandbox Code Playgroud)
它会产生这个:
Initializing Ruby Program
This is main Ruby Program
Run Code Online (Sandbox Code Playgroud)
它在Vagrantfile中完美运行.
$ vagrant plugin install vagrant-triggers
Run Code Online (Sandbox Code Playgroud)
然后加:
config.trigger.after :up do
puts 'hello'
end
Run Code Online (Sandbox Code Playgroud)
到了Vagrantfile.
| 归档时间: |
|
| 查看次数: |
13347 次 |
| 最近记录: |