hashicorp / vagrant

Vagrant is a tool for building and distributing development environments.
https://www.vagrantup.com
Other
26.06k stars 4.42k forks source link

Action trigger before "Vagrant::Action::Builtin::Provision" not working #12514

Open luispabon opened 2 years ago

luispabon commented 2 years ago

Vagrant version

2.2.9 2.2.18

Host operating system

Ubuntu 21.04

Guest operating system

Ubuntu 20.04

Vagrantfile

# -*- mode: ruby -*-
# vi: set ft=ruby :
require 'yaml'

Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/focal64"

  # Workaround for https://bugs.launchpad.net/bugs/1873506
  config.vm.box_version = "20210415.0.0"
  config.vm.box_check_update = false

  # Provision the machine with ansible. This requires you to have ansible installed on your host
  config.vm.provision "ansible" do |ansible|
    ansible.playbook = "ansible/playbook.yaml"
  end

  config_options = YAML.load_file('config.yaml')
  config_options['services'].each do |service|
    config.trigger.before :"Vagrant::Action::Builtin::Provision", type: :action do |trigger|
        trigger.run = { inline: "make init-service-hostnames -e SITE_HOST=" + service['gateway']['hostname'] }
    end
  end

  config.vm.provider "virtualbox" do |vb|
    vb.gui = false
    vb.cpus = 1
    vb.memory = 2048
  end
end

Here's a live example you can checkout https://github.com/phpdocker-io/vagrant-docker/tree/provisioner-fix (note the branch to checkout is provisioner-fix not main). Make sure you ln -s config.yaml.dist config.yaml or the loop where the trigger is won't have any data to loop through.

Debug output

~ vagrant provision 
==> default: Running action triggers before provision ...
==> default: Running trigger...
==> default: Running provisioner: ansible...
    default: Running ansible-playbook...

PLAY [System setup] ************************************************************

TASK [Gathering Facts] *********************************************************
ok: [default]

[ ...  a whole other bunch of ansible output ...]

Note the lack of output after Running trigger. It should look like image

Expected behavior

Vagrant runs the make init-services-hostnames ... command on the host computer

Actual behavior

Nothing happens. Running on the :provision action, instead of the builtin class on the example above, works but partially - it won't run on the first vagrant up or vagrant reload --provision. The objective is to run that command before provisioning, regardless of how the provisioning is triggered.

Steps to reproduce

  1. Set vagrant file
  2. Run vagrant up or vagrant provision or vagrant reload --provision

References

n/a

soapy1 commented 2 years ago

Hey there, dependency provisioning is an experimental feature. In order to use it you must enable experimental mode. If the problem persists after that has been enabled, could you provide a debug log.

Cheers!

luispabon commented 2 years ago

Thank you @soapy1

Indeed the problem seems to persist. I upgraded to the very latest vagrant available on Hashicorp's Ubuntu repos (2.2.18), set VAGRANT_EXPERIMENTAL=dependency_provisioners then re-created the machine from scratch and gathered logs for the first vagrant up, and also vagrant provision and vagrant reload --provision.

The vagrant setup lives here (notice the branch): https://github.com/phpdocker-io/vagrant-docker/tree/provisioner-fix

Log files (generated with 2.2.18):

sam-xif commented 2 years ago

Hi @soapy1 and @luispabon

Any updates on this? I am trying to write an action trigger like so:

config.trigger.before :"Vagrant::Action::Builtin::Provision", type: :action do |trigger|

that runs on vagrant up. It doesn't run on vagrant up nor does it run on vagrant up --provision, so @luispabon's workaround in https://github.com/phpdocker-io/vagrant-docker/commit/602949872c50c280b032565c7844720357131a69 doesn't work for me.

ajmeese7 commented 2 months ago

Hitting the same issue as @sam-xif, I would really like to be able to have a trigger that works before the provision action so it will work as expected on vagrant up --provision and not just vagrant provision.