Archives

Categories

Customising Vagrant behaviour

Vagrant is a great piece of software and an integral part of my development toolbox.

One thing that a lot of people don’t realise about Vagrant is that the Vagrantfile which you use to store your configuration in is just Ruby code. This means you can quickly and easily customise the behaviour of Vagrant.

Here are a few quick examples to get you going, place the following snippets in your Vagrant file and then execute a Vagrant command like
vagrant up to see their output.

Output the folder path of your Vagrantfile

1

2

vagrantPath=File.dirname(__FILE__)

puts vagrantPath

Output the Vagrant command (up, halt, destroy etc.)

1

puts ARGV[0]

Output all of the Vagrant command line arguments

1

2

3

ARGV.eachdo|a|

putsa

end

Output the chosen Vagrant provider

1

2

3

4

5

6

7

provider=''

ARGV.eachdo|a|

ifa.include?('--provider=')

provider=a[11,a.length]

end

end

puts provider

Output the username running Vagrant

1

puts ENV['USER']

As you can see adding these snippets to your Vagrantfile is easy and allows powerful custom logic. You could provision certain resources based on the user running Vagrant or stop people from being able to run commands like
vagrant halt, it is very flexible.