Vagrant Commands in Depth

Spread the love

In this guest post by Alex Braunton, a web development expert, you will learn about some of the most important Vagrant commands and sub-commands.

A brief note on formatting commands

In this article, you’ll come across certain keywords as placeholders. These placeholders are for you to enter values into the commands and sub-commands. A typical placeholder will look like this: [INSERT VALUE].

An example would be vagrant login –user [INSERT VALUE], where [INSERT VALUE] would be something such as myusername and the final command that you input would be vagrant login –user myusername. There is no need for capital letters or square brackets.

When you see the [VMNAME] placeholder, this refers to a specific Vagrant machine name that you want to run the command against on your system. The default Vagrant machine is called default, so an example command would be vagrant resume default, which would resume the machine from a suspended state.

General Vagrant commands and sub-commands

The general commands and sub-commands in Vagrant are not category-specific. They may only get used once or serve an incredibly specific purpose.

The list-commands command

The list-commands command will list all the available commands within the main vagrant command scope. It will alphabetically list each command and give a brief description.

Options/flags

There is only one flag for this command, -h/–help, which will print the help screen for this command.

An example is the vagrant list-commands –help command.

The help command

The help command will show you the correct syntax for a command and list a selection of the most popular Vagrant commands.

The version command

The version command will return the version of Vagrant that is currently installed on your system, the latest version available online, and supply a website URL to the downloads page of the Vagrant website.

Options/flags

There is only one flag for this command, -h/–help, which will print the help screen for this command.

An example is the vagrant version –help command.

The global-status command

The global-status command will return information about all of the Vagrant environments associated with the current user. It will return the Vagrant environment ID, name, provider, state, and directory. This command can be useful for giving you an overview into what is happening on your system with the Vagrant environments.

Options/flags

There are two flags for this command:

  • -h/–help: Prints the help screen for this command
  • –prune: Prunes any invalid entries

An example is the vagrant global-status –help command.

Vagrant’s configuration commands and sub-commands

In this section, you will look at Vagrant’s configuration commands and sub-commands. These are often used to configure Vagrant, such as installing a package or taking a snapshot of an environment.

The login command

The login command is used to log into your HashiCorp Vagrant Cloud account. Logging into the Vagrant Cloud will allow you to access and download protected boxes and the Vagrant Share service, which allows you to share your Vagrant environment with anyone.

Options/flags

There are six flags for this command:

  • -c/–check: Checks to see whether you are already logged in
  • -d/–description [INSERT VALUE]: Takes a parameter to set a description of the token
  • -k/–logout: Logs you out if you are already logged in
  • -t/–token [INSERT VALUE]: Takes a parameter to set the Vagrant Cloud token
  • -u/–username [INSERT VALUE]: Takes a parameter to specify your Vagrant Cloud email or username
  • -h/–help: Prints the help screen for this command

An example is the vagrant login –check command.

The package command

The package commands allow you to create a Vagrant box out of a running Vagrant environment.

Options/flags

There are five flags for this command:

  • –base [INSERT VALUE]: Takes a parameter of the name of a virtual machine in VirtualBox to package as a base box. This flag only works if you are using the VirtualBox provisioner.
  • –output [INSERT VALUE]: Takes a parameter to name the output file.
  • –include [INSERT VALUE, INSERT VALUE]: Takes comma-separated parameters to include additional files in this packaging process.
  • –vagrantfile [INSERT VALUE]: Takes a parameter of the Vagrantfile you wish to package into this box.
  • -h/–help: Prints the help screen for this command.

An example is the vagrant package –help command.

The snapshot command

The snapshot command allows you to manage snapshots of Vagrant environments. You can save, delete, and restore snapshots. Only certain providers support snapshotting If your provider does not, Vagrant will return a warning when this command is run.

sub-commands

There are six sub-commands available for the snapshot command:

  • vagrant snapshot delete [INSERT VALUE] [INSERT VALUE]: The first parameter is the virtual machine name and the second parameter is the name of the snapshot you wish to delete.
  • vagrant snapshot list [INSERT VALUE]: The parameter can optionally be the Vagrant machine name. This command will list all available snapshots on your system or all snapshots for a certain Vagrant machine if the parameter is specified.
  • vagrant snapshot push: This can be used to create a new snapshot of a running Vagrant environment. It will add this onto the Snapshot stack.
  • vagrant snapshot pop: This can be used as an opposite to the push command to reverse a snapshot that has been pushed.
  • vagrant snapshot save [INSERT VALUE] [INSERT VALUE]: This saves a snapshot of the current machine. It’s similar to the push command, but it is recommended that you do not mix this command with push or popas it is unsafe. The first parameter is the virtual machine name and the second parameter is the snapshot name.
  • vagrant snapshot restore [INSERT VALUE] [INSERT VALUE]: This restores a supplied snapshot. The first parameter is the virtual machine name and the second is the name of the snapshot you wish to restore to.

The provider command

The provider command will return the provider for the current running machine, or it can accept an environment ID.

Options/flags

There are three flags for this command:

  • –install: Attempts to install the provider
  • –usable: Checks whether the provider is usable
  • -h/–help: Prints the help screen for this command

An example is the vagrant provider –install command.

The plugin command

The plugin command allows you to manage Vagrant plugins. You can install, list, repair, uninstall, and update plugins.

sub-commands

There are seven sub-commands available for the plugin command:

  • vagrant plugin expunge: Removes all user-installed plugins. This will remove any data and dependencies associated with them. This is a useful command if you wish to delete them all in one go.
  • vagrant plugin install [INSERT VALUE]: Installs a plugin by supplying its name as the command’s parameter. You can install a plugin from a known Gem or from a local Gem file on your system.
  • vagrant plugin license [INSERT VALUE] [INSERT VALUE]: Installs a proprietary Vagrant plugin license. The first parameter is the plugin name and the second is the licence file.
  • vagrant plugin list: Lists all installed plugins on your system. It will list plugin information, such as its version. This command is useful for finding out which plugins and versions you have installed.
  • vagrant plugin repair: Attempts to repair any plugins where there has been an issue and it is not working correctly. The issue could be during the installation of a custom plugin or an error with the plugins.json file.
  • vagrant plugin uninstall [INSERT VALUE]: Deletes a plugin using a supplied plugin name as the first parameter. This command supports multiple parameters, with each one being a plugin name. You can delete one or multiple plugins using this command.
  • vagrant plugin update [INSERT VALUE]: Updates a specific plugin if the first parameter is supplied as the plugin’s name. If no parameter is supplied, this command will update all installed plugins.

The cap command

The cap command allows you to run or check the capabilities of a guest machine. These capabilities are often guest-specific and are configured specifically, for example, in Vagrant plugin development.

Options/flags

There are two flags for this command:

  • –check [INSERT VALUE] [INSERT VALUE]: Checks for a certain capability. The first parameter is the capability name and the second parameter is the capability arguments. This command will not run the capability.
  • -h/–help: Prints the help screen for this command.

An example is the vagrant cap –help command.

If this article was helpful and piqued your interest in leveraging Vagrant to implement end-to-end DevOps and infrastructure management, you can check out Hands-On DevOps with Vagrant. Written by Alex Braunton, Hands-On DevOps with Vagrant is for system administrators, and DevOps engineers and architects who want to explore Vagrant and start using it as a powerful DevOps tool.

Leave a Reply

Your email address will not be published. Required fields are marked *