19

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 Answers 6

28

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

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: ------------------------------------------------------
1
  • You can also use variables, like http://localhost:#{HTTP_PORT}, where HTTP_PORT=8080 is defined earlier in the Vagrantfile.
    – dbernard
    Commented Feb 25, 2022 at 13:55
8

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.

3
  • 1
    Note that if you want to use a Ruby variable in the args, this question has you covered. Commented Mar 31, 2016 at 15:01
  • What if you have a Vagrantfile that brings up multiple VM's? I don't want the up message to show up several times. i =really= want it to show up after all the processing is done and before vagrant exits. Commented Oct 1, 2017 at 17:52
  • 1
    This still works but should be considered obsolete in favor of config.vm.post_up_message as mentioned in Cory Klein's answer from 2016-04-26
    – joemaller
    Commented Jan 13, 2018 at 13:58
7

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.

5

Try the vagrant-triggers plugin:

$ vagrant plugin install vagrant-triggers

Then add:

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

to the Vagrantfile.

2
  • This will only execute on the first vagrant up, and only thereafter for vagrant provision. Hence, it might not do quite what you want. Commented Jun 15, 2015 at 5:13
  • Updated answer - came across a different plugin which might do what you want. Commented Jul 22, 2015 at 1:08
1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.