Possiility to run a kind of post install scripts on the host machine

266 views
Skip to first unread message

Sandra Kosmalla

unread,
Apr 6, 2018, 10:28:40 AM4/6/18
to Vagrant
Hello mailing list,

I have following issue: I'd like to run after a successful 'vagrant up' a script on the host machine. Is there a possibility in the Vagrantfile to reference this script file, so that vagrant runs it automatically after a successful 'vagrant up'?

Thanks you and best regards

Sandra

Alvaro Miranda Aguilera

unread,
Apr 6, 2018, 10:52:30 AM4/6/18
to vagra...@googlegroups.com
Hello

YEs, have a look at vagrant triggers:





--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/mitchellh/vagrant/issues
IRC: #vagrant on Freenode
---
You received this message because you are subscribed to the Google Groups "Vagrant" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vagrant-up+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vagrant-up/50b188f2-90c9-4e09-ab40-3b8fccabefb8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Alvaro

Sandra Parsick

unread,
Apr 8, 2018, 5:20:20 AM4/8/18
to Vagrant
Awesome! Thank you, Alvaro.

Tony Apuzzo

unread,
May 9, 2018, 3:04:11 AM5/9/18
to Vagrant
There is a hack to do this without triggers in a pretty 'clean' fashion:  This approach allows you to reload the machine only the very first time and to run a second set of provisioning.  Uses Ruby's built-in at_exit trigger.

# Function to check whether VM was already provisioned (first run of ``vagrant up``)
# NOTE this would be confused by using more than one provider in the same folder
#      but that's not possible currently.
def provisioned?
  Dir['.vagrant/machines/default/*/action_provision'].any?
end

Vagrant.configure("2") do |config|
  # ....
  unless provisioned?()
    # First call of vagrant up
    if ARGV[0] == 'up'
      # at_exit ensures that Vagrant's lock on the VM is released before re-exec'ing.
      at_exit do
        if $!.nil? || $!.is_a?(SystemExit) && $!.success?
          # First time up, rexecute vagrant to reboot and continue provisioning
          exec "vagrant reload --provision"
        else
          # Oops, we failed during initial up, offer to delete partially created machine
          code = $!.is_a?(SystemExit) ? $!.status : 1
          print "\nERROR Vagrant up failed with code #{code}\n"
          exec "vagrant destroy"
        end
      end
    end
  else
    # Subsequent calls of vagrant up
    # Put your secondary provisioning configuration here
  end
end

Brian Cain

unread,
May 9, 2018, 11:36:19 AM5/9/18
to vagra...@googlegroups.com
Hi all,

You can do this now natively in Vagrant as of 2.1.0 with the new core triggers feature:

Vagrant.configure("2") do |config|
  config.vm.define "guest" do |g|
    g.vm.box = "your-vagrant/box-here"
    g.trigger.after :up,
      run_remote: {path: "path/to/script.sh"}
  end
end


For more information check out the docs: https://www.vagrantup.com/docs/triggers/

Cheers!

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/mitchellh/vagrant/issues
IRC: #vagrant on Freenode
---
You received this message because you are subscribed to the Google Groups "Vagrant" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vagrant-up+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Brian Cain
Reply all
Reply to author
Forward
0 new messages