I want to display some text on the screen when running vagrant up
(or vagrant provision
, etc.) if and only if provisioning is being done. (For vagrant up
it is only run the first time, or if specifically forced with --provision
.)
How can this be done?
According to the Vagrant issue #7043 where somebody wanted to use
@env[:provision_enabled]
to see if provisioning is being run. It was answered that you could also check the arguments your Vagrantfile was called with:Example usage
I added two functions to the bottom of my Vagrantfile:
Which I can use around any statement in my Vagrantfile:
I'm not sure if I understood your question correctly, but if you want to show a text message if and only if provisioning runs, and you already know that provisioning runs only on first
vagrant up
and when forcing it using the--provision
switch - then why not just add the output of the message to the provisioning itself?This could be as simple as using a shell provisioner and running an
echo
command inside of that.As Vagrant supports multiple provisioners within one Vagrantfile and is able to run all of them when provisioning a virtual machine, this is a dead-easy step, no matter whether you use the shell provisioner anyway, or if you use any other provisioner.
Adding a shell provisioner is probably the easiest solution, with the small cost that it is executed on the VM over SSH.
Another option is to use the vagrant-host-shell plugin:
If you like over-engineering, you can even make your own plugin in Vagrantfile. ;)