Yordan Grigorov
Yordan Grigorov

Reputation: 203

How to vagrant halt an environment by name, not by id

From the vagrant documentation:

vagrant halt [name|id]

Let's say vagrant global-status outputs the following:

id       name    provider   state    directory
------------------------------------------------
6e16e1a  envname virtualbox running  D:/git/envname

So, from outside the directory containing Vagrantfile I should be able to halt this machine either with

vagrant halt 6e16e1a #works!

As well as with

vagrant halt envname #doesn't work!

The error message:

A Vagrant environment or target machine is required to run this command. Run vagrant init to create a new Vagrant environment. Or, get an ID of a target machine from vagrant global-status to run this command on. A final option is to change to a directory with a Vagrantfile and to try again.

This is the same output as when I vagrant halt in a directory with no Vagrantfile in it.

So, Can I vagrant halt by name, if yes, how?

Vagrant 2.2.14 on Windows 10

Vagrantfile excerpt:

config.vm.hostname = "envname"
config.vm.define "envname"

Upvotes: 4

Views: 489

Answers (1)

crogers
crogers

Reputation: 636

I couldn't find a "clean" solution to this problem so I wrote a little bash function to get the box id from the box name.

function vgid() {
  BOX_NAME=$1
  BOX_ID=$(vagrant global-status --prune | grep $BOX_NAME | awk '{print $1}')

  echo $BOX_ID
}

Using this function I can now use the box's name in commands like this:

vagrant up $(vgid name)

It's not my favorite solution but it does work and I prefer it to copy-pasting ids from the global-status output.

Upvotes: 0

Related Questions