Print message after booting vagrant machine with "vagrant up"

20k views Asked by At

I need to display a message on the completion of the vagrant up command.

I've tried defining a function:

def hello
    puts 'hello'
end

And then calling it and the end of the file:

hello 

But it always prints at the beginning of the output rather than the end. How can I print a message at the end?

6

There are 6 answers

0
Roman Iuvshin On BEST ANSWER

Once I've started to learn Ruby I've found the ideal solution :)

BEGIN Declares code to be called before the program is run.

#!/usr/bin/ruby
puts "This is main Ruby Program"
BEGIN {
    puts "Initializing Ruby Program"
}

it will produce this:

Initializing Ruby Program
This is main Ruby Program

And it works perfectly inside of Vagrantfile.

2
Jon Burgess On

Try the vagrant-triggers plugin:

$ vagrant plugin install vagrant-triggers

Then add:

config.trigger.after :up do
  puts 'hello'
end

to the Vagrantfile.

1
slm On

You can also use a HEREDOC style variable with the config.vm.post_up_message like so:

$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

Which will result in output like so:

==> 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: ------------------------------------------------------
3
joemaller On

Vagrant doesn't need a plugin to display a message at the end, just add a shell provisioner after all your other provisioners and have that echo whatever you want.

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

With that, vagrant up should end with something like this:

==> default: Running provisioner: shell...
    default: Running: inline script
==> default: Vagrant Box provisioned!
==> default: Local server address is http://vagrant.dev

Adding privileged: false (as mentioned in Vagrant Issue 1673) is necessary to suppress Ubuntu's stdin: is not a tty error.

0
Daniel Rhodes On

The heredoc solution from @slm is super-cute, but you can also put the heredoc in the Ruby thus:

config.vm.post_up_message = <<-HEREDOC
  This is line 1
  This is line 2
  THis is line 3
HEREDOC

Actually there are a couple of slightly different Ruby heredoc styles: https://infinum.co/the-capsized-eight/multiline-strings-ruby-2-3-0-the-squiggly-heredoc

0
Cory Klein On

Vagrant now has builtin support for a message to appear after vagrant up. Just add this to your Vagrantfile:

config.vm.post_up_message = "This is the start up message!"

And then after your VM has come up you'll see this message in green:

==> 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!