after :provision trigger

478 views
Skip to first unread message

Tobias Eckl

unread,
Jul 23, 2018, 7:51:54 AM7/23/18
to Vagrant
Hi all,

I'm running vagrant 2.1.2.
Unfortunately the after :provision trigger is only triggered when doing a vagrant provision
Why won't it trigger on the first vagrant up or a vagrant up --provision.

I found multiple old "solutions" like https://github.com/emyl/vagrant-triggers/issues/21
This one for example fails with:
vm:
* The 'trigger' provisioner could not be found.

what is the current way to execute a trigger after the first time bringing the VM up?

I need this because users etc are changed during provisioning and I need to run vagrant reload to set a new ssh user and mount the defined shares with a new user.

Regards,
Tobias

Tobias Eckl

unread,
Jul 23, 2018, 8:04:29 AM7/23/18
to Vagrant
I discovered a workaround by using the after :up trigger together with a check if the vm was already created:

def machine_exists(vm_name)
  if get_machine_id(vm_name).nil?
    return false
  else
    return true
  end
end

def get_machine_id(vm_name)
  machine_id_filepath = ".vagrant/machines/#{vm_name}/virtualbox/id"
  if not File.exists? machine_id_filepath
    return nil
  else
    return File.read(machine_id_filepath)
  end
end

.
.
.

    if machine_exists("server")
      server.ssh.username = "new-user"
      server.ssh.private_key_path = ....
    else
      server.trigger.after :up do |trigger|
        trigger.name = "Server reload trigger"
        trigger.info = "Server reload trigger"
        trigger.run = {inline: "vagrant reload server"}
      end
    end

Nevertheless the, at least in my opinion, expected behavior would be that the provision trigger fires if provisioning was done.

Brian Cain

unread,
Jul 23, 2018, 12:28:40 PM7/23/18
to vagra...@googlegroups.com
Hello there Tobias!

Please check out the vagrant trigger documentation: https://www.vagrantup.com/docs/triggers/

The trigger feature is for execution scripts before or after vagrant commands. So the behavior you noticed is expected. If you define a :provision trigger, it will fire before or after that command is invoked.

--
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/f8c14015-856d-42de-99b1-6ecef5666b10%40googlegroups.com.

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



--
Brian Cain

Tobias Eckl

unread,
Jul 23, 2018, 1:55:27 PM7/23/18
to Vagrant
Thanks for the fast reply.

So you are telling me that there is no way (beside my ugly workaround) to make a trigger fire only once after the initial creation of the machine?
To unsubscribe from this group and stop receiving emails from it, send an email to vagrant-up+...@googlegroups.com.



--
Brian Cain

Brian Cain

unread,
Jul 24, 2018, 11:28:25 AM7/24/18
to vagra...@googlegroups.com
On Mon, Jul 23, 2018 at 10:55 AM, Tobias Eckl <eckl....@gmail.com> wrote:
Thanks for the fast reply.

So you are telling me that there is no way (beside my ugly workaround) to make a trigger fire only once after the initial creation of the machine?

For now that is correct. Triggers will always fire any time they are defined to run. 
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/c323f046-ff44-4844-ae2f-16679e101dde%40googlegroups.com.

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



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