Categories
DevOps

How to display custom message after vagrant machine is started

Display custom message after the vagrant machine is started.

Use config.vm.post_up_message option to set acustom message.

Vagrant.configure("2") do |config|
  config.vm.box = "debian/buster64"

  config.vm.post_up_message = "Debian Buster/64 box"
end

It will display it after the machine is started.

Bringing machine 'default' up with 'virtualbox' provider...
==> default: Checking if box 'debian/buster64' version '10.20210228.1' is up to date...
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
    default: The guest additions on this VM do not match the installed version of
    default: VirtualBox! In most cases this is fine, but in rare cases it can
    default: prevent things such as shared folders from working properly. If you see
    default: shared folder errors, please make sure the guest additions within the
    default: virtual machine match the version of VirtualBox you have installed on
    default: your host and reload your VM.
    default: 
    default: Guest Additions Version: 5.2.0 r68940
    default: VirtualBox Version: 6.1
==> default: Rsyncing folder: /home/milosz/Projects/vagrant/ => /vagrant
==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> default: flag to force provisioning. Provisioners marked to run always will still run.

==> 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: Debian Buster/64 box

A multi-line message is also possible.

Vagrant.configure("2") do |config|
  config.vm.box = "debian/buster64"
  up_message = <<-MSG
Debian Buster/64 box

Additional information:
 * Vagrant Cloud page: https://app.vagrantup.com/debian
 * Quick start: https://wiki.debian.org/Teams/Cloud/VagrantQuickStart
 * Documentation: https://wiki.debian.org/Teams/Cloud/VagrantBaseBoxes
 * Support: https://lists.debian.org/debian-cloud/
  MSG

  config.vm.post_up_message = up_message
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: Debian Buster/64 box
==> default: 
==> default: Additional information:
==> default:  * Vagrant Cloud page: https://app.vagrantup.com/debian
==> default:  * Quick start: https://wiki.debian.org/Teams/Cloud/VagrantQuickStart
==> default:  * Documentation: https://wiki.debian.org/Teams/Cloud/VagrantBaseBoxes
==> default:  * Support: https://lists.debian.org/debian-cloud/
==> default: 

There are also different possibilities as you can read local or remote file contents.

  config.vm.post_up_message = File.read("readme.txt")
  config.vm.post_up_message = Net::HTTP.get(URI.parse('https://vagrantboxes.local/readme.txt'))