You are currently viewing 2 Methods To Create Custom Message in Vagrant After VM Startup

2 Methods To Create Custom Message in Vagrant After VM Startup

This guide covers how to create a custom message in Vagrant after VM startup using the shell provisioned and post_up_message parameter.

RELATED ARTICLES – Vagrant Articles

Introduction

Vagrant, an open-source tool, empowers developers by creating and managing portable virtual software development environments. Its magic lies in streamlining software configuration management and slashing setup time. With a user-friendly workflow and a penchant for automation, Vagrant dances seamlessly across major providers like VirtualBox, Libvirt, VMware, and AWS.

Displaying a message in Vagrant after your VM is up and running is a simple but useful feature. It can help you provide important information to the user or assist in debugging. We can achieve this using the vagrant shell provisioner and post_up_message parameter.

Vagrant Shell Provisioner

The Vagrant Shell Provisioner is a powerful tool that allows you to run shell scripts on your VM during provisioning. It’s commonly used for tasks like installing software, configuring services, and setting up your development environment. To use the Shell Provisioner, add the following configuration to your Vagrantfile

config.vm.provision "shell", inline: <<-UB
  echo "Here comes the message..."
UB

You can replace the strings in the echo statement to display your messages.

Parameter post_up_message

The config.vm.post_up_message parameter allows you to specify a message that will be shown to the user after running vagrant up. Add the following line to your Vagrantfile

config.vm.post_up_message = "Here comes my custom message..."

Vagrant will display the custom message at last when you run the vagrant up command.

Prerequisites

If you wish to follow along, you need to have Vagrant installed on your machine along with any of the following providers.

  • Virtualbox
  • VMWare
  • HyperV
  • Libvirt(KVM)

RELATED ARTICLE – Different ways to set default provider in vagrant

Create Ubuntu Vagrantfile

For the demonstration purpose, I am using the Ubuntu box.

  1. Create a vagrant file with the Ubuntu box.
$ vagrant init ubuntu/jammy64
  1. Create a minimum vagrant file with the Ubuntu box.
$ vagrant init -m ubuntu/jammy64
  1. Custom vagrant file with Ubuntu box.
Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/jammy64"
  config.vm.box_check_update = false
  config.vbguest.auto_update = false
  config.vm.network "public_network"
  config.vm.synced_folder ".", "/vagrant", disabled: true
end

I will be using the above custom template for demonstration.

Create Custom Message In Vagrant Using Shell Provisioner

The vagrant shell provisioner can be used to display messages during the boot or post-boot stage. You can dynamically pull values from the virtual machine using the shell provisioner. For example, the following shell provisioner will get the IP address using the hostname command and print it as a message.

config.vm.provision "shell", :run => "always", :privileged => false, inline: <<-UB1
  IP=$(hostname -I | awk '{ print $2 }')
  PORT=8080
  echo "+------------------------------------+"
  echo "+ CONNECT TO APP = https://${IP}:${PORT}"
  echo "+------------------------------------+"
UB1

Run the vagrant up command and it will display the following message.

==> default: Configuring and enabling network interfaces...
==> default: Running provisioner: shell...
    default: Running: inline script
    default: +------------------------------------+
    default: + CONNECT TO APP = https://192.168.X.X:8080
    default: +------------------------------------+

The advantage of this approach is, that the shell provisioner can be made to run before any other provisioner or as the last provisioner to display information.

By default, the provisioner will run only on the first time creating the virtual machine. Setting the ‘run’ paramter to ‘always’ will run the provisioner every time.

config.vm.provision "shell", :run => "always"

Create Custom Message In Vagrant Using vm.post_up_message Parameter

The post_up_mesage accepts a string and will run as the last step in the vagrant up command.

config.vm.post_up_message = "++ Connect to the app using https://localhost:8888"

The following message will be displayed once the machine is up and running.

==> 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: ++ Connect to the app using https://localhost:8888 

Create Multiline Custom Message In Vagrant Using Heredoc

Both the shell provisioner and post_up_message parameter support multiline messages using heredoc.

  1. Heredoc with post_up_message parameter. Here the multiline string is stored in a variable called connection using heredoc syntax and the same is expanded with the config.vm.post_up_message parameter.
$connection = <<-'UB1'
  The service is listening on port 8080. Connect using following URL.
  +------------------------------------+
  + CONNECT TO APP = https://localhost:8080    
  +------------------------------------+
UB1

Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/jammy64"
  config.vm.box_check_update = false
  config.vbguest.auto_update = false
  config.vm.post_up_message = $connection
end

Output for the above Vagrantfile.

==> 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:   The service is listening on port 8080. Connect using following URL.
==> default:   +------------------------------------+
==> default:   + CONNECT TO APP = https://localhost:8080    
==> default:   +------------------------------------+
  1. Heredoc with shell provisioner. Here again, the multiline string is stored in a variable called connection using heredoc syntax but the difference here is to print the message, bash built-in statement echo is used.
$connection = <<-'UB1'
  echo "The service is listening on port 8080. Connect using following URL."
  echo "+------------------------------------+"
  echo "+ CONNECT TO APP = https://localhost:8080    "
  echo "+------------------------------------+"
UB1

Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/jammy64"
  config.vm.box_check_update = false
  config.vbguest.auto_update = false
  config.vm.provision "shell", :run => "always", inline: $connection
end

The output for the above vagrantfile.

==> default: Running provisioner: shell...
    default: Running: inline script
    default: The service is listening on port 8080. Connect using following URL.
    default: +------------------------------------+
    default: + CONNECT TO APP = https://localhost:8080
    default: +------------------------------------+ 

Wrap Up

The shell provisioner and the post_up_message parameter are powerful tools that can help you automate the setup of your development environment and provide useful information after the vagrant up command has been executed. By understanding and using these features, you can make your Vagrant experience even more efficient and enjoyable.

Leave a Reply

12 + 14 =