使用 "vagrant up" 启动 vagrant 机器后打印消息
Print message after booting vagrant machine with "vagrant up"
我需要在 vagrant up
命令完成时显示一条消息。
我试过定义一个函数:
def hello
puts 'hello'
end
然后调用它和文件结尾:
hello
但它总是在输出的开头处打印,而不是在结尾处打印。如何在最后打印消息?
$ vagrant plugin install vagrant-triggers
然后添加:
config.trigger.after :up do
puts 'hello'
end
到Vagrantfile
。
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
这样,vagrant up
应该以这样的结尾:
==> default: Running provisioner: shell...
default: Running: inline script
==> default: Vagrant Box provisioned!
==> default: Local server address is http://vagrant.dev
添加 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"
}
它将产生这个:
Initializing Ruby Program
This is main Ruby Program
它在 Vagrantfile 中完美运行。
Vagrant 现在内置支持在 vagrant up
之后显示消息。只需将此添加到您的 Vagrantfile
:
config.vm.post_up_message = "This is the start up message!"
然后在您的虚拟机启动后,您会看到这条绿色消息:
==> 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!
您还可以将 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
这将导致输出如下:
==> 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: ------------------------------------------------------
来自@slm 的 heredoc 解决方案非常可爱,但您也可以将 heredoc 放在 中 Ruby 这样:
config.vm.post_up_message = <<-HEREDOC
This is line 1
This is line 2
THis is line 3
HEREDOC
实际上有几个稍微不同的 Ruby heredoc 样式:https://infinum.co/the-capsized-eight/multiline-strings-ruby-2-3-0-the-squiggly-heredoc
我需要在 vagrant up
命令完成时显示一条消息。
我试过定义一个函数:
def hello
puts 'hello'
end
然后调用它和文件结尾:
hello
但它总是在输出的开头处打印,而不是在结尾处打印。如何在最后打印消息?
$ vagrant plugin install vagrant-triggers
然后添加:
config.trigger.after :up do
puts 'hello'
end
到Vagrantfile
。
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
这样,vagrant up
应该以这样的结尾:
==> default: Running provisioner: shell...
default: Running: inline script
==> default: Vagrant Box provisioned!
==> default: Local server address is http://vagrant.dev
添加 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"
}
它将产生这个:
Initializing Ruby Program
This is main Ruby Program
它在 Vagrantfile 中完美运行。
Vagrant 现在内置支持在 vagrant up
之后显示消息。只需将此添加到您的 Vagrantfile
:
config.vm.post_up_message = "This is the start up message!"
然后在您的虚拟机启动后,您会看到这条绿色消息:
==> 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!
您还可以将 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
这将导致输出如下:
==> 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: ------------------------------------------------------
来自@slm 的 heredoc 解决方案非常可爱,但您也可以将 heredoc 放在 中 Ruby 这样:
config.vm.post_up_message = <<-HEREDOC
This is line 1
This is line 2
THis is line 3
HEREDOC
实际上有几个稍微不同的 Ruby heredoc 样式:https://infinum.co/the-capsized-eight/multiline-strings-ruby-2-3-0-the-squiggly-heredoc