Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

returning a value from a provision script to vagrant

Tags:

vagrant

Is it possible to return a value from a provision script back to vagrant?

response = config.vm.provision( "shell", path: "script.sh" )

if response = 'ok'
   do_something
end

I couldn't see anything in the vagrant docs describing how this could be done.

like image 670
Chris Snow Avatar asked Jan 14 '14 15:01

Chris Snow


2 Answers

As far as I am aware I don't think it is possible to return a value like that.

You can however easily get around this by having your provisioning script write a file in the /vagrant directory of the guest machine.

You can then use Ruby to process this file which will be in the same folder as your Vagrantfile.

like image 110
Matt Cooper Avatar answered Oct 10 '22 00:10

Matt Cooper


I was able to get it from the VMs by doing this way:

On Windows:

config.trigger.after :provision do |trigger|
    trigger.name = "create token"
    trigger.run = {"inline": "vagrant ssh --no-tty -c 'hostname' master01 > test.txt"}
end

On Mac:

config.trigger.after :provision do |trigger|
  trigger.name = "create token"
  trigger.run = {"inline": "/bin/bash -c 'vagrant ssh --no-tty -c "hostname" master01 > test.txt'"}
end

This will dump the output of the command from the VM to the given file in CWD on the host.

Notes:

  • About bash redirection - why '/bin/bash -c' is required? (here in case of Mac) - https://github.com/hashicorp/vagrant/issues/10674
  • I am using Vagrant 2.2.6
like image 2
Suku Avatar answered Oct 10 '22 00:10

Suku