I am doing a bunch of work in VMs managed with Vagrant+chef, and I have several different VMs configured for different projects/roles so that I can do dev work on a VM that has just the set of things on it that a QA or production box will have. To make it easier, I put together some bash functions and a tab completion setup in my .bash_profile to make it easier to work with vagrant and multiple VMs. I will describe what I wanted to accomplish and how I did it.
What I wanted was to be able to have multiple VMs and have it be easy to run a command against any of them without having to cd (change directory) to the correct directory for a server and then do what I needed. The idea is that the following commands should behave as follows:
To make the scripting easy, I decided that all the Vagrant setups would live in one directory.
I put the following code in my ~/.bash_profile file (also available in this gist):
The vag function takes care of the vag server\name_ vagrant\sub-command ..._.
The \vagrant_servers()_ function and the complete -F ... command that follows it handles the tab completion. For the first argument to the commands it completes for, it shows the list of servers (directory names) in the common directory that your vagrant servers live in. If you are using the vag command, it will provide tab completion on the second argument with the list of vagrant sub-commands available. The list of subcommands was scraped out of the invocation of the vagrant command line with no arguments earlier in the script where VAGRANT_SUBCMDS is defined. No other arguments past those supported get any tab completion.
Finally, the firevm shell function takes care of the special handling to resume the vm, ssh into it, and then suspend it.